Android - Google Maps integration

From XennisWiki
Jump to: navigation, search

The easiest way to integrate Google Maps in an (native) Android application is to use the official Google Maps Android API (in this article version 2).

Getting started

  • Setup Google Play Services SDK
    • Use the SDK Manager to install the Google Play Service SDK (Extras > Google Play services) and a compatible version of the Google APIs platform (Android x.x (API xx) > Google APIs)
    • Reference the Google Play service library to your Android project
  • Fellow the Getting Started guide

Example - Marker and circle

MyMapActivity.java

[...]
public class MyMapActivity extends FragmentActivity implements OnMyLocationChangeListener, OnCameraChangeListener {
	
    /**
     * Note that this may be null if the Google Play services APK is not available.
     */
    private GoogleMap mMap;

    [...]

    /**
     * Set up the map with its configurations and markers, circles etc.
     */
    private void setUpMap() {
    	inks = new LinkedList<Ink>();
    	
    	mMap.setMyLocationEnabled(true);
	mMap.setIndoorEnabled(true);
    	mMap.setOnMyLocationChangeListener(this);
    	mMap.setOnCameraChangeListener(this);

        this.addCicleMarker(new LatLng(59.942724, 10.717987), 50, "HelloWorld", "This is a sample message");
        this.addCicleMarker(new LatLng(59.944554, 10.716855), 40, "Hello Android", "Another sample message");
    }

   /**
     * Zooms the map to the current location.
     * 
     * @param zoom Zoom level
     */
    private void zoomMapToCurrentLocation(float zoom) {
    	try {
		mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(getMyLocationLatLng(), zoom));
	} catch (Exception e) {
		e.printStackTrace();
	}    	
    }
    
    /**
     * Get current location as LatLng object.
     * 
     * @return Current location as LatLng object
     * @throws Exception When current location is null
     */
    private LatLng getMyLocationLatLng() throws Exception {
    	Location myLocation = mMap.getMyLocation();
    	if (myLocation != null) {
        	return new LatLng(myLocation.getLatitude(), myLocation.getLongitude());    		
    	} else {
    		throw new Exception("Current location is null.");
    	}
    }
    
    /**
     * Add an CicleMarker (combination of a Marker and Circle object) to the map.
     * 
     * @param position center of the CicleMarker.
     * @param radiusInMeters radius of the CicleMarker
     * @param title title of the CicleMarker
     * @param message message of the CicleMarker
     */
    private void addCicleMarker(LatLng position, double radiusInMeters, String title, String message) {
	mMap.addCircle(new CircleOptions()
		.center(position)
		.radius(radiusInMeters)
		.strokeColor(Color.GRAY)
		.strokeWidth(2)
		.fillColor(0x30000000));   	
    	mMap.addMarker(new MarkerOptions()
		.position(position)
		.title(title)
		.snippet(message)
		.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    }

    @Override
    public void onMyLocationChange(Location location) {
    	Log.d("Map", "location lat: " + location.getLatitude() + ", lng:" + location.getLongitude());
    }

    @Override
    public void onCameraChange(CameraPosition camera) {
    	Log.d("Map", "camera zoom: " + camera.zoom);		
    }

}

See also

External links