Android allows you to integrate Google Maps into your app. Google Maps displays any location on a map, can show different routes, navigate location, directions, search locations on the map, etc. You can also customize Google Maps according to your requirements.
In Android, there are four different types of Google Maps, all of which have different views. The maps are as follows:
The normal map displays a typical road map, natural features like rivers, and some features built by humans.
Syntax of normal type of map:
1
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
The hybrid type of map shows the satellite photograph data with typical road maps and also displays road and feature labels.
Syntax of hybrid type of map:
1
googleMap.setMapType(GoogleMap.MAP_TYPE_Hybrid);
The satellite map displays satellite photograph data but does not show road and feature labels.
Syntax of satellite type of map:
1
googleMap.setMapType(GoogleMap.MAP_TYPE_Satellite);
The terrain map displays photographic data with colors, contour lines, labels, and perspective shading.
Syntax of terrain type of map:
1
googleMap.setMapType(GoogleMap.MAP_TYPE_Terrain);
This type of map displays an empty grid with no tiles loaded.
Google Map API provides methods to easily customize Google Maps from its default view and change it according to your needs.
These methods are as follows:
addMarker(markerOptions options)
: This method is used to place a marker with some text over it displaying your location on the map.
addCircle(CircleOptions options)
: You can add a circle to the map by calling the addCircle() method.
addPolygon(PolygonOptions options)
: You can add a polygon to the map by using the addPolygon() method.
addTileOverlay(TileOverlayOptions options)
: Using this method you can add tile overlay to the map.
animateCamera(CameraUpdate update)
: This method is used to move the map according to the update with an animation.
clear()
: This method is used to clear your map. The clear() method removes everything from the map.
getMyLocation()
: This method is used to return the currently displayed user location.
moveCamera(CameraUpdate update)
: This method is used to reposition the camera according to the instructions defined in the update.
setTrafficEnabled(boolean enabled)
: This method is used to toggle the traffic layer on or off.
setZoomControlsEnabled(boolean)
: This method is used to enable or disable the zoom gestures in the map.
snapshot(GoogleMap.SnapshotReadyCallback callback)
: This method is used to take a snapshot of the map.
stopAnimation()
: This method is used to stop the camera animation if there is one in progress.
First, you have to create a project with google maps activity.
The next step is to copy the URL from the google_map_api_xml file to generate the Google map key.
Paste the copied URL into your browser. It will give the following screen:
Click on Create API key to generate the API key.
After clicking on Create API key, it will generate your API key displaying the following page.
Copy this generated API key in your google_map_api.xml file
build.gradel file:
You need to add the following dependencies.
1
implementation 'com.google.android.gms:play-services-maps:11.8.0'
AndroidManifest file:
You need to add some permission along with the Google Map API key in the AndroidManifest file.
1 2 3 4 5 6 7 8 9 10 11
<!--Permissions--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> < uses - permission android: name = "android.permission.INTERNET" / > <uses-permission android:name="com.google.android.providers.gsf.permission. READ_GSERVICES" /> < uses - permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" / > <!--Google MAP API key--> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/google_maps_key" />
activity_maps.xml
1 2 3 4 5 6 7 8
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps. SupportMapFragment" tools:context="com.example.googlemaps.MapsActivity"/>
MapsActivity.java
In the MapsActivity.java class you need to implement the OnMapReadyCallback interface and override the onMapReady() callback method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Package com.example.googlemaps;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng australia = new LatLng(21, 57);
mMap.addMarker(new MarkerOptions()
.position(australia)
.title("Marker in Australia"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(australia));
}
}
Output: