Add Google Maps to your Android App

You can find the corresponding udacity course here.

Table of Contents

  1. Play services
  2. Maps
  3. Markers
  4. StreetView

Play services

To use them add to the build.gradle:

compile 'com.google.android.gms:play-services:10.2.1'

Then add to the Manifest:

<meta-data 
	android:name="com.google.gms.version"
	android:value="@integer/google_play_services_version"/>
<uses-permission
	android:name="android.permission.ACCESS_FINE_LOCATION"/>

Create GoogleApiClient in onCreate()

Connect GoogleApiClient in onStart() to Location Service (or other relevant API)

Overwrite onConnectionFailed() and onConnectionSuspend() in Location Service

Write onConnected() and create a LocationRequest that queries the Location Service

Write onLocationChanged() and get the Location object which you can then use

Maps

Generate a SHA 1 Key from the terminal by typing:

cd ~/.android

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

Go to developers console. Create a project. Activate Maps API. Generate an API Key for Andorid App and enter your SHA 1 and packagename.

Maps fragment

Create an xml fragment with

android:name="com.google.android.gms.maps.MapFragment"

Set attributes on the map fragment:

xmlns:map="http://schemas.android.com/apk/res-auto"
map:cameraBearing="112.5"
map:cameraTargetLat="40.7484"
map:cameraTargetLng="-73.9857"
map:cameraTilt="65"
map:cameraZoom="17"

Google maps object

Allows you to change your map while the app is running.

Look here

Camera Position Options

Use moveCamera() instantaniously move to a target. Use animateCamera() to go to a target with a fly over effect.

Markers

MarkerOptions myPlace = new MarkerOptions()
	.position(new LatLng(47.489805, -122.120502))
	.title("My Place")

⚠ Making an initial invisble marker visible is faster than creating a new marker => Great way to implement place filtering! (Ex.: Hide or show Hotels with specific cost levels)

Show or Hide markers info window programatically with marker.showInfoWindow() or marker.hindeInfoWindow()

Create an InfoWindowAdapter to fully customize the appereance of an marker’s info window.

Implement .setOnMarkerClickListener() to kick off other actions when clicking on the marker.

Implement .onInfoWindowClick() to kick off other actions when showing an info window on a marker click.

Custom Markers

Add icon property to MarkerOptions with .icon(BitmapDescriptionFactory.fromResource(R.drawable.ic_launcher));

Polylines

Creates a trace for all the LatLng you add to the Polyline with:

PolylineOptions().geodesic(true).add(LatLng);

Get to the initial one back at the end to get a Polygone.

Circles

In onMapReady() add:

m_map.addCircle(new CircleOptions()
     .center(new LatLng(47.489805, -122.120502))
     .radius(5000) // = 5 km
     .strokeColor(Color.GREEN)
     .fillColor(Color.argb(64,0,255,0)));

StreetView

By default these 3 options are enabled, manage them with the following, if you like:

User interaction: