Mobile Application Development Google Maps Android API...

15
Mobile Application Development Google Maps Android API v2 Waterford Institute of Technology November 5, 2014 John Fitzgerald Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 1/15

Transcript of Mobile Application Development Google Maps Android API...

Page 1: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Mobile Application DevelopmentGoogle Maps Android API v2

Waterford Institute of Technology

November 5, 2014

John Fitzgerald

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 1/15

Page 2: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2API features

Google Maps Android API• Embed & display map• Access Google Map servers• Download map data• Add markers, polygons,

overlays• Change zoom level• Determine geolocation• Select map type (normal,

hybrid . . . )

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 2/15

Page 3: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2Preparatory work

Google Play Services• Install via Android SDK Manager• Import to MyRent workspace• Reference in manifest file• Reference in MyRent properties

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 3/15

Page 4: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2Maps API key

Generating & using key• Each app requires key• Key obtainable at Google API console• Must be registered user• Generate key• Add key to manifest• Switch on API v2 in console

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 4/15

Page 5: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2Geolocation

LatLng stores map data• Units are degrees• Ranges:

• Longitude: 0 to +/- 180• Latitude: 0 to +/- 90

• One degree (metres)• latitude approx 111• longitude same at equator• zero at poles

• Accuracy decimal places:• Four: 11 m (Garmin 15

m)• Six: 11 cm• Seven: 11 mm

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 5/15

Page 6: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2Helpers

Data input and manipulation• Geolocation data input as String• Necessary convert to & from Android LatLng

public static LatLng latLng(Context context, String geolocation){

String[] g = geolocation.split(",");return new LatLng(Double.parseDouble(g[0]), Double.parseDouble(g[1]));

}

public static String latLng(LatLng geo){

return String.format("%.6f", geo.latitude) + ", "+ String.format("%.6f", % geo.longitude);

}

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 6/15

Page 7: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2Modify manifest file

• Add permissions

• Add API key

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 7/15

Page 8: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

Fields added to ResidenceFragment

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 8/15

Page 9: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

Initialize map fragment in onActivityCreated

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 9/15

Page 10: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

Implement interfaces

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 10/15

Page 11: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

OnMarkerDragListener• Three methods to implement• Fully implement only onMarkerDragEnd

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 11/15

Page 12: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

OnCameraChangeListener• Triggered by pan, zoom . . .

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 12/15

Page 13: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

Initialize Map when fragment starts• Invoke in onStart• Register marker & camera listeners• Set map type (e.g. Hybrid, Terrain)• Display map

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 13/15

Page 14: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

Update Map• When geolocation changed manually (afterTextchanged)• Boolean flag used to distinguish manual geolocation changes

and those caused by dragging marker.

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 14/15

Page 15: Mobile Application Development Google Maps Android API v2edeleastar.github.io/android-app-dev/topic08/talk-4/ba-android_maps.… · Google Maps Android API v2 ResidenceFragment Initialize

Google Maps Android API v2ResidenceFragment

Geolodation data input• Validation introduced in MapHelper.latlng• Disallows invalid geolocation• try-catch block used

Waterford Institute of Technology, Mobile Application Development Google Maps Android API v2 15/15