1 / 32

Lecture 13 Mobile Programming

Lecture 13 Mobile Programming. Google Maps Android API. Agenda. Generating SHA1 Fingerprint Signing up for API Key (as developer) Permissions MapFragment and GoogleMap object Layers MyLocation. Google Maps API Key.

arlais
Télécharger la présentation

Lecture 13 Mobile Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 13 Mobile Programming Google Maps Android API

  2. Agenda • Generating SHA1 Fingerprint • Signing up for API Key (as developer) • Permissions • MapFragment and GoogleMap object • Layers • MyLocation

  3. Google Maps API Key • In order to use the Google Maps API, you first need to obtain an API key and configure your app • GoogleMap API v1 is obsolete!! • Now, you better use GoogleMap API v2. • https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw • Please read page 863 – 970 in your textbook!

  4. Getting the SHA1 Fingerprint • To register for a Maps API Key, you need to provide an SHA1 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 (SHA1): • Go Google API Consoleand sign up for the Android Maps API • Save the generated API key, you will use it in your app.

  5. Use (Support) WebFragment • 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.MapFragment” or SupportMapFragment depending on the version of your phone.

  6. Use (Support) WebFragment • In the java file: • SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView) • GoogleMap map = mapGrag.getMap(); • CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(x, y), where x and y are double/float. • map.moveCamera(center) • CameraUpdate zoom = CameraUpdateFactory.zoomTo(15). • map.animateCamera(zoom)

  7. Add Marker or OverlayItem • Create Marker object • MarkerOptionsmo = new MarkerOptions().position(LatLng).title(“text”).snippet(“text”).draggable(true); • Add a marker to the map object. • Map.addMarker(mo);

  8. Customized Info Window • Create a new class that implements InfoWindowAdapter • Class PopupAdapter implements InfoWindowAdapter{ LayoutInflater inflater; PopupAdapter(LayoutInflater inflater){ this.inflater = inflater; } @Override public View getInfoContents(Marker marker){ }

  9. Customized Info Window • @Override public View getInfoContents(Marker marker){ View popup = inflater.inflate(R.layout.popup, null); TextView tv = (TextView) popup.findViewById(); tv.setText(Marker.getTitle); return popup; } @Override Public View getInfoWindow(Marker marker){ return null;}

  10. Customized Info Window • In java file: • map.setInfoWindowAdapter(new PopuoAdapter(getLayoutInflater()));

  11. Find My Location • In java file: • map.setMyLocationEnabled(true); • You must call map.setOnMyLocationChangeListener(Listener) • The listener must override the method: • Public void onMyLocationChange(Location lastKnownLocation)

  12. Map Listeners • setOnCameraChangeListener(this) • implement OnCameraChangeListener • Override public void onCameraChange(CameraPosition position) • setMyLocationChangeListener(this): • implement OnMyLocationChangeListener • Override public void onMyLocationChange(Location lastKnownLocation) • If you set marker.draggable(true) • Implement OnMarkerDragListener • Override three methods: • onMarkerDrag(Marker maker) • onMarkerDragEnd(Marker marker) • onMarkerDragStart(Marker marker)

  13. 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

  14. Control • You can enable and disable those gestures by: • setRotateGesturesEnabled() • setScrollGesturesEnabled() (for panning the map) • setTiltGesturesEnabled() • setZoomControlsEnabled() (for the + and - buttons) • setZoomGesturesEnabled() (for pinch-to-zoom) • SetCompassEnable()

  15. JSON Values • "title" : "IT Business Analyst Intern" • "organization" : "Medtronic" • "city" : "Brooklyn Park"

  16. JSON Arrays "details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]

  17. 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 bla" , "drank some soda" , "hit manager in face with pie"] }

  18. Parsing JSON JSONObject json = new JSONObject(raw); final String moviePlot = "" + json.getString("Plot"); mTextMoviePlot.post(new Runnable() { @Override public void run() { mTextMoviePlot.setText(moviePlot); } });

  19. Parsing JSON JSONObject json = new JSONObject(raw); final String moviePlot = "" + json.getString("Plot"); mTextMoviePlot.post(new Runnable() { @Override public void run() { mTextMoviePlot.setText(moviePlot); } }); JSONObject is a set of name/value mappings, can represent a JSON document

  20. Parsing JSON JSONObject json = new JSONObject(raw); final String moviePlot = "" + json.getString("Plot"); mTextMoviePlot.post(new Runnable() { @Override public void run() { mTextMoviePlot.setText(moviePlot); } }); Retrieve the plot of the movie

  21. Parsing JSON 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

  22. 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 bla" , "drank some soda" , "hit manager in face with pie"] }

  23. 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 bla" , "drank some soda" , "hit manager in face with pie"] } This is a JSONObject

  24. 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 bla" , "drank some soda" , "hit manager in face with pie"] } You can get title by calling getString("title") on the JSONObject

  25. 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 bla" , "drank some soda" , "hit manager in face with pie"] } You can get organization by calling getString("organization") on the JSONObject

  26. 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 bla" , "drank some soda" , "hit manager in face with pie"] } Etcetera, etcetera ...

  27. 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 bla" , "drank some soda" , "hit manager in face with pie"] } This however, is not a String, but an array. Get this by calling getJSONArray() on the JSONObject

  28. 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 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

  29. 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 bla" , "drank some soda" , "hit manager in face with pie"] }

  30. Parsing JSON See HttpGetJsonExample.tar

  31. FACEBOOK API See FacebookApiExample.tar See FacebookHello.tar

  32. References • The Busy Coder's Guide to Android Development - Mark Murphy • Android Developers • The Mobile Lab at Florida State University

More Related