Sunday, May 13, 2012

Update location on OpenStreetMap


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.

Update location on OpenStreetMap


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


5 comments:

Unknown said...

Hi!
Can you provide help for zoomToSpan() in osmdroid because when i try to zoomToSpan map is hangup.

Erik said...

hello Girish,

I try to modify modify updateLoc() as below. It work as expected when GPS disabled, but hangup with black screen when GPS enabled! Is it your case?
//=====
private void updateLoc(Location loc){
GeoPoint locGeoPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude());
myMapController.setCenter(locGeoPoint);
//myOpenMapView.invalidate();

//
myMapController.zoomToSpan(100, 100);
myOpenMapView.invalidate();
}
//=====

Anonymous said...

Thanks Man this really is a great source for OSM beginners to start off.

Regards
Muhammad Babar

gumuruhsspj said...

hi,... thanks for the tutorial.
but is it possible if the map
changed with our own map?

how to do that?

Faisal Mohammad Nirzon said...

thanks