Lecture 13 Mobile Programming Google Maps Android API
Lecture 13 Mobile Programming Google Maps Android API
Agenda • Generating SHA 1 Fingerprint • Signing up for API Key (as developer) • Permissions • Map. Fragment and Google. Map object • Layers
Google Maps API Key • In order to use the Google Maps API, you first need to obtain an API key and configure your app • Google. Map API v 1 is obsolete!! • Now, you better use Google. Map API v 2. o https: //docs. google. com/document/pub? id=19 n. Qzv. KPCVLd 7_Vrpwn. Hfl-AE 9 fjb. Jy. Sow. ONZZt. NHzw o Please read page 863 – 970 in your textbook!
Getting the SHA 1 Fingerprint • • • To register for a Maps API Key, you need to provide an SHA 1 fingerprint of the certificate that you will use to sign your application Navigate to C: Documents and Settings<User>. android Run the following command keytool -list –v –keystore debug. keystore -alias androiddebugkey -storepass android -keypass android Copy the text that comes after Certificate fingerprint (SHA 1): Go Google API Console and sign up for the Android Maps API Save the generated API key, you will use it in your app.
Use (Support) Web. Fragment • In the layout file: • • • You need define a <fragment> Give it an id, so you can access it in source code. Class=“com. google. android. gms. maps. Map. Fragment ” or Support. Map. Fragment depending on the version of your phone.
Use (Support) Web. Fragment • • In the java file: Support. Map. Fragment map. Frag = (Support. Map. Fragment) get. Support. Fragment. Manager(). find. Fragment. By. Id(R. id. map. View) Google. Map map = map. Grag. get. Map(); Camera. Update center = Camera. Update. Factory. new. Lat. Lng(new Lat. Lng(x, y), where x and y are double/float. map. move. Camera(center) Camera. Update zoom = Camera. Update. Factory. zoom. To(15). map. animate. Camera(zoom)
Add Marker or Overlay. Item • • Create Marker object • Marker. Options mo = new Marker. Options(). position(Lat. Lng). title(“text”). snippet( “text”). draggable(true); Add a marker to the map object. • Map. add. Marker(mo);
Customized Info Window • • Create a new class that implements Info. Window. Adapter Class Popup. Adapter implements Info. Window. Adapter{ Layout. Inflater inflater; Popup. Adapter(Layout. Inflater inflater){ this. inflater = inflater; } @Override public View get. Info. Contents(Marker marker){ }
Customized Info Window • @Override public View get. Info. Contents(Marker marker){ View popup = inflater. inflate(R. layout. popup, null); Text. View tv = (Text. View) popup. find. View. By. Id(); tv. set. Text(Marker. get. Title); return popup; } @Override Public View get. Info. Window(Marker marker){ return null; }
Customized Info Window • In java file: • map. set. Info. Window. Adapter(new Popuo. Adapter(get. Layout. Inflater()));
Find My Location • In java file: • • • map. set. My. Location. Enabled(true); You must call map. set. On. My. Location. Change. Listener(Listener) The listener must override the method: Public void on. My. Location. Change(Location last. Known. Location) •
Map Listeners • set. On. Camera. Change. Listener(this) o o • Override public void on. Camera. Change(Camera. Position position) set. My. Location. Change. Listener(this): o o • implement On. Camera. Change. Listener implement On. My. Location. Change. Listener Override public void on. My. Location. Change(Location last. Known. Location) If you set marker. draggable(true) o o Implement On. Marker. Drag. Listener Override three methods: § § § on. Marker. Drag(Marker maker) on. Marker. Drag. End(Marker marker) on. Marker. Drag. Start(Marker marker)
Guesture • • The user can change zoom level either by + and - buttons or via “pinch-to zoom” gestures The user can change the center of the map via simple swipe gestures The user can change the camera tilt via two-finger vertical swipes, so instead of a traditional top-down perspective, the user can see things on an angle The user can change the orientation of the map via a two-finger rotating swipe, to change the typical “north is to the top of the map” to some other orientation
Control • You can enable and disable those gestures by: o o o set. Rotate. Gestures. Enabled() set. Scroll. Gestures. Enabled() (for panning the map) set. Tilt. Gestures. Enabled() set. Zoom. Controls. Enabled() (for the + and - buttons) set. Zoom. Gestures. Enabled() (for pinch-to-zoom) Set. Compass. Enable()
JSON Values • • • "title" : "IT Business Analyst Intern" "organization" : "Medtronic" "city" : "Brooklyn Park"
JSON Arrays "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"]
JSON Objects { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/10" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] }
Parsing JSONObject json = new JSONObject(raw); final String movie. Plot = "" + json. get. String("Plot"); m. Text. Movie. Plot. post(new Runnable() { @Override public void run() { m. Text. Movie. Plot. set. Text(movie. Plot); } });
Parsing JSONObject json = new JSONObject(raw); JSONObject is a set of name/value mappings, can represent a JSON document final String movie. Plot = "" + json. get. String("Plot"); m. Text. Movie. Plot. post(new Runnable() { @Override public void run() { m. Text. Movie. Plot. set. Text(movie. Plot); } });
Parsing JSONObject json = new JSONObject(raw); final String movie. Plot = "" + json. get. String("Plot"); m. Text. Movie. Plot. post(new Runnable() { Retrieve the plot of the movie @Override public void run() { m. Text. Movie. Plot. set. Text(movie. Plot); } });
Parsing JSON is not always this simple however, but it's usually straightforward once you understand JSON A JSONObject may consist of more JSONObjects, JSONArrays, Strings, Booleans, Integers, etc
Parsing JSON { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] }
Parsing JSON This is a JSONObject { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] }
Parsing JSON { "title" : "IT Business Analyst Intern" , You can get title by "organization" : "Medtronic" , calling get. String("title") on the JSONObject "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] }
Parsing JSON { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , You can get organization by calling get. String("organization") on the JSONObject "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] }
Parsing JSON { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , Etcetera, etcetera. . . "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] }
Parsing JSON { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] } This however, is not a String, but an array. Get this by calling get. JSONArray() on the JSONObject
Parsing JSON { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] } After which you can use the getters on the JSONArray to get the desired data
Parsing JSON How would you represent this data using XML? { "title" : "IT Business Analyst Intern" , "organization" : "Medtronic" , "city" : "Brooklyn Park" , "state" : "MN" , "start_date" : "05/10" , "end_date" : "07/11" , "details" : [ "bla bla" , "drank some soda" , "hit manager in face with pie"] }
Parsing JSON See Http. Get. Json. Example. tar
FACEBOOK API See Facebook. Api. Example. tar See Facebook. Hello. tar
References • The Busy Coder's Guide to Android Development - Mark Murphy • Android Developers • The Mobile Lab at Florida State University
- Slides: 32