Skip to content Skip to sidebar Skip to footer

Osmdroid Get Current User Location

I am trying to implement OpenStreetMap using osmdroid. I successfully got the world map set to a particular set of geo co-ordinates. here is my code: package com.example.re.osm;

Solution 1:

You can use MyLocationNewOverlay

...
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
mapController.setCenter(startPoint);
//add
GpsMyLocationProvider provider = new GpsMyLocationProvider(this);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationOverlay = new MyLocationNewOverlay(map, provider);
locationOverlay.enableFollowLocation();
locationOverlay.runOnFirstFix(new Runnable() {
    publicvoidrun() {
        Log.d("MyTag", String.format("First location fix: %s", locationOverlay.getLastFix()));
    }
});
map.getOverlayManager().add(locationOverlay);

In onResume method in the activity then add:

public void onResume(){
    super.onResume();
    //this will refresh the osmdroid configuration on resuming.//if you make changes to the configuration, use//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);//Configuration.getInstance().save(this, prefs);
    Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
    //add
    locationOverlay.enableMyLocation();
}

And in onPause

public void onPause(){
    super.onResume();
    //add
    locationOverlay.disableMyLocation();
}

Where locationOverlay is obviously a field typed as MyLocationNewOverlay.

For more details check documentation for the class. There's also an example in the sample application.

Post a Comment for "Osmdroid Get Current User Location"