Chapter 4 Location and Map
Chapter 4 Location and Map
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
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