Android Google Maps Example Tutorial

Total Views : 1,336
Zoom In Zoom Out Read Later Print

In this tutorial we’ll discuss and implement some features of android google maps API in application. Please make sure that you’ve been through the Android Google Maps Setup before reading this because we are not covering setup as it covered before.

You can read Android Google Maps Setup from this URL.

1.Create activity_main.xml and add Add the map fragment to attach it.
2. To get hold of the GoogleMap object in our MainActivity class we need to implement the OnMapReadyCallback interface and override the onMapReady callback method in our MainActivity.

Map Type

if you want you can change map type using GoogleMap object.

setMapType()

googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Map Zoom & Rotation

Use following line of code to enable or disable map zoom and rotaation.

Zoom & Rotation

googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
Adding Markers

Add a marker for a location.

addMarker()

googleMap.addMarker(new MarkerOptions()
.position(new LatLng(28.457523, 77.026344))
.title("Gurugram")
.snippet("Gurugram(Hr.)"));

googleMap.addMarker(new MarkerOptions()
.position(new LatLng(28.644800, 77.216721.))
.title("Delhi")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
Animating Camara Movement

Animating the movement of camera to a specific point use animateCamera() method or you can use moveCamera() if you don't need animation.

animateCamera()

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(28.644800, 77.216721),15));

GoogleMap methods

1. snipt(String s) : it is used to display data over the marker when tapped.
2. addCircle(CircleOptions options) : This method is used to add circle on a map.
3. addPolygon(PolygonOptions options) : This method is used to add polygon on the map.
4. addTileOverlay(TileOverlayOptions options) : Add tile overlay on the map.
5. getMyLocation() : return currently displayed user location.
6. setTrafficEnabled(boolean enabled) : Toggles the traffic layer.
7. snapshot(GoogleMap.SnapshotReadyCallback callback) : Take a snapshot of the map.
8. stopAnimation() : If camera animation is in process, stop it.
9. clear() : Remove everything from the map.

Example Code

MainActivity.java

package com.prodevsblog.googlemap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
SupportMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
init();
}
void init(){

findViewById(R.id.changeType).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

LatLng gurugram = new LatLng(28.457523, 77.026344);
googleMap.addMarker(new MarkerOptions()
.position(gurugram)
.title("Gurugram")
.snippet("Gurugram(Hr.)"));

googleMap.addMarker(new MarkerOptions()
.position(new LatLng(28.644800, 77.216721))
.title("Delhi")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

mMap.moveCamera(CameraUpdateFactory.newLatLng(gurugram));

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(28.644800, 77.216721), 1));
}
});
}
});
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in India, and move the camera.
LatLng gurugram = new LatLng(28.457523, 77.026344);
googleMap.addMarker(new MarkerOptions()
.position(gurugram)
.title("Gurugram")
.snippet("Gurugram(Hr.)"));

googleMap.addMarker(new MarkerOptions()
.position(new LatLng(28.644800, 77.216721))
.title("Delhi")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

mMap.moveCamera(CameraUpdateFactory.newLatLng(gurugram));
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Map Type"
android:id="@+id/changeType"/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@+id/changeType"/>
</RelativeLayout>

See More

Latest Photos