Google Maps
Android allows us to integrate Google Maps into our application. You can show any place on the map, or show different routes on the map e.t.c. You can customize the map according to your choice
Adding Google Maps
Google provides this feature using the Google Play services library where you have to download it externally. After downloading, you have to integrate it with your project. Finally you need to integrate your app with Google through Google console. This topic is fully discussed
Google Map - Activity file
Google offers GoogleMap and MapFragment APIs to integrate maps into your Android application. To use GoogleMap, you must create an object of GoogleMap and get the reference of the map from the XML layout file. Its syntax is given below -
GoogleMap googleMap;
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Google Map - Layout file
Now you have to add the map fragment into xml layout file. Its syntax is given below −
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Google Map - AndroidManifest file
The next thing you need to do is to add some permissions along with the Google Map API key in the AndroidManifest.XML file. Its syntax is given below −
android:name="android.permission.ACCESS_NETWORK_STATE" />
android:name="android.permission.INTERNET" />
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDKymeBXNeiFWY5jRUejv6zItpmr2MVyQ0" />
Customizing Google Map
You can easily customize google map from its default view , and change it according to your demand.
Adding Marker
You can place a maker with some text over it displaying your location on the map. It can be done by via addMarker() method. Its syntax is given below −
final LatLng TutorialsPoint = new LatLng(21 , 57);
Marker TP = googleMap.addMarker(new MarkerOptions().position(TutorialsPoint).title("TutorialsPoint"));
Channing Map Type
You can also change the type of the MAP. There are four different types of map and each give different view of the map. These types are Normal,Hybrid,Satellite and terrain. You can use them as below
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
Enable/Disable zoom
You can also enable or disable the zoom gestures in the map by calling the setZoomControlsEnabled(boolean) method. Its syntax is given below −
googleMap.getUiSettings().setZoomGesturesEnabled(true);
Apart from these customization, there are other methods available in the GoogleMap class , that helps you more customize the map. They are listed below −
Sr.No | Method & description |
---|---|
1 | addCircle(CircleOptions options)
This method add a circle to the map |
2 | addPolygon(PolygonOptions options)
This method add a polygon to the map |
3 | addTileOverlay(TileOverlayOptions options)
This method add tile overlay to the map |
4 | animateCamera(CameraUpdate update)
This method Moves the map according to the update with an animation |
5 | clear()
This method removes everything from the map. |
6 | getMyLocation()
This method returns the currently displayed user location. |
7 | moveCamera(CameraUpdate update)
This method repositions the camera according to the instructions defined in the update |
8 | setTrafficEnabled(boolean enabled)
This method Toggles the traffic layer on or off. |
9 | snapshot(GoogleMap.SnapshotReadyCallback callback)
This method Takes a snapshot of the map |
10 | stopAnimation()
This method stops the camera animation if there is one in progress |