maps dr. david janzen except as otherwise noted, the content of this presentation is licensed under...
TRANSCRIPT
![Page 1: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/1.jpg)
Maps
Dr. David Janzen
Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
![Page 2: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/2.jpg)
Maps
• Google Maps API is widely used on the web
• The Android SDK provides support for easily integrating the Google Maps API
![Page 3: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/3.jpg)
Using Google Maps in our apps
• Configure– Maps require the Google API as the project
build target– Maps require a Map API Key in order to be
deployed– http://code.google.com/android/add-ons/
google-apis/maps-overview.html
• Code– Create a MapView in a MapActivity– Create Map Overlays
![Page 4: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/4.jpg)
Add Google API in Eclipsehttp://developer.android.com/sdk/adding-components.html
![Page 5: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/5.jpg)
Add Google API in Eclipse• Use API 4 for SDK 1.6
http://developer.android.com/guide/appendix/api-levels.html
![Page 6: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/6.jpg)
Add Google API in Eclipse• Set the Google API as the Project Build Target
– Right-click on the project, select Properties
![Page 7: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/7.jpg)
Keys
• As we learned in lab 1 section 6, https://sites.google.com/site/androidappcourse/labs/lab-1
our apps must be signed in order to deploy them on a device
• Eclipse automatically creates a signed debug keystore that is used when launching our app from Eclipse
• In order to deploy our app to the public, we must create a signed keystoreSee http://developer.android.com/guide/publishing/app-
signing.html#ExportWizard
![Page 8: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/8.jpg)
Find your keystorehttp://code.google.com/android/add-ons/google-apis/mapkey.html
![Page 9: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/9.jpg)
Find your debug keystorehttp://code.google.com/android/add-ons/google-apis/mapkey.html
![Page 10: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/10.jpg)
Get your certificate fingerprinthttp://code.google.com/android/add-ons/google-apis/mapkey.html
![Page 11: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/11.jpg)
Register your certificate with Googlehttp://code.google.com/android/maps-api-signup.html
![Page 12: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/12.jpg)
What’s in the legal agreement?
• Read the Terms of Service (sections 1-11)– http://code.google.com/android/maps-api-
signup.html
• Examples– Maps may include ads in future– Google may limit number of transactions– Cannot use for turn-by-turn directions or
autonomous driving
![Page 13: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/13.jpg)
Add the Map API Key to your Application
<com.google.android.maps.MapViewandroid:id="@+id/myMap"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="@string/mapApiKey"/>
![Page 14: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/14.jpg)
Configure AndroidManifest.xml
<application android:name="MyApplication" > <uses-library android:name="com.google.android.maps" /> ... </application>
![Page 15: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/15.jpg)
Finally, we can start coding
• MapView – Contains a map
• via Google Maps API
• Map tile retrieval and caching is all done for you
– Includes pan– Includes zoom
• use setBuiltInZoomControls(true);
![Page 16: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/16.jpg)
MapView Modes
• MapView – You determine mode
• setSatellite(true);
• setTraffic(true);
• setStreetView(true);
![Page 17: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/17.jpg)
MapActivity
• MapView can only be constructed or inflated in a MapActivity
public class MyActivity extends MapActivity {… @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); … MapView myMap = (MapView)findViewById(R.id.myMap); myMap.setBuiltInZoomControls(); myMap.setSatellite(true);
![Page 18: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/18.jpg)
MapController
• You can pan and zoom the map programmatically
MapView myMap = (MapView)findViewById(R.id.myMap); MapController mapController = myMap.getController(); mapController.setZoom(1); //widest zoom/far away … mapController.setZoom(21); //narrowest zoom/close in mapController.zoomIn(); //one level mapController.zoomOut(); //one level
![Page 19: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/19.jpg)
GeoPoint
• You can move to a particular point
MapView myMap = (MapView)findViewById(R.id.myMap); MapController mapController = myMap.getController();
Double lat = 37.123456 * 1E6; Double long = -122.123456 * 1E6; GeoPoint point = new GeoPoint(lat.intValue(), long.intValue()); mapController.setCenter(point); //jump to point … mapController.animateTo(point); //smooth transition to point
![Page 20: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/20.jpg)
Reverse Geocoding
• Find address from longitude/latitude
location = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER); double lat = location.getLatitude(); double lng = location.getLongitude(); Geocoder gc = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; try { addresses = gc.getFromLocation(lat, lng, 10); } catch (IOException e) {}
![Page 21: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/21.jpg)
Forward Geocoding
• Find longitude/latitude (and more) from address
Geocoder gc = new Geocoder(this, Locale.US); List<Address> addresses = null; try { addresses = gc.getFromLocationName( “123 Main St., Newton, Kansas”, 10); } catch (IOException e) {} double lat = addresses.get(0).getLatitude(); String zip = addresses.get(0).getPostalCode();
![Page 22: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/22.jpg)
Geolocation
• Options– GPS, cell network– Wifi-based
• Skyhook Wireless
• http://www.skyhookwireless.com/developers/Android_Integration_Manual.php
![Page 23: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/23.jpg)
Setting up location servicespublic MyActivity() { criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW);};private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); } public void onProviderDisabled(String provider) { updateWithNewLocation(null); } public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {}};
![Page 24: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/24.jpg)
@Overrideprotected void onStart() { super.onStart(); locationManager = (LocationManager)getSystemService( Context.LOCATION_SERVICE); provider = locationManager.getBestProvider(criteria, true); // or could be LocationManager.GPS_PROVIDER try {
updateWithNewLocation(locationManager.getLastKnownLocation( provider));
} catch (Exception e) {} locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);}private void updateWithNewLocation(Location location) { double latitude = 0.0; double longitude = 0.0; if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); //do something with latitude and longitude (e.g. print or move map there)}
![Page 25: Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License](https://reader035.vdocuments.net/reader035/viewer/2022062408/56649f355503460f94c536c6/html5/thumbnails/25.jpg)
Turn GPS on and off to save battery
@Overrideprotected void onPause() {
super.onPause();//stop receiving GPS
locationManager.removeUpdates(locationListener);}@Overrideprotected void onResume() {
super.onResume();//restart receiving GPS
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}