0% found this document useful (0 votes)
11 views15 pages

Chapter 4 Location and Map

Chapter 4 covers the integration of location-based services and Google Maps in Android applications. It explains how to use the FusedLocationProviderClient for location tracking, handle permissions, and utilize the Google Maps API for creating map-based activities. Key concepts include geolocalization, location services, and necessary configurations in the AndroidManifest.xml and build.gradle files.

Uploaded by

Yesuneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

Chapter 4 Location and Map

Chapter 4 covers the integration of location-based services and Google Maps in Android applications. It explains how to use the FusedLocationProviderClient for location tracking, handle permissions, and utilize the Google Maps API for creating map-based activities. Key concepts include geolocalization, location services, and necessary configurations in the AndroidManifest.xml and build.gradle files.

Uploaded by

Yesuneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 4

Location and Map


Learning Objectives:
• Understand the basics of location-based services in Android
• Learn how to integrate Google Maps into an Android application
• Implement location tracking using the
FusedLocationProviderClient
• Understand how to handle permissions and manage APIs for
location services.
Contiued….
• Location Services: Allow apps to determine the user's current
location.
• Google Play Services:
• Provides APIs like FusedLocationProviderClient for location
services.
• Use Cases:
• Real-time navigation (e.g., Google Maps)
• Location-based notifications (e.g., "You are near a store")
• Activity tracking (e.g., fitness apps).
Continued…
• Geolocalization  Identification of the real-world geographic location of
the end-user.
• location-based services (LBS): enable you to find the device’s current
location.
 LOCALIZATION THROUGH GPS
 LOCALIZATION THROUGH WI-FI
 LOCALIZATION THROUGH CELLULAR NETWORK

• How? You can specify which technology to use


• explicitly by the name
• you can provide a set of Criteria in terms of accuracy, cost, and other
requirements.
Continued …
• Maps and location-based services use latitude and longitude to
pinpoint geographic locations, but your users are more likely to think
in terms of a street address.
• The maps library includes a geocoder that you can use to convert
back and forth between latitude/longitude values and real-world
addresses.
• Using the external Maps library included as part of the Google API
package, you can create map-based Activities using Google Maps as a
user interface element.
• You have full access to the map, which enables you to control display
settings, alter the zoom level, and pan to different locations.
Integrating Google Maps Step
• 1: Enable the Google Maps API
• Go to the Google Cloud Console.
• Create or select a project.
• Enable the "Maps SDK for Android" API.
• Generate an API key.
Getting a Google Play API Key
 Note: You need an Android API key, not a browser key. You can use
the same API key for your Google Maps Android API apps and your
Google Places API for Android apps.
 Access your project's API keys and other credentials as
follows:
1. Go to the Google cloud
Console(https://cloud.google.com/cloud-console/)
2. In the sidebar on the left, select Credentials.
3. If your project doesn't already have an Android API key,
create one now by selecting Add credentials > API key >
Android key.
4. Your new Android API key appears in the list of API keys for
your project. An API key is a string of characters,
something like this:
AIzaSyA5vdR8ddYADWuQlXcrvosEar8qYCuJ-Vo
7
Continued …

8
• we need to generate Google Map API key. The key is generated freely
from google cloud console
Callback methods in Google Map
• OnMapRreadyCallback: This callback interface invokes when it instance is set on
MapFragment object. The onMapReady(GoogleMap) method of OnMapReadyCallback
interface is called when the map is ready to used. In the onMapReady(GoogleMap)
method we can add markers, listeners and other attributes.
• LocationListener: This interface is used to receive notification when the device
location has changed. The abstract method of LocationListener
onLocationChanged(Location) is called when the location has changed.
• .ConnectionCallbacks: This interface provide callbacks methods
onConnected(GoogleApiClientBundle) and onConnectionSuspended(int) which are
called when the device is to connected and disconnected.
GoogleApiClient.OnConnectionFailedListener: This interface provide callbacks method
onConnectionFailed(ConnectionResult) which is called when there was an error in
connecting the device to the service.
Continued…
• Update build.gradle (app-level):
• implementation 'com.google.android.gms:play-services-
maps:18.1.0’
• Update AndroidManifest.xml:
• <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
• <uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
• <uses-permission android:name="android.permission.INTERNET" />
• permission android:name="android.permission.ACCESS_NETWORK_STATE" />
• <application> <meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" /></application>

• My_API_KEY AIzaSyA5vdR8ddYADWuQlXcrvosEar8qYCuJ-Vo
USING LOCATION-BASED SERVICES
 “Location-based services” is an umbrella term that describes the
different technologies you can use to find a device’s current
location.
 The two main LBS elements are:

Location Manager - This class provides access to the system location
services. These services allow applications to obtain periodic updates of the
device’s geographical location, or to fire an application-specified Intent when
the device enters the proximity of a given geographical location.

Location Providers -Each of these represents a different location-fi nding
technology used to determine the device’s current location.
 Using the Location Manager, you can do the following:

Obtain your current location

Follow movement

Set proximity alerts for detecting movement into and out of a specified area

Find available Location Providers

Monitor the status of the GPS receiver

11
 Access to the location-based services is provided by
the Location Manager.
 To access the Location Manager, request an
instance of the LOCATION_SERVICE using the
getSystemService method, as

 Before you can use the location-based services, you


need to add one or more uses-permission tags to
your manifest.
 The following snippet shows how to request the fi ne
and coarse permissions in your application manifest:

12
Basic Classes and Methods
public class GoogleMapActivity extends AppCompatActivity implements
OnMapReadyCallback
SupportMapFragment mapFragment=(SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.id_ma
p);
mapFragment.getMapAsync(this);
And implements the following method
public void onMapReady(GoogleMap googleMap) {
//Location of Addis ababa 9°1′48″N 38°44′24″E
LatLng location=new LatLng(9.148,38.4424);
googleMap.addMarker(new MarkerOptions().position(location).title("Addis
Ababa"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(location));
googleMap.addMarker(new
MarkerOptions().position(location).title("Addis
LatLng locationAA=new LatLng(9.148,38.4424); Ababa"));
LatLng locationBDR=new LatLng(11.5936,37.3908);
LatLng locationDire=new LatLng(9.350,41.520);
LatLng locationDessie=new LatLng(11.8,39.38);
googleMap.addMarker(new MarkerOptions().position(locationAA).title("Addis
Ababa"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(locationAA));
googleMap.addMarker(new MarkerOptions().position(locationBDR).title("Bahir
Dar"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(locationBDR));
googleMap.addMarker(new MarkerOptions().position(locationDire).title("Dire
Dawa"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(locationDire));
googleMap.addMarker(new
MarkerOptions().position(locationDessie).title("Dessie Wollo"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(locationDessie));
The
End

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy