Using Overlays

Google Maps provides a facility that allows you to place custom data on top of the map. You can see an example of this if you search for pizza restaurants in your area: Google Maps places pushpins, or balloon markers, to indicate each location. The way Google Maps provides this facility is by allowing you to add a layer on top of the map. Android provides several classes that help you to add layers to a map. The key class for this type of functionality is Overlay, but you can use an extension of this class called ItemizedOverlay. Listing 7-15 shows an example.

Listing 7-15. Marking Up a Map Using ItemizedOverlay import java.util.ArrayList; import java.util.List;

import android.graphics.Canvas;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import com.google.android.maps.OverlayItem;

public class MappingOverlayActivity extends MapActivity { private MapView mapView;

^Override protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState);

setContentView(R.layout.mapview);

mapView = (MapView) findViewByld(R.id.mapview);

LinearLayout layout = (LinearLayout) findViewByld(R.id.zoomCtrls); layout.addView(mapView.getZoomControls());

mapView.setClickable(true);

Drawable marker=getResources().getDrawable(R.drawable.mapmarker); marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

mapView.getOverlays().add(new InterestingLocations(marker));

^Override protected boolean isRouteDisplayed() { return false;

class InterestingLocations extends ItemizedOverlay {

private List<OverlayItem> locations = new ArrayList<OverlayItem>(); private Drawable marker;

public InterestingLocations(Drawable marker) {

super(marker); this.marker=marker; // create locations of interest GeoPoint disneyMagicKingdom = new GeoPoint((int)(28.4l8971*1000000),(int)(-8l.58l436*1000000));

GeoPoint disneySevenLagoon = new GeoPoint((int)(28.410067*1000000),(int)(-8l.583699*1000000));

locations.add(new OverlayItem(disneyMagicKingdom , "Magic Kingdom", "Magic Kingdom"));

locations.add(new OverlayItem(disneySevenLagoon , "Seven Lagoon", "Seven Lagoon"));

populate();

^Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow);

boundCenterBottom(marker);

^Override protected OverlayItem createItem(int i) { return locations.get(i);

^Override public int return

Listing 7-15 demonstrates how you can overlay markers onto a map. The example places two markers (see Figure 7-8): one at Disney's Magic Kingdom, and another one at Disney's Seven Seas Lagoon (both near Orlando, Florida).

In order for you to add markers onto a map, you have to create and add an extension of com.google.android.maps.Overlay to the map. The Overlay class itself cannot be instantiated, so you'll have to extend it or use one of the extensions. In our example, we have implemented InterestingLocations, which extends ItemizedOverlay, which in turn extends Overlay. The Overlay class defines the contract for an overlay, and ItemizedOverlay is a handy implementation that makes it easy for you to create a list of locations that can be marked on a map.

The general usage pattern is to extend the ItemizedOverlay class and add your "items"— interesting locations—in the constructor. After you instantiate your points of interest, you call the populate() method of ItemizedOverlay. The populate() method is a utility that caches the OverlayItem(s). Internally, the class calls the size() method to determine the number of overlay items, and then enters a loop, calling createItem(i) for each item. In the createItem method, you return the already created item given the index in the array.

As you can see from Listing 7-15, you simply create the points and call populate() to show markers on a map. The Overlay contract manages the rest. To make it all work, the onCreate() method of the activity creates the InterestingLocations instance, passing in the Drawable that's used for the markers. Then onCreate()adds the InterestingLocations instance to the overlay collection (mapView.getOverlays().add()).

Another interesting aspect of Listing 7-15 is the creation of the OverlayItem(s). In order to create an OverlayItem, you need an object of type GeoPoint. The GeoPoint class represents a location by its latitude and longitude, in micro degrees. In our example, we obtained the latitude and longitude of Magic Kingdom and Seven Seas Lagoon using geocoding sites on the web. (As you'll see shortly, you can use geocoding to convert an address to a latitude/longitude pair, for example.) We then converted the latitude and longitude to micro degrees (because the APIs operate on micro degrees) by multiplying by 1,000,000 and then performing a cast to an integer.

locations.size();

Figure 7-8. MapView with markers

All in all, you'll agree that placing markers on a map couldn't be easier. Or could it? We don't have a database of latitude/longitude pairs, but we're guessing that we'll need to somehow create one or more GeoPoints using a real address. That's when you can use the Geocoder, which is part of the location package that we'll discuss next.

0 0

Responses

  • zewdi asmara
    How to add new overlay on 3d model in android opengl?
    6 years ago

Post a comment