Haniku
Haniku

Reputation: 731

customized icon in osmdroid marker android

I have several stops in route and I wanna display them with customized icons. Everything works like a charm when using default osmdroid icons. But when I change them to image from drawable, markers are displayed above the route (see the image).

            Marker marker = new Marker(mapView);
            marker.setPosition(new GeoPoint(info.getLat(), info.getLon()));
            marker.setAnchor(ANCHOR_CENTER, ANCHOR_BOTTOM);              
            marker.setTitle(info.getName());
            Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.bus, null);
            Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
            Drawable dr = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, (int) (48.0f * getResources().getDisplayMetrics().density), (int) (48.0f * getResources().getDisplayMetrics().density), true));
            marker.setIcon(dr);
            mapView.getOverlays().add(marker);
            mapView.invalidate();

Im using osmbonuspack:6.6.0 and osmdroid-android:6.1.0 and Android 9 (API 28). I already tried to set android:hardwareAccelerated="false" as it said in OSMDroid - Default marker moving when zooming out on Android API 28 or setAnchor(ANCHOR_CENTER,ANCHOR_CENTER) but it wasnt working. Is there any other solution?

customized icons

Upvotes: 6

Views: 5924

Answers (3)

willf80
willf80

Reputation: 1223

Add android:hardwareAccelerated="false" attribute on your activity tag who show the osmdroid map.

<activity
android:name=".MapActivity"
android:hardwareAccelerated="false"/>

This is PokkoDev solution : Full answerer link here

Upvotes: 0

quilkin
quilkin

Reputation: 1447

Setting CENTER, CENTER worked for me.

myMark.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER);

The default placing (without any setAnchor command) appears to be equivalent to

myMark.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);

presumably because the default (teardrop) marker ponts to the bottom of the drop.

Note that you can also replace the constants with other floats: the documentation says between 0.0 and 1.0 but other (bigger, or negative) values appear to work as well.

Upvotes: 0

SXN
SXN

Reputation: 83

The issue with this approach seems to be related to custom icon resolution, it parse the real resolution but it downscales the displayed image. As a fix / workaround you can move your custom png marker out of drawable folder into mipmap-xxxhdpi for example. And also don't forget to set the anchor, like: customMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);

L.E. Do not forget to use a high resolution marker(I have tested with png image)

Upvotes: 4

Related Questions