Search code examples
androidopenstreetmaposmdroid

OSM - Show current location with custom icon


Can anyone tell me how can I show my current location on OSM map with custom icon?


Solution

  • With default person icon:

    MyLocationNewOverlay myLocationoverlay = new MyLocationNewOverlay(mapView);
    myLocationoverlay.enableFollowLocation();
    myLocationoverlay.enableMyLocation();
    mapView.getOverlays().add(myLocationoverlay);
    

    With custom icon:

    MyLocationNewOverlay myLocationoverlay = new MyLocationNewOverlay(mapView);
    myLocationoverlay.enableFollowLocation();
    Drawable currentDraw = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
    Bitmap currentIcon = null;
    if (currentDraw != null) {
        currentIcon = ((BitmapDrawable) currentDraw).getBitmap();
    }
    myLocationoverlay.setPersonIcon(currentIcon);
    myLocationoverlay.enableMyLocation();
    mapView.getOverlays().add(myLocationoverlay);
    

    I am using updated OSM dependency

    compile 'org.osmdroid:osmdroid-android:5.6.5'