问题
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.
回答1:
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.
回答2:
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.
回答3:
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);
回答4:
I was also stuck in the same situation and I wanted to number the pins insted of A,B,C..
The good News is that I found the solution. I only created a TextView with a Pin background and inserted a number into it dynamically and converted that into Drawable.
You can create different TextView with pin colour background that you want.
Here is the link to my question which I have answered. :)
回答5:
I used this code:
OverlayItem crtItem = new OverlayItem( getPoint( Double.parseDouble(latCoordRow),Double.parseDouble(longCoordRow) ), nameRow , addressRow );
Drawable crtMarker = markerIconsArray.get(categoryRow); //my current drawable (from a HashMap)
crtItem.setMarker(crtMarker); // set new marker
crtMarker.setBounds(0, 0, crtMarker.getIntrinsicWidth(),crtMarker.getIntrinsicHeight()); //setBounds
boundCenterBottom(crtMarker); //correct shadow problem
If you will not setBounds your drawable won't show.
来源:https://stackoverflow.com/questions/2648482/different-named-markers-on-google-android-map