Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading…
Transcript

Android Bootcamp Week 6

Playing with Maps

Mary-Anne Cosgrove

Senior Developer Consultant

mcosgro@thoughtworks.com

https://github.com/ThoughtWorksAustralia/AndroidBootcampProject#week-6-google-apis

Last week:

  • We learned about communicating with servers

  • We retrieved a set of Treasures and a list of High Scores from our own server

This week:

  • We will find out how to use Google Play Services and the Google Maps API

  • We will add the treasure map, zoom to our current location, and map our attempts.

  • We will complete the game, then map the actual Treasure locations

Retrospective...

We hope you enjoyed the series!

Thanks for

coming!

  • What did we do well?
  • What could we do better?
  • What puzzles you?

Exercise 1:

Setting up to use Play Services

Install the SDK

Not hard, but there are quite a few steps

Read the instructions at

https://developers.google.com/maps/documentation/android/start#installing_the_google_maps_android_v2_api

carefully

THIS CAUSES THE GRADLE CHECK TO FAIL. Robolectric issue 1025 https://github.com/robolectric/robolectric/issues/1025

Summary

  • Generate an API key so you can use Play Services
  • Connect to Play Services and draw your map onConnected
  • Use LocationClient to get your current location
  • Use CameraUpdate with CameraUpdateFactory to pan or zoom your map
  • Use MarkerOptions and addMarker() to add markers
  • Follow the resources links on Github to explore further on your own

Exercise 2: Displaying the Map

2a: Add the map to our fragment

  • To display a map we need a Google MapFragment. Rename our MapFragment to TreasureMapFragment to avoid confusion

  • There is a <fragment> tag you can use in a layout xml.

  • But now try moving from Map page to Treasure List page and back...

Do this onConnected

2b: You can't have nested fragments in layout xmls

2c: Move the map to our current location

  • Create it programmatically using MapFragment.newInstance() and keep it in an instance variable

  • Add it to the containing fragment with a ChildFragmentManager transaction

  • Use MapFragment.getMap() to obtain a GoogleMap. Keep that too

  • Increase the minSDKVersion to 17 or use SupportMapFragment instead of MapFragment

Exercise 3:

Display Markers for the Attempts

Exercise 4:

Complete the Game

  • Use LocationClient to get our current location

  • We have to connect to it onStart and disconnect onStop

  • TreasureMapFragment can implement GooglePlayServicesClient.ConnectionCallbacks and .OnConnectionFailedListener

  • Check GooglePlayServicesUtil

.isGooglePlayServicesAvailable() before using the map

  • Now we can zoom to our location

Map the Treasures as well as the Attempts if the Game has ended

  • Add "Finish the Game" button to High Scores fragment.

  • Set its onClick to endGame

  • Improve the styling, if you wish

  • Implement endGame in HelloAndroid activity

And to round it off,

send your Score to the server!

Google Play Services

  • Add a Gson Converter to the Treasure Service that converts names to lower case with underscores - http://square.github.io/retrofit/

  • Add a POST to /players in the TreasureService interface

  • Send the score to the server. Use a Retrofit Callback to handle the response asynchronously.

  • If successful, refresh the high scores by calling showHighScores()

  • Log an error if unsuccessful

While you

were sleeping...

Let's Explore!

Game model and its unit tests have been expanded...

  • When you take a photo, an Attempt is recorded for that Treasure

  • You get feedback on how far your Attempt is from the Treasure

  • Attempts and Treasures implement

Locatable so they can be mapped

  • Game calculates your Score

  • You can use image thumbnails as map markers

  • You can cluster markers as the map zooms out

  • You can set the map zoom to include all markers in view

  • You can draw a walking route between markers

design by Dóri Sirály for Prezi

The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Learn more about creating dynamic, engaging presentations with Prezi