bobanf89 / osmdroid

Automatically exported from code.google.com/p/osmdroid
0 stars 0 forks source link

Shape drawable does not appear as marker in ItemizedOverlay #441

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create drawable as XML resource.
2. Set this drawable as marker to overlay.
3. Add item to overlay and overlay to map.

What is the expected output? What do you see instead?
Custom marker to be drawn on item location but it doesn't happen.

What version of the product are you using? On what operating system?
osmdroid-android-3.0.10, Android 4.0.4

This is custom marker in marker.xml file in res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="oval">
     <size android:width="20dp"
         android:height="20dp"/>
     <solid android:color="#F00"/>
</shape>

This is onCreate method of MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setTileSource(TileSourceFactory.MAPNIK);

    Drawable marker = getResources().getDrawable(R.drawable.marker);

    ItemizedIconOverlay<OverlayItem> overlay = new ItemizedIconOverlay<OverlayItem>(
            new ArrayList<OverlayItem>(), marker, null,
            new DefaultResourceProxyImpl(this));

    GeoPoint geoPoint = new GeoPoint(54.232, -4.485);
    OverlayItem item = new OverlayItem(null, null, geoPoint);
    overlay.addItem(item);

    mapView.getOverlays().add(overlay);

    mapView.setBuiltInZoomControls(true);
    mapView.getController().setZoom(8);
    mapView.getController().setCenter(geoPoint);
}

But if I create overlay by this constructor then default marker is drawn
ItemizedIconOverlay<OverlayItem> overlay = new 
ItemizedIconOverlay<OverlayItem>(this, new ArrayList<OverlayItem>(), null);

Original issue reported on code.google.com by lukas.vy...@gmail.com on 17 Jun 2013 at 7:34

GoogleCodeExporter commented 8 years ago
Hmmm... maybe ShapeDrawables don't work in ItemizedOverlay. What happens if you 
pull a regular icon from the resources and use that as the marker? Does that 
work?

Original comment by kurtzm...@gmail.com on 17 Jun 2013 at 9:44

GoogleCodeExporter commented 8 years ago
png icon is drawn correctly. I think that in Android drawable is just drawable 
no matter what is its source.

Original comment by lukas.vy...@gmail.com on 18 Jun 2013 at 7:21

GoogleCodeExporter commented 8 years ago

Original comment by kurtzm...@gmail.com on 21 Jun 2013 at 12:15

GoogleCodeExporter commented 8 years ago
To use ShapeDrawables (or possibly other drawables that aren't BitmapDrawable) 
you will need to turn off safe canvas drawing for the ItemizedOverlay. You can 
do this by calling mItemizedOverlay.setUsingSafeCanvas(false).

Also update to the latest trunk which includes a fix for ItemizedOverlay.

Original comment by kurtzm...@gmail.com on 21 Jun 2013 at 12:17

GoogleCodeExporter commented 8 years ago
Tried Drawable with locationOverlay.setUseSafeCanvas(false) on 3.0.11 from 
trunk and still got no icons.

Then i tried to make a ShapeDrawable created programatically and result is the 
same. Any headsup on the progress on this issue?

Original comment by gav...@gmail.com on 25 Jul 2013 at 3:17

GoogleCodeExporter commented 8 years ago
I see you called the overlay locationOverlay - I just want to make sure you are 
setting setUseSafeCanvas(false) on the ItemizedOverlay and not the 
MyLocationOverlay by accident. Also try setting setUseSafeCanvas(false) on the 
MapView itself to see if that works.

I may just put in a check for instanceof BitmapDrawable in ItemizedOverlay and 
if that doesn't pass then call getUnsafeCanvas() and call the draw inside that 
handler.

Original comment by kurtzm...@gmail.com on 25 Jul 2013 at 3:47

GoogleCodeExporter commented 8 years ago
setUseSafeCanvas(false) on the MapView works, it displays drawable shapes, 
however they are drawn above their real position. All the itemizedOverlayItem 
are drawn above their real position, actually. So it might not be a good option.
setUseSafeCanvas(false) on the ItemizedOverlay does not solve anything :(
Thanks anyway for your advice.

Original comment by waeselyn...@gmail.com on 19 Aug 2013 at 11:48

GoogleCodeExporter commented 8 years ago
Did you set your Hotspot correctly? You likely want Hotspot.CENTER.

Original comment by kurtzm...@gmail.com on 19 Aug 2013 at 2:43

GoogleCodeExporter commented 8 years ago
I finally opted for using an image to avoid to loose too much time, but yes, I 
used to set up hotspot. kinda weird issue :-/

Original comment by waeselyn...@gmail.com on 20 Aug 2013 at 9:09

GoogleCodeExporter commented 8 years ago
You can make BitmapDrawable from xml resource by drawing it on temporary 
canvas. 

private Drawable toBitmapDrawable(Drawable anyDrawable){ 
    Bitmap bmp = Bitmap.createBitmap(anyDrawable.getIntrinsicWidth(), anyDrawable.getIntrinsicHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(bmp);
    anyDrawable.setBounds(0, 0, anyDrawable.getIntrinsicWidth(), anyDrawable.getIntrinsicHeight());
    anyDrawable.draw(canvas);
    return new BitmapDrawable(getResources(), bmp);
}

Then use BitmapDrawable for overlay instead of xml resource.
Drawable marker = 
toBitmapDrawable(getResources().getDrawable(R.drawable.marker));

ItemizedIconOverlay<OverlayItem> overlay = new ItemizedIconOverlay<OverlayItem>(
            new ArrayList<OverlayItem>(), marker, null,
            new DefaultResourceProxyImpl(this));

Original comment by sergey.alexeev@gmail.com on 5 Apr 2014 at 11:39