I want to find map of my current position and update it when i move via showing a icon in OSMdroid.I find no problem when give specific location latitude and longitude and drawing icon but there are two errors when i give current location latitude and longitude.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
setContentView(R.layout.main);
mMapView = (MapView) this.findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mapController = this.mMapView.getController();
mapController.setZoom(15);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = mLocMgr.getBestProvider(criteria, true);
Location location = mLocMgr.getLastKnownLocation(provider);
GeoPoint mypoint = new GeoPoint(location.getLatitude(), location.getLongitude()); // centre map here
GeoPoint mypointicon = new GeoPoint(location.getLatitude()+1000, location.getLongitude()+1000); // icon goes here
mapController.setCenter(mypoint);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100,
this);
ArrayList<OverlayItem> items=new ArrayList<OverlayItem>();
items.add(new OverlayItem("Here", "Sample Description", mypointicon));
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Toast.makeText(
MapOverLayGiveActivity.this,
"Item '" + item.mTitle, Toast.LENGTH_LONG).show();
return true; // We 'handled' this event.
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
Toast.makeText(
MapOverLayGiveActivity.this,
"Item '" + item.mTitle ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
mMapView.invalidate();
}
Error:
java.lang.NullPointerException
java.lang.RunTimeException
What's wrong in my code and how can i do that? thanks...
To follow location changes, implement LocationListener as below:
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
currentLocation = new GeoPoint(location);
displayMyCurrentLocationOverlay();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Inside onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationListener = new MyLocationListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if( location != null ) {
currentLocation = new GeoPoint(location.getLatitude(), location.getLongitude());
}
}
and displayMyCurrentLocationOverlay method:
private void displayMyCurrentLocationOverlay() {
if( currentLocation != null) {
if( currentLocationOverlay == null ) {
currentLocationOverlay = new ArrayItemizedOverlay(myLocationMarker);
myCurrentLocationOverlayItem = new OverlayItem(currentLocation, "My Location", "My Location!");
currentLocationOverlay.addItem(myCurrentLocationOverlayItem);
mapView.getOverlays().add(currentLocationOverlay);
} else {
myCurrentLocationOverlayItem.setPoint(currentLocation);
currentLocationOverlay.requestRedraw();
}
mapView.getController().setCenter(currentLocation);
}
}
You use this.myLocationOverlay - and because of that the osmDroid draw the current location - but for update the location you must use a location listener. Also, you must remove previous overlay from the map using mapView.getOverlays.clear()
function
Well, let's say that you don't have acces to internet or GPS. In my opinion is safe to have a default point on the map. In this code I check for a valid location. If is null, then I use DEFAULT values. In my application I have no problems. I also draw a navigation path on the map, even if my location is changed.
{
Location location = null;
for (String provider : locationManager.getProviders(true))
{
location = locationManager.getLastKnownLocation(provider);
if (location != null)
{
//location.setLatitude(MAP_DEFAULT_LATITUDE);
//location.setLongitude(MAP_DEFAULT_LONGITUDE);
locationManager.requestLocationUpdates(provider, 0, 0, mUpdateHandler);
break;
}
}
//add car position
if (location == null)
{
location = new Location(LocationManager.GPS_PROVIDER);
location.setLatitude(MAP_DEFAULT_LATITUDE);
location.setLongitude(MAP_DEFAULT_LONGITUDE);
updateCarPosition(new GeoPoint(location));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With