Launching a Map Application by Using an Intent

Location applications such as the Maps application handle the ACTION_VIEW intent when supplied with a URI with geographic coordinates. This URI has a special format.

When you have determined the latitude and longitude of a location, you can launch the Maps application (or any application that handles this type of data), using the following URI format string: geo:lat,lon

Here's an example:

String geoURI = String.format("geo:%f,%f", lat, lon);

This special URI can also include a zoom level, which is a number between 1 and 23, where zoom level 1 shows the whole planet, and zoom level 23 zooms in all the way (often way too far for map resolution). To include a zoom level, use the following URI format string: geo:lat,lon?z=level

Here's an example:

String geoURI = String.format("geo:%f,%f?z=10", lat, lon);

When you have a properly formatted URI, you can use the parse() method to generate the Uri and use it with the ACTION_VIEW intent, as follows: Uri geo = Uri.parse(geoURI);

Intent geoMap = new Intent(Intent.ACTION_VIEW, geo); startActivity(geoMap);

If there are applications on the device that handle geo-format URIs, the appropriate application (for example, the Google Maps application) will launch as the new foreground activity and show the location. After the user has looked at the map, he or she can return to the calling application by pressing the Back button.

By using the Geocoder class and an intent to launch the Google Maps application, you can complete the favorite place picker dialog, including the Map It! Button control, as shown in Figure 14.10.

FIGURE 14.10

The favorite place picker dialog, launching the Google Maps application.

If you press the Menu button in the Maps application, you can change the map mode to satellite and zoom in to see the Great Pyramids, as well as all the tour buses and the Sphinx, as shown in Figure 14.11.

FIGURE 14.11

Using the Google Maps application to zoom in satellite view.

0 0

Post a comment