Android provides facility to integrate Google map in our application. Google map displays your current location, navigate location direction, search location etc. We can also customize Google map according to our requirement.
Mar 04, 2019 Google Maps is one of the many applications bundled with the Android platform. In Addition, to simply using the Maps application, you can also embed it into your own applications and make it do some very cool things. This section describes how to use Google Maps in your Android applications and programmatically perform the following:. How to generate a Google Maps API key. Starting June 2016, Google requires you to use an API key in order to place Google Maps on your site. The key is free and allows you free usage of up to 25,000 times.
There are four different types of Google maps, as well as an optional to no map at all. Each of them gives different view on map. These maps are as follow:
Google map API provides several methods that help to customize Google map. These methods are as following:
Methods | Description |
---|---|
addCircle(CircleOptions options) | This method add circle to map. |
addPolygon(PolygonOptions options) | This method add polygon to map. |
addTileOverlay(TileOverlayOptions options) | This method add tile overlay to the map. |
animateCamera(CameraUpdate update) | This method moves the map according to the update with an animation. |
clear() | This method removes everything from the map. |
getMyLocation() | This method returns the currently displayed user location. |
moveCamera(CameraUpdate update) | This method reposition the camera according to the instructions defined in the update. |
setTrafficEnabled(boolean enabled) | This method set the traffic layer on or off. |
snapshot(GoogleMap.SnapshotReadyCallback callback) | This method takes a snapshot of the map. |
stopAnimation() | This method stops the camera animation if there is any progress. |
Let's create an example of Google map integrating within our app. For doing this we select Google Maps Activity.
Copy the URL from google_map_api.xml file to generate Google map key.
Paste the copied URL at the browser. It will open the following page.
Click on Create API key to generate API key.
After clicking on Create API key, it will generate our API key displaying the following screen.
Copy this generated API key in our google_map_api.xml file
To get the GoogleMap object in our MapsActivity.java class we need to implement the OnMapReadyCallback interface and override the onMapReady() callback method.
Add the following user-permission in AndroidManifest.xml file.
Add the following dependencies in build.gradel file.
Output
To use the Maps Embed API you must have an API key. The API key is a unique identifier that isused to authenticate requests associated with your project for usage and billing purposes.
You must have at least one API key associated with your project.
To get an API key:
You must include an API key with every Maps Embed API request.In the following example, replace YOUR_API_KEY
withyour API key.
For more information about MODE
and parameters
options in the code above, see Forming the URL for the Maps Embed API.
We strongly recommend that you restrict your API key. Restrictions provide added security and helpensure only authorized requests are made with your API key. There are two restrictions. You shouldset both:
Note: If you need to call web, web service, and/or mobile APIs from the same (client-side) app, create and restrict multiple keys.
To restrict an API key: