Slide 1

Slide 1 text

from using the Google Maps API V2 for Android Learnings

Slide 2

Slide 2 text

Benjamin Weiss http://gplus.to/keyboardsurfer Twitter: @keyboardsurfer Senior Software Developer at ImmobilienScout24 Organizer: GDG Android in Berlin Co-Organizer of many Android events

Slide 3

Slide 3 text

Agenda ● A look back ● Old VS New ● Getting started with Maps V2 ● Caveats and Pitfalls

Slide 4

Slide 4 text

You've come a long way

Slide 5

Slide 5 text

Comparison

Slide 6

Slide 6 text

Tiles Vector graphics Load paths and render them locally Bitmaps Load whole bitmaps and display them locally

Slide 7

Slide 7 text

Fragmentation MapFragment ● Display multiple maps ● Embeddable MapView ● One Map at a time ● Map VS other content

Slide 8

Slide 8 text

UI Enhancement Direct interaction 1. Create Markers 2. Add them to the Map Overlay 1. Create an Overlay 2. Add it to the Map 3. Create Markers 4. Add to Overlay

Slide 9

Slide 9 text

Getting started

Slide 10

Slide 10 text

Getting started https://code.google.com/apis/console

Slide 11

Slide 11 text

Getting started Obtain API keys keytool -list -v -keystore $name -alias $name SHA1: f0:00:... keystoreSHA1 + ';' + packageName

Slide 12

Slide 12 text

Add info to your AndroidManifest.xml Getting started

Slide 13

Slide 13 text

Add info to your AndroidManifest.xml Getting started

Slide 14

Slide 14 text

Getting started https://developers.google.com/ maps/documentation/android/start

Slide 15

Slide 15 text

Pitfalls

Slide 16

Slide 16 text

I can haz maps?

Slide 17

Slide 17 text

I can haz maps? GooglePlayServicesUtil. isGooglePlayServicesAvailable(activity);

Slide 18

Slide 18 text

You can haz maps ConnectionResult.SUCCESS

Slide 19

Slide 19 text

What have you done? ConnectionResult. DEVELOPER_ERROR

Slide 20

Slide 20 text

Not yet can haz ConnectionResult. RESOLUTION_REQUIRED

Slide 21

Slide 21 text

Not yet can haz ConnectionResult. startResolutionForResult(...)

Slide 22

Slide 22 text

No can haz! ConnectionResult. SERVICE_MISSING

Slide 23

Slide 23 text

No can haz! ConnectionResult. SERVICE_DISABLED

Slide 24

Slide 24 text

No can haz! ConnectionResult. getErrorDialog(...)

Slide 25

Slide 25 text

State management java.lang.IllegalStateException: Uninitialized at maps.m.y.c(Unknown Source) at maps.z.a.b(Unknown Source) at maps.z.b.m(Unknown Source) at maps.z.b.a(Unknown Source) at maps.s.c.a(Unknown Source) at maps.ab.a.a(Unknown Source) at maps.ab.ax.a(Unknown Source) at maps.as.aw.a(Unknown Source) at maps.w.g.a(Unknown Source) at maps.w.g.b(Unknown Source) at maps.q.p.l(Unknown Source) at maps.q.p.run(Unknown Source)

Slide 26

Slide 26 text

private void setUpMap() { GoogleMap googleMap = getMap(); if (null != googleMap) { onMapInitialized(googleMap); return; } else { try { MapsInitializer.initialize(getActivity()); } catch (GooglePlayServicesNotAvailableException e) { return; //no play services -> no map for your user } new MapInitTask().execute(); } } Setting up the map

Slide 27

Slide 27 text

final class MapInitTask extends AsyncTask { @Override protected Void doInBackground(Void... aVoid) { try { Thread.sleep(125); } catch (InterruptedException e) { //no-op } return null; } ... } The waiting game

Slide 28

Slide 28 text

final class MapInitTask extends AsyncTask { ... @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if (null != MyMapFragment.this.getMap()) { //return the map via a listener } else { new MapInitTask().execute(); } } } Are we there yet?

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Map Markers map.addMarker( new MarkerOptions(...));

Slide 31

Slide 31 text

Map Markers .position(LatLng) .title(String) .snippet(String)

Slide 32

Slide 32 text

Map Markers No simple way to set an ID / TAG

Slide 33

Slide 33 text

InfoWindows class MyInfoWindowAdapter implements GoogleMap.InfoWindowAdapter { ... @Override public View getInfoContents(Marker marker) { //inflate view, get title and snippet MyData data = displayedData.get(Integer.valueOf(marker.getTitle())); title.setText(data.getTitle()); snippet.setText(data.getSnippet()); return view; } }

Slide 34

Slide 34 text

Memory management java.lang.OutOfMemoryError: at maps.bl.b.a(Unknown Source) at maps.bl.b.a(Unknown Source) at maps.bl.b.a(Unknown Source) at maps.aq.a.a(Unknown Source) at maps.ar.m.b(Unknown Source) at maps.ar.m.a(Unknown Source) at maps.ar.n.a(Unknown Source) at maps.ar.b.run(Unknown Source)

Slide 35

Slide 35 text

Memory management BitmapDescriptorFactory.from...

Slide 36

Slide 36 text

Don't blink

Slide 37

Slide 37 text

No more blinking mapHost.requestTransparentRegion(mapHost);

Slide 38

Slide 38 text

Issue tracker https://code.google.com/p/ gmaps-api-issues/issues

Slide 39

Slide 39 text

Aftermath What you should keep in mind

Slide 40

Slide 40 text

It has some rough edges

Slide 41

Slide 41 text

It evolves

Slide 42

Slide 42 text

And improves

Slide 43

Slide 43 text

Aftermath Use it!

Slide 44

Slide 44 text

Image Sources Google Maps for Android (http://goo.gl/ulb3T) Old VS New (http://goo.gl/Hciq6) Getting Started (http://goo.gl/RmYR2) Pitfall (http://goo.gl/fQdZJ) Kittens (http://goo.gl/ectcy) Markers (http://goo.gl/So3kI) Weeping Angel (http://goo.gl/XS8tm) Rough Edges (http://goo.gl/e2Odq) Evolution (http://goo.gl/kWMIU) Improvment (http://goo.gl/gnixN)

Slide 45

Slide 45 text

Questions Benjamin Weiss http://gplus.to/keyboardsurfer @keyboardsurfer