In this tutorial we’ll discuss and implement some interesting features of android google maps API in our application. Before we get onto the discussion. Please make sure that you’ve been through the Android Google Maps Setup. It’s a prerequisite.

Android Google Maps API Overview

In this tutorial we’ll implement a few interesting features provided by the Android Google Maps API.

Features include map markers, map types, camera animations and a few more.

Add the map fragment in the content_main.xml layout as we had done in the previous tutorial.

This attaches the MapFragment to our MainActivity.

To get hold of the GoogleMap object in our MainActivity class we need to implement the OnMapReadyCallback interface and override the onMapReady callback method.

Setting Google Map Type

Using the google map object we can change the map type too. There are four different types of map and each give different view of the map. These types are Normal, Hybrid, Satellite and Terrain. We can use them as given below.

Google Map Zoom, Rotation

We can enable/disable map zoom and rotations using the following lines of codes:

Some other customization methods available in the GoogleMap class are given below.

  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

Adding Markers on the Google Map

snippet() is used to display more data over the marker when it’s tapped.

Animating or moving the camera to a specific point is performed using the following snippet:

In the above code 16 is the zoom level number. The map zooms in and centers onto the defined LatLng object.

Note: The LatLng object is instantiated and passed with the latitude and longitude double values.

Android Google Maps Example Project Structure


Android Google Maps Example Code

The is defined as below:

We call getMapAsync() on the SupportMapFragment object to register the callback. The FloatingActionButton invokes a new OnMapReadyCallBack method with a different map type.

The content_main.xml contains the MapFragment as shown below:

The output of the android google maps example in action is shown below.
android google maps example app

This brings an end to this tutorial. You can download the final Android Google Maps Example project from the below link and replace the YOUR_API_KEY with the your own google maps api key.

By admin

Leave a Reply