Java – Osmdroid Bonus Pack – MyLocationNewOverlay

Osmdroid Bonus Pack – MyLocationNewOverlay… here is a solution to the problem.

Osmdroid Bonus Pack – MyLocationNewOverlay

I currently have several features causing some issues that initially worked fine but now produce errors after changing something. Using Android Studio you can look at previous versions of the code, but to no avail.

Anyway, I have a MyLocationNewOverlay global declaration like this:

MyLocationNewOverlay location_overlay;

Starts when the user navigates to an activity using map:

map = (MapView) findViewByID(;

<.. some working code that sets the tile source and the center.. >

location_overlay = new MyLocationNewOverlay(getApplicationContext(), map);

This code shows a small humanoid marker with a precision circle when run, but now it doesn’t show any errors, even though it doesn’t produce any errors. I tried MyLocationOverlay, which is now obsolete, but it doesn’t work either.

The second problem lies in the “onClick” method on the button, which should focus the map on the user’s current location, which also used to work.

public void onBtnFocusOnMe(View view){
    GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());
    if(gp != null){

This will be done in GeoPoint gp = new GeoPoint(location_overlay.getMyLocation()); A null pointer error was generated


How I usually cover some projects is like this, it’s not your direct solution, but you might be able to extract something useful from here :

public void showStartGoalMarkers(GeoPoint start, GeoPoint goal) {
    List<OverlayItem> mStartGoalItems = new ArrayList<>();
    OverlayItem startItem = new OverlayItem("", "", start);
    Drawable newMarker = mMapView.getContext().getResources().getDrawable(R.drawable.ic_start);

    OverlayItem goalItem = new OverlayItem("", "", goal);
    Drawable newMarker2 = mMapView.getContext().getResources().getDrawable(R.drawable.ic_end);

mMapView.getOverlays().add(new ItemizedIconOverlay<OverlayItem>(mStartGoalItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
        public boolean onItemSingleTapUp(int index, OverlayItem item) {
            return false;

        public boolean onItemLongPress(int index, OverlayItem item) {
            return false;
    }, mMapView.getResourceProxy()));


Finally, you invalidate the map view. Hope this helps.

Edit: Code to mark the current location

and code to update the current location when a new location is passed:

 private void markMyLocation(Location location) {
    mOverlayItems.add(0, new OverlayItem("", "", new GeoPoint(location)));

if (mMyLocationOverlay == null) {
        mMyLocationOverlay = new MyLocationOverlay(mOverlayItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
            public boolean onItemSingleTapUp(int index, OverlayItem item) {
                IMapController mapController = mMapView.getController();
                return true;

            public boolean onItemLongPress(int index, OverlayItem item) {
                return false;
        }, mMapView.getResourceProxy());


} else {
        IMapController mapController = mMapView.getController();

MyLocationOverlay class:

public class MyLocationOverlay extends ItemizedIconOverlay<OverlayItem> {

List<OverlayItem> mMyLocation;
int mResourceId;

public MyLocationOverlay(List<OverlayItem> pList,
                            OnItemGestureListener<OverlayItem> pOnItemGestureListener,
                            ResourceProxy pResourceProxy) {
    super(pList, pOnItemGestureListener, pResourceProxy);
    this.mMyLocation = pList;
    this.mResourceId = R.drawable.my_location;

public void draw(Canvas canvas, MapView mapview, boolean arg2) {
    super.draw(canvas, mapview, true);

if (!mMyLocation.isEmpty()) {

IGeoPoint geoPointLocation = mMyLocation.get(0).getPoint();
        Point out = new Point();
        mapview.getProjection().toPixels(geoPointLocation, out);

Bitmap bm = BitmapFactory.decodeResource(mapview.getResources(),
                out.x - bm.getWidth() / 2,  //shift the bitmap center
                out.y - bm.getHeight() / 2,  //shift the bitmap center


public boolean onSingleTapUp(MotionEvent event, MapView mapView) {
     TODO Auto-generated method stub
    return super.onSingleTapUp(event, mapView);
    return true;

Basically what I did was overwrite a single item in the ArrayList mOverlayItems and invalidate the map when the method was called.

Related Problems and Solutions