In the mobile applications world, one idea that is implemented in a very good way is the use of maps and the location-based applications, that help us in multiple ways in our everyday life. particularly store location.
Google provides via Google Play Services a library for using maps. Maps allow users to explore the world more clearly and let them identify locations with custom markers and much more.
1. Setup for Implementing Google Map
To use Google Maps, set up the Google Play services SDK in your app development project.
compile ‘com.google.android.gms:play-services:6.5.87’
1.1 Create a Google Maps API key for Android
To access the Google Maps tools, you need an API key, which you can obtain through a Google account. The key is based on your Android app debug or release certificate. More information can be found on following links:
https://console.developers.google.com/
https://developers.google.com/maps/documentation/android-api
1.2 Add permissions and metadata to the Manifest
We need to add a feature element for OpenGL ES version 2:
1 2 3 4 |
<!-- Required OpenGL ES 2.0. for Maps V2 --> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> |
add metadata for google API:
1 2 3 4 5 6 7 8 9 |
<!-- Goolge API Key --> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_map_api_key_here" /> <!-- Required after latest Google Play Services update --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> |
2. Implementaion
2.1 MapFragment
The MapFragment
class extends the Fragment
class and provides the life cycle management and the services for displaying aGoogleMap
widget. GoogleMap
is the class which shows the map. The MapFragment
has the getMap()
method to access this class.
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
2.2 Example Activity
1 2 3 4 5 |
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class MyActivty extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (googleMap == null) { googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); UiSettings mapSettings; mapSettings = googleMap.getUiSettings(); mapSettings.setZoomControlsEnabled(true); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } // check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(), R.string.sorry_unable_to_create_maps, Toast.LENGTH_SHORT).show(); } } } |
2.3 Adding Marker
We can create markers on the map via the Marker
class. This class can be highly customized. On GoogleMap
we can register a listener for the markers in our map via thesetOnMarkerClickListener(OnMarkerClickListener)
method. The OnMarkerClickListener
class defines theonMarkerClicked(Marker)
method which is called if a marker is clicked.
1 2 |
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude1, longitute1)).title(address); Marker m=googleMap.addMarker(marker); |
2.4 Zoom Camera to view the Marker
1 2 3 4 |
// updating the camera view CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 6f); // Finally move the map: googleMap.moveCamera(cu); |
Stay updated to know more about Maps in Android application !