Android Google Map

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.

Types of Google Maps

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:

  1. Normal: This type of map displays typical road map, natural features like river and some features build by humans.
  2. Hybrid: This type of map displays satellite photograph data with typical road maps. It also displays road and feature labels.
  3. Satellite: Satellite type displays satellite photograph data, but doesn’t display road and feature labels.
  4. Terrain: This type displays photographic data. This includes colors, contour lines and labels and perspective shading.
  5. None: This type displays an empty grid with no tiles loaded.

Syntax of different types of map

  1. googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  2. googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  3. googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
  4. googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

Methods of Google map

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.

Example of Google Map

Let’s create an example of Google map integrating within our app. For doing this we select Google Maps Activity.

android Google Map 1

Copy the URL from google_map_api.xml file to generate Google map key.

android Google Map 2

Paste the copied URL at the browser. It will open the following page.

android Google Map 3

Click on Create API key to generate API key.

android Google Map 4

After clicking on Create API key, it will generate our API key displaying the following screen.

android Google Map 5

Copy this generated API key in our google_map_api.xml file

android Google Map 6

activity_maps.xml


MapsActivity.java

To get the GoogleMap object in our MapsActivity.java class we need to implement the OnMapReadyCallback interface and override the onMapReady() callback method.

package example.com.mapexample;  
  
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);  
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.  
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()  
                .findFragmentById(R.id.map);  
        mapFragment.getMapAsync(this);  
  
    }  
  
    @Override  
    public void onMapReady(GoogleMap googleMap) {  
        mMap = googleMap;  
  
        // Add a marker in Sydney and move the camera  
        LatLng sydney = new LatLng(-34, 151);  
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));  
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));  
  
    }  
}

Required Permission

Add the following user-permission in AndroidManifest.xml file.

   
  
   
  

AndroidManifest.xml

  
  
      
      
      
      
  
      
  
          
  
          
              
                  
  
                  
              
          
      
  
  
build.gradel
Add the following dependencies in build.gradel file.

dependencies {  
    implementation fileTree(dir: 'libs', include: ['*.jar'])  
    implementation 'com.android.support:appcompat-v7:26.1.0'  
    implementation 'com.google.android.gms:play-services-maps:11.8.0'  
    testImplementation 'junit:junit:4.12'  
    androidTestImplementation 'com.android.support.test:runner:1.0.1'  
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'  
}

build.gradel

Add the following dependencies in build.gradel file.

dependencies {  
    implementation fileTree(dir: 'libs', include: ['*.jar'])  
    implementation 'com.android.support:appcompat-v7:26.1.0'  
    implementation 'com.google.android.gms:play-services-maps:11.8.0'  
    testImplementation 'junit:junit:4.12'  
    androidTestImplementation 'com.android.support.test:runner:1.0.1'  
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'  
}

Output

android Google Map 7