Combine the exercises "Simple example use osmdroid and slf4j-android to implement OpenStreetMapView on Android" and "Location updates from GPS_PROVIDER and NETWORK_PROVIDER" we create a OpenStreetMap to center on user location.
By calling MapController.setCenter(GeoPoint), we can move OpenStreetMap to center on a certain location.
Modify AndroidOpenStreetMapViewActivity.java to implement LocationListener, and updateLoc(Location) method to update and redraw OpenStreetMap once location changed.
package com.exercise.OpenStreetMapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class AndroidOpenStreetMapViewActivity extends Activity {
private MapView myOpenMapView;
private MapController myMapController;
LocationManager locationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myOpenMapView = (MapView)findViewById(R.id.openmapview);
myOpenMapView.setBuiltInZoomControls(true);
myMapController = myOpenMapView.getController();
myMapController.setZoom(12);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//for demo, getLastKnownLocation from GPS only, not from NETWORK
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastLocation != null){
updateLoc(lastLocation);
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(myLocationListener);
}
private void updateLoc(Location loc){
GeoPoint locGeoPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude());
myMapController.setCenter(locGeoPoint);
myOpenMapView.invalidate();
}
private LocationListener myLocationListener
= new LocationListener(){
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateLoc(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
}
Keep using the main.xml in the exercises "Simple example use osmdroid and slf4j-android to implement OpenStreetMapView on Android".
Permission need:
- android.permission.ACCESS_NETWORK_STATE
- android.permission.INTERNET
- android.permission.WRITE_EXTERNAL_STORAGE
- android.permission.ACCESS_FINE_LOCATION
Download the files.
Next:
- Add Scale Bar on OpenStreetMap
No comments:
Post a Comment