-1

I am using OSMdroid to show an offline map with user's current location.

1) PREFERRED - I would like to change the default icon showing current location to a custom icon. Also I need it to change its rotation based on bearing returned from GPS lets say every 5 seconds. The position of the icon should be in the center of the screen.

OR

2) possibility - rotating the map with custom current location icon fixed on the bottom of the screen.

Is there any way to do it in osmdroid?

Thanks.

Andrej S
  • 61
  • 8

3 Answers3

0

1) that's been supported for a while with osmdroid. There are several my location type overlays and examples on how to use them. Depending on what version of osmdroid you're using, the mechanism to replace the default icon is a bit different.

2) there's a pull request open for osmdroid to support this feature. i'm pretty sure it allowed for the my location offset to be anywhere on the screen.

spy
  • 3,199
  • 1
  • 18
  • 26
0

Seems like I figured out the first possibility. I added a marker to the map which changes its position when onLocationChanged() is called. Also the map moves so the marker is in the center. Then I do marker.setRotation(bearing).

Andrej S
  • 61
  • 8
0
myLocationOverlay = new DirectedLocationOverlay(this);
Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.direction_arrow, null);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
myLocationOverlay.setDirectionArrow(bitmap);

//code to change default location icon
map.getOverlays().add(myLocationOverlay);    

need to implement onLocationChanged method

@Override
public void onLocationChanged(final Location pLoc) {
    long currentTime = System.currentTimeMillis();
    if (mIgnorer.shouldIgnore(pLoc.getProvider(), currentTime))
        return;
    double dT = currentTime - mLastTime;
    if (dT < 100.0) {
        //Toast.makeText(this, pLoc.getProvider()+" dT="+dT, Toast.LENGTH_SHORT).show();
        return;
    }
    mLastTime = currentTime;

    GeoPoint newLocation = new GeoPoint(pLoc);
    if (!myLocationOverlay.isEnabled()) {
        //we get the location for the first time:
        myLocationOverlay.setEnabled(true);
        map.getController().animateTo(newLocation);
    }

    GeoPoint prevLocation = myLocationOverlay.getLocation();
    myLocationOverlay.setLocation(newLocation);
    myLocationOverlay.setAccuracy((int) pLoc.getAccuracy());

    if (prevLocation != null && pLoc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
        mSpeed = pLoc.getSpeed() * 3.6;
        long speedInt = Math.round(mSpeed);
        TextView speedTxt = findViewById(R.id.speed);
        speedTxt.setText(speedInt + " km/h");

        //TODO: check if speed is not too small
        if (mSpeed >= 0.1) {
            mAzimuthAngleSpeed = pLoc.getBearing();
            myLocationOverlay.setBearing(mAzimuthAngleSpeed);
        }
    }

    if (mTrackingMode) {
        //keep the map view centered on current location:
        map.getController().animateTo(newLocation);
        map.setMapOrientation(-mAzimuthAngleSpeed);
    } else {
        //just redraw the location overlay:
        map.invalidate();
    }

    if (mIsRecordingTrack) {
        recordCurrentLocationInTrack("my_track", "My Track", newLocation);
    }
}
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
Harry
  • 1