This chapter took a whirlwind tour of the MapView object, which displays Google Maps in your Android application. You have learned the various ways in which the map can be manipulated, and you have also learned how you can obtain geographical location data using the various network providers: GPS, Cellular triangulation, or Wi-Fi triangulation.
EXERCISES
1. If you have embedded the Google Maps API into your Android application but it does not show the map when the application is loaded, what could be the likely reasons?
2. What is the difference between geocoding and reverse geocoding?
3. Name the two location providers that you can use to obtain your location data.
4. What is the method for monitoring a location?
Answers to the Exercises can be found in Appendix C.
WHAT YOU LEARNED IN THIS CHAPTER
TOPIC | KEY CONCEPTS |
Displaying the MapView |
<com.google.android.maps.MapView android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="<your_key_here>" /> |
Referencing the Map library |
<uses-library android:name="com.google.android.maps" /> |
Displaying the zoom controls |
mapView.setBuiltInZoomControls(true);
|
Programmatically zooming in or out of the map |
mc.zoomIn(); mc.zoomOut(); |
Changing views |
mapView.setSatellite(true); mapView.setTraffic(true); |
Animating to a particular location |
mc = mapView.getController(); String coordinates[] = {"1.352566007", "103.78921587"}; double lat = Double.parseDouble(coordinates[0]); double lng = Double.parseDouble(coordinates[1]); p = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6)); mc.animateTo(p); |
Adding markers |
Implement an Overlay class and override the draw() method
|
Getting the location of the map touched |
GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); |
Geocoding and reverse geocoding | Use the Geocoder class |
Obtaining location data |
private LocationManager lm; //... lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = new MyLocationListener(); lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationListener); //... private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { if (loc != null) { } } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { }} |
Monitoring a location |
lm.addProximityAlert(37.422006, -122.084095, 5, -1, pendIntent);
|