Copyright © 2013 CommonsWare, LLC
MapsV2...
AndYou!
AnDevCon Boston 2013
Copyright © 2013 CommonsWare, LLC
Maps: Not Invented in 2008!
● Android 1.0: Maps SDK Add-On
– Same as what Google Maps app used
– Creaky API, particularly with fragments
● The Fork
– Google Maps started using own engine
– Gained more features than old SDK add-on
supported
Copyright © 2013 CommonsWare, LLC
Maps: Not Invented In 2012 Either!
● MapsV2
– Introduced in late 2012
– New API, new device requirements, new features
● Close to full Google Maps app
● MapsV1: Deprecated!
– Can no longer official get MapsV1 API keys
– Still runs, if you have keys from before
Copyright © 2013 CommonsWare, LLC
Legal, Without Ease
● MapsV1 and MapsV2:Terms of Service
– Required to use these APIs
– Put restrictions on how you use those APIs
● Example: no real-time turn-by-turn navigation
– IANALNDIPOOTV
● Please have qualified legal counsel review these
terms if they concern you
Copyright © 2013 CommonsWare, LLC
Users Have Issues (No, Really!)
● Requirements
– Play Services Framework
● May already exist
● May be available from the Play Store
● Not available by other legitimate means
– OpenGL ES 2.0
● On 99.8% of devices with the Play Store
Copyright © 2013 CommonsWare, LLC
You Have IssuesToo
● No Emulator Support
– Play Services Framework not available in
emulator images
● Google has indicated this will be resolved...
sometime
– Various workarounds of dubious legality
documented on teh Interwebs
● Expectation:Test on Hardware
Copyright © 2013 CommonsWare, LLC
Preparation M
● Setting Up MapsV2 Development
– Download Google Play Services SDK from SDK
Manager (in Extras area)
– Result: Android Library Project
● Eclipse: import into workspace, then attach to
application project
● Command-line: android update project
● Other IDEs: ???
Copyright © 2013 CommonsWare, LLC
Yet More Preparation M
● MapsV2 API Key
– No, it's not the same as your MapsV1 API key
– Obtain it from the Google API Console
– Tied to your package name, signing key
● SHA-1 fingerprint vs. MD5
● Up to ~5 signing key/package name pairs per API key
Copyright © 2013 CommonsWare, LLC
Let's Get Mappy, People
● A MapsV2 Project
– ...does not need to use a Google APIs SDK as a
build target
– …does need to restrict to OpenGL ES 2.0 devices
● Or make optional, but test at runtime
– ...does need permissions, including a custom one
– ...does need a MapsV2 API key
Copyright © 2013 CommonsWare, LLC
Let's Get Mappy, People
● A MapsV2 Activity
– ...does not have to inherit from any magic
activity class
– ...does need to use MapFragment,
SupportMapFragment, or MapView
– ...does have a lot of legwork to ensure MapsV2 is
available, the user has access to license terms,
etc.
● ...all of which can be nicely abstracted away
Copyright © 2013 CommonsWare, LLC
Working (with a map) For a Living
● GoogleMap
– Your gateway to the actual mapping API
– Obtained from MapFragment,
SupportMapFragment, or MapView via
getMap()
– Most method calls result in IPC
● Comparatively slow
● Avoid doing lots of these in a tight loop
● Must be called on the main application thread
Copyright © 2013 CommonsWare, LLC
Showing Something Useful
● CameraUpdate
– Changes in characteristics of the “camera”
● Center, zoom, etc.
– Create using CameraUpdateFactory
– Apply to GoogleMap
● moveCamera()
● animateCamera()
Copyright © 2013 CommonsWare, LLC
Typing Up a Map
● Default MapType: MAP_TYPE_NORMAL
● Changing MapTypes
– Choose a type (e.g., MAP_TYPE_HYBRID)
– Call setMapType() on your GoogleMap
Copyright © 2013 CommonsWare, LLC
Marking Up a Map
● No More Overlays!
● Add MarkersVia addMarker()
– Takes a MarkerOptions object
– Fluent API to describe marker
– Position as a LatLng
● No more microdegrees!
– Provide title and snippet for default pop-up
“info window”
Copyright © 2013 CommonsWare, LLC
BYO Info
● Controlling the InfoWindows
– Implement InfoWindowAdapter interface
● getInfoContents(): your ownView to pour into
MapsV2-supplied frame
● getInfoWindow(): your ownView with your own
frame
– Associate with GoogleMap via
setInfoWindowAdapter()
Copyright © 2013 CommonsWare, LLC
Taps, Sans Bugle
● MarkerTaps
– Implement OnMarkerClickListener interface
● onMarkerClick() called when user taps on a
Marker
– Associate with GoogleMap via
setOnMarkerClickListener()
Copyright © 2013 CommonsWare, LLC
Markers Can Be a Real Drag
● Marker Drag-and-Drop
– Call draggable(true) on MarkerOptions
– Implement OnMarkerDragListener interface
● onMarkerDragStart()
● onMarkerDrag()
● onMarkerDragEnd()
– Associate with GoogleMap via
setOnMarkerDragListener()
Copyright © 2013 CommonsWare, LLC
WhereYou At?
● Enabling “My Location”
– setMyLocationEnabled(true) on
GoogleMap
● Adds “My Location” button
● Requires suitable permissions (e.g.,
ACCESS_FINE_LOCATION)
● When tapped, camera follows the user
– OnMyLocationChangeListener and
setOnMyLocationChangeListener()
Copyright © 2013 CommonsWare, LLC
Pages of Maps
● MapFragment andViewPager
– It just works!
● Problem:ViewPagerWants Gestures
– Default: cannot pan map horizontally
– Solution: CustomViewPager subclass, overriding
canScroll(), to indicate widgets that handle
their own scrolling
Copyright © 2013 CommonsWare, LLC
Dirty Details, Done Dirt Cheap
● AbstractMapActivity Features
– Determining if Play Services Framework is
available
– Leading user to install Play Services Framework
if possible
– Showing license terms
Copyright © 2013 CommonsWare, LLC
Other Fun Stuff
● Supplying own location data
● Polylines, polygons, and circles
● Controlling default gestures
● TileOverlay and GroundOverlay
– Images displayed on top of map tiles
● ...and more!
Copyright © 2013 CommonsWare, LLC
Do Worry, Be Mappy
● Problems and Issues
– Reported leaks in configuration changes
– GLSurfaceView, animations, and black bars
– Play Services Framework detection buggy
– IPC and performance
– OpenGL ES 2.0 requirement
Copyright © 2013 CommonsWare, LLC
Speaker Contact
Information
Source Code

Maps V2... And You!

  • 1.
    Copyright © 2013CommonsWare, LLC MapsV2... AndYou! AnDevCon Boston 2013
  • 2.
    Copyright © 2013CommonsWare, LLC Maps: Not Invented in 2008! ● Android 1.0: Maps SDK Add-On – Same as what Google Maps app used – Creaky API, particularly with fragments ● The Fork – Google Maps started using own engine – Gained more features than old SDK add-on supported
  • 3.
    Copyright © 2013CommonsWare, LLC Maps: Not Invented In 2012 Either! ● MapsV2 – Introduced in late 2012 – New API, new device requirements, new features ● Close to full Google Maps app ● MapsV1: Deprecated! – Can no longer official get MapsV1 API keys – Still runs, if you have keys from before
  • 4.
    Copyright © 2013CommonsWare, LLC Legal, Without Ease ● MapsV1 and MapsV2:Terms of Service – Required to use these APIs – Put restrictions on how you use those APIs ● Example: no real-time turn-by-turn navigation – IANALNDIPOOTV ● Please have qualified legal counsel review these terms if they concern you
  • 5.
    Copyright © 2013CommonsWare, LLC Users Have Issues (No, Really!) ● Requirements – Play Services Framework ● May already exist ● May be available from the Play Store ● Not available by other legitimate means – OpenGL ES 2.0 ● On 99.8% of devices with the Play Store
  • 6.
    Copyright © 2013CommonsWare, LLC You Have IssuesToo ● No Emulator Support – Play Services Framework not available in emulator images ● Google has indicated this will be resolved... sometime – Various workarounds of dubious legality documented on teh Interwebs ● Expectation:Test on Hardware
  • 7.
    Copyright © 2013CommonsWare, LLC Preparation M ● Setting Up MapsV2 Development – Download Google Play Services SDK from SDK Manager (in Extras area) – Result: Android Library Project ● Eclipse: import into workspace, then attach to application project ● Command-line: android update project ● Other IDEs: ???
  • 8.
    Copyright © 2013CommonsWare, LLC Yet More Preparation M ● MapsV2 API Key – No, it's not the same as your MapsV1 API key – Obtain it from the Google API Console – Tied to your package name, signing key ● SHA-1 fingerprint vs. MD5 ● Up to ~5 signing key/package name pairs per API key
  • 9.
    Copyright © 2013CommonsWare, LLC Let's Get Mappy, People ● A MapsV2 Project – ...does not need to use a Google APIs SDK as a build target – …does need to restrict to OpenGL ES 2.0 devices ● Or make optional, but test at runtime – ...does need permissions, including a custom one – ...does need a MapsV2 API key
  • 10.
    Copyright © 2013CommonsWare, LLC Let's Get Mappy, People ● A MapsV2 Activity – ...does not have to inherit from any magic activity class – ...does need to use MapFragment, SupportMapFragment, or MapView – ...does have a lot of legwork to ensure MapsV2 is available, the user has access to license terms, etc. ● ...all of which can be nicely abstracted away
  • 11.
    Copyright © 2013CommonsWare, LLC Working (with a map) For a Living ● GoogleMap – Your gateway to the actual mapping API – Obtained from MapFragment, SupportMapFragment, or MapView via getMap() – Most method calls result in IPC ● Comparatively slow ● Avoid doing lots of these in a tight loop ● Must be called on the main application thread
  • 12.
    Copyright © 2013CommonsWare, LLC Showing Something Useful ● CameraUpdate – Changes in characteristics of the “camera” ● Center, zoom, etc. – Create using CameraUpdateFactory – Apply to GoogleMap ● moveCamera() ● animateCamera()
  • 13.
    Copyright © 2013CommonsWare, LLC Typing Up a Map ● Default MapType: MAP_TYPE_NORMAL ● Changing MapTypes – Choose a type (e.g., MAP_TYPE_HYBRID) – Call setMapType() on your GoogleMap
  • 14.
    Copyright © 2013CommonsWare, LLC Marking Up a Map ● No More Overlays! ● Add MarkersVia addMarker() – Takes a MarkerOptions object – Fluent API to describe marker – Position as a LatLng ● No more microdegrees! – Provide title and snippet for default pop-up “info window”
  • 15.
    Copyright © 2013CommonsWare, LLC BYO Info ● Controlling the InfoWindows – Implement InfoWindowAdapter interface ● getInfoContents(): your ownView to pour into MapsV2-supplied frame ● getInfoWindow(): your ownView with your own frame – Associate with GoogleMap via setInfoWindowAdapter()
  • 16.
    Copyright © 2013CommonsWare, LLC Taps, Sans Bugle ● MarkerTaps – Implement OnMarkerClickListener interface ● onMarkerClick() called when user taps on a Marker – Associate with GoogleMap via setOnMarkerClickListener()
  • 17.
    Copyright © 2013CommonsWare, LLC Markers Can Be a Real Drag ● Marker Drag-and-Drop – Call draggable(true) on MarkerOptions – Implement OnMarkerDragListener interface ● onMarkerDragStart() ● onMarkerDrag() ● onMarkerDragEnd() – Associate with GoogleMap via setOnMarkerDragListener()
  • 18.
    Copyright © 2013CommonsWare, LLC WhereYou At? ● Enabling “My Location” – setMyLocationEnabled(true) on GoogleMap ● Adds “My Location” button ● Requires suitable permissions (e.g., ACCESS_FINE_LOCATION) ● When tapped, camera follows the user – OnMyLocationChangeListener and setOnMyLocationChangeListener()
  • 19.
    Copyright © 2013CommonsWare, LLC Pages of Maps ● MapFragment andViewPager – It just works! ● Problem:ViewPagerWants Gestures – Default: cannot pan map horizontally – Solution: CustomViewPager subclass, overriding canScroll(), to indicate widgets that handle their own scrolling
  • 20.
    Copyright © 2013CommonsWare, LLC Dirty Details, Done Dirt Cheap ● AbstractMapActivity Features – Determining if Play Services Framework is available – Leading user to install Play Services Framework if possible – Showing license terms
  • 21.
    Copyright © 2013CommonsWare, LLC Other Fun Stuff ● Supplying own location data ● Polylines, polygons, and circles ● Controlling default gestures ● TileOverlay and GroundOverlay – Images displayed on top of map tiles ● ...and more!
  • 22.
    Copyright © 2013CommonsWare, LLC Do Worry, Be Mappy ● Problems and Issues – Reported leaks in configuration changes – GLSurfaceView, animations, and black bars – Play Services Framework detection buggy – IPC and performance – OpenGL ES 2.0 requirement
  • 23.
    Copyright © 2013CommonsWare, LLC Speaker Contact Information Source Code