Recipe 7.5 Adding Markers to the Map

Android Versions
Level 1 and above
Permissions
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
Source Code to Download from Wrox.com
Map-Overlays.zip

You want to add markers to the map indicating the current location, or locations of interest.

Solution

In the previous recipe, you learned how to make the Google Maps navigate to a particular location. Doing so pans the map, with the location you are navigating to be displayed at the center. A much more useful visual cue to the user would be a marker (commonly known as the pushpin) on the map so that the location is immediately clear to the user. The following sections show you the various ways to add this.

Solution Part 1: Displaying a Marker at the Current Location

One common request from developers is the capability to display the current location of the user without resorting to using the LocationManager class as described in Chapter 8, “Location-Based Services,” Recipe 8.1.

To do so, you can use the MyLocationOverlay class. All you need is to do is create an instance of this class and then add it to the overlay lists of the MapView.

NOTE An overlay in MapView is a layer that can be added on top of the map. An overlay can contain bitmaps, drawings, etc.

The following code snippet shows how to display the current location on the map using the MyLocationOverlay class:

package net.learn2develop.maps; import ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.