1

I am displaying markers on a map. I am not sure how I can specify a different drawable resource for different markers?

I would like to show a green pin if locations distance < 50, etc. etc.

pin = getResources().getDrawable(R.drawable.pushpin);
        itemizedOverlay = new MyItemizedOverlay(pin, mapView);

        for (Record element : list) {
            GeoPoint point;
            OverlayItem overlayItem;

            double lat = Double.parseDouble(element.getLatitude());
            double lng = Double.parseDouble(element.getLongitude());
            double locationDistance = Double.parseDouble(element.getLocationDist());

            geoPoint.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)));
            listOfOverlays = mapView.getOverlays();
            point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            Log.i("deep", "deep    " + point);

            overlayItem = new OverlayItem(point, "", element.getTitle());
            if(locationDistance < 50){
                //green
            }
            else if(locationDistance > 50 && locationDistance < 100){
                //yellow
            }
            else if(locationDistance > 100 && locationDistance < 150){
                //blue
            }
Galen
  • 29,976
  • 9
  • 71
  • 89
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

3 Answers3

4

I was able to user setMarker(). See answer #3:

Different named Markers on Google Android Map

Community
  • 1
  • 1
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
3

I found the solution for the setMarker method to work properly:

Drawable drawable = getResources().getDrawable(R.drawable....);  
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
overlayItem.setMarker(drawable); 
Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
aaa
  • 31
  • 1
1

Step #1: Create your own subclass of OverlayItem

Step #2: Override getMarker() and return the image you want

Here is a sample project demonstrating this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @CommonsWare, what about extending ItemizedOverlay? Would you not recommend going that route for any reason? – McStretch Nov 15 '10 at 21:52
  • @McStretch: You could override `onDraw()`. However, IMHO, it would seem to make more sense for an `OverlayItem` to have the business logic for what icon it uses, rather than have `ItemizedOverlay` have to have all those rules. But, that's based on limited design information, and so my gut instinct may be wrong in this case. – CommonsWare Nov 15 '10 at 22:09
  • Why can't I call overlayItem.setMarker(drawable) ? //doesn't work btw – Sheehan Alam Nov 15 '10 at 23:18
  • @Sheehan Alam: I seem to recall running into problems with `setMarker()`, though I forget the details. – CommonsWare Nov 16 '10 at 00:08
  • @CommonsWare: how would I override getMarker() would I pass it my locationDistance, and have it return the appropriate resource? – Sheehan Alam Nov 16 '10 at 01:27
  • @Sheehan Alam: I do not know enough about your app to suggest an implementation. However, you cannot change the method signature of `getMarker()`. Hence, the `OverlayItem` would need to know its distance, or whatever criteria it is you are using. – CommonsWare Nov 16 '10 at 01:33
  • @CommonsWare: So for OverlayItem to know my location, I should prob set a private variable there, and have getMarker() access it internally? – Sheehan Alam Nov 16 '10 at 02:57
  • @CommonsWare: I tried out your solution. While I can add a graphic below my pushpin, I would like to ultimately replace my pushpin depending on location distance. Is there a way to do that? – Sheehan Alam Nov 16 '10 at 06:46
  • @Sheehan Alam: Please see the sample project that i linked to, which allows the user to toggle any marker between its original marker and a new one. – CommonsWare Nov 16 '10 at 10:55