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

0 Response to "Add Scale Bar on OpenStreetMap"

Post a Comment