Sunday, May 13, 2012

Add Scale Bar on OpenStreetMap

To add a scale bar on OpenStreetMapView is very easy:
- New a ScaleBarOverlay object
- add the Overlay to the MapView

ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
myOpenMapView.getOverlays().add(myScaleBarOverlay);

ScaleBar added


Modify main Java code AndroidOpenStreetMapViewActivity.java from last exercise "Update location on OpenStreetMap".
package com.exercise.OpenStreetMapView;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ScaleBarOverlay;

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);
        }
        
        //Add Scale Bar
        ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
        myOpenMapView.getOverlays().add(myScaleBarOverlay);
    }
    
    @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
   
  }
     
    };

}


Next:
- Display current location marker on OpenStreetMap


3 comments:

  1. Not working with android 4.0 and osmdroid-android-3.0.8
    ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
    myScaleBarOverlay.setImperial();
    myOpenMapView.getOverlays().add(myScaleBarOverlay);

    ReplyDelete
  2. can't seem to display the scale bar.. the application was force to close.
    anyone can help?

    ReplyDelete
  3. The scalebar kept crashing my app, too. After beating my head against the internet for a while, I stumbled across the idea that there are difficulties playing with more modern versions of Android. I simply changed the target SDK version to "11" in the manifest and the app stopped crashing.

    ReplyDelete