How to add a setMyLocationEnabled button on OSMap?

2019-05-20 09:10发布

问题:

Can we enable a 'GotoMyLocation' button on OSMDroid like mMap.setMyLocationEnabled(true); on GoogleMap?

I didn't find such feature in MapView class.

回答1:

Create one of these

org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay mylocation = new MyLocationNewOverlay(...);

Add it to your MapView

mMapView.getOverlays().add(mylocation);

Then try mylocation.enableFollowLocation();



回答2:

To set your current location use, LOCATION_SERVICE of LocationManager. This will listen your gps location and will set your center to the present location.

LocationManager myLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        LocationListenerProxy llp=new LocationListenerProxy(myLocationManager);
        llp.startListening(gpsLocationListener, 1, 1) ; 

place this code in your onCreate Activity and then define locationlistener in a different class

public final LocationListener gpsLocationListener = new LocationListener() {
      @Override
      public void onLocationChanged(Location location){  
          textView.setText(location.getLatitude()+","+location.getLongitude());
          Log.d("Location Changed >>> ", location.getLatitude()+","+location.getLongitude());
      }
      public void onProviderDisabled(String provider){}
      public void onProviderEnabled(String provider){}
      public void onStatusChanged(String provider, int status, Bundle extras){}
     };


标签: osmdroid