views:

903

answers:

3

Hi,

i want to add many different markers on an android map. My code works good so far with the same overlay over and over again:

mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new MyItemizedOverlay(drawable);
OverlayItem overlayItem = new OverlayItem(geoPoint, "foo", "bar");
mapOverlays.add(itemizedOverlay);

This works fine so far. But every marker is the same. What I want to do now is having different markers on the map like the ones you see on Google Maps Webapp (a marker named "A", the next one "B", and so on). How can I achieve this? Do I have to add an extra png marker file to my app ? (marker_a.png, marker_b.png,...) or is there a simpler way to achieve this? It could also be that there will be more than 26 results so that i possibly need different colours of the markers.

A: 

Yes, you need another png, so it would look like this:

mapOverlays = mapView.getOverlays();

// All "A"s
drawable = this.getResources().getDrawable(R.drawable.marker_a);
itemizedOverlay = new MyItemizedOverlay(drawable);
OverlayItem overlayItem = new OverlayItem(geoPoint, "foo", "bar");
mapOverlays.add(itemizedOverlay);

// All "B"s
drawable = this.getResources().getDrawable(R.drawable.marker_b);
itemizedOverlay = new MyItemizedOverlay(drawable);
OverlayItem overlayItem = new OverlayItem(geoPoint, "foo", "bar");
mapOverlays.add(itemizedOverlay);
CaseyB
+2  A: 

Here is a sample project showing several different PNGs on a single ItemizedOverlay. You just have to override some of the drawing methods to handle the different PNGs.

CommonsWare
+2  A: 

One of the answers provides the solution with different ItemizedOverlay for each marker group. You can achieve the same with single ItemizedOverlay by calling overlayItem.setMarker(drawable)

If you are going to load your markers from resources, don't forget to call:

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

before you call setMarker. Otherwise markers will not be shown.

Since markers are type Drawable you can obtain them like any other Drawable, including creating them run-time.

Viktor Bresan