I am working on an android application that determines user location in osm maps. I am being able to show user location on the map, but if the location change the whole map is reloaded, what’s wrong with that? Also how I can increase the accuracy of the user location? And how can I make a circle that increase and decrease according to the accuracy(as shown in Google one)?
code :
public class OsmDemoActivity extends Activity implements LocationListener,
MapViewConstants
{
private MapView mMapView;
private MapController mapController;
private LocationManager mLocMgr;
private ItemizedOverlay<OverlayItem> mMyLocationOverlay;
private ResourceProxy mResourceProxy;
ArrayList<OverlayItem> items;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
setContentView(R.layout.main);
//mMapView.setUseDataConnection(false);
initilaizeMap();
//addOverlay();
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
public void initilaizeMap()
{
mMapView = (MapView) this.findViewById(R.id.mapView);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
//mMapView.setUseDataConnection(false);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mapController = this.mMapView.getController();
mapController.setZoom(15);
mapController.setCenter(new GeoPoint(15.610762,32.540345));
/*
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mMapView,
mResourceProxy);
myLocationOverlay.enableMyLocation();
//myLocationOverlay.disableMyLocation(); // not on by default
myLocationOverlay.disableCompass();
myLocationOverlay.disableFollowLocation();
myLocationOverlay.setDrawAccuracyEnabled(true);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapController.animateTo(myLocationOverlay
.getMyLocation());
}
});
//ArrayList<OverlayItem> mOsmOverlays;
//mOsmOverlays.add(myLocationOverlay);
*/
}
public void addOverlay()
{
GeoPoint point2 = new GeoPoint(53554070, -2959520); // centre map here
GeoPoint point3 = new GeoPoint(53554070 + 1000, -2959520 + 1000); // icon goes here
GeoPoint point4 = new GeoPoint(15.610844, 32.540045);
GeoPoint point5 = new GeoPoint(15610844 + 40, 32540045 + 40);
GeoPoint point6 = new GeoPoint(15610844 + 50, 32540045 + 50);
GeoPoint point7 = new GeoPoint(15610844 + 10, 32540045 +10);
mapController.setCenter(point4);
items = new ArrayList<OverlayItem>();
// Put overlay icon a little way from map center
items.add(new OverlayItem("Here5", "SampleDescription", point5));
items.add(new OverlayItem("Here6", "SampleDescription", point6));
items.add(new OverlayItem("Here7", "SampleDescription", point7));
/* OnTapListener for the Markers, shows a simple Toast. */
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemSingleTapUp '" + item.mTitle, Toast.LENGTH_LONG).show();
return true; // We 'handled' this event.
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemLongPress '" + item.mTitle ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
mMapView.invalidate();
}
public void displayLocation(GeoPoint loc)
{
mapController.setCenter(loc);
items = new ArrayList<OverlayItem>();
// Put overlay icon a little way from map center
items.add(new OverlayItem("Here u r", "SampleDescription", loc));
/* OnTapListener for the Markers, shows a simple Toast. */
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemSingleTapUp '" + item.mTitle, Toast.LENGTH_LONG).show();
return true; // We 'handled' this event.
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemLongPress '" + item.mTitle ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
mMapView.getOverlays().clear();
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
//mMapView.invalidate();
}
public void onLocationChanged(Location location)
{
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint gpt = new GeoPoint(lat, lng);
//mapController.setCenter(gpt);
//mMapView.invalidate();
displayLocation(gpt);
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
You must use an location change listener Maybe this can help you
Listener:
package dispatch.driver.osmMaps;
import org.osmdroid.util.GeoPoint;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class OsmGeoUpdateHandler implements LocationListener
{
private OsmMapsActivity mMapActivity;
public OsmGeoUpdateHandler(OsmMapsActivity aMapActivity)
{
this.mMapActivity = aMapActivity;
}
@Override
public void onLocationChanged(Location location)
{
Toast.makeText(mMapActivity,
"latitude = " + location.getLatitude() * 1e6 + " longitude = " + location.getLongitude() * 1e6,
Toast.LENGTH_SHORT).show();
int latitude = (int) (location.getLatitude() * 1E6);
int longitude = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(latitude, longitude);
mMapActivity.updateCarPosition(point);
}
@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
}
}
OsmMapActivity class (how to use listener)
public class OsmMapsActivity extends Activity
{
// final private int MAP_DEFAULT_ZOOM = 14;
final private double MAP_DEFAULT_LATITUDE = 44.445883;
final private double MAP_DEFAULT_LONGITUDE = 26.040963;
private MapView mMapView;
private ResourceProxy mResourceProxy;
private OsmMapsItemizedOverlay mItemizedOverlay;
private MyLocationOverlay mMyLocationOverlay;
private LocationManager locationManager;
private OverlayItem lastPosition = null;
private Order mOrder;
OsmGeoUpdateHandler mUpdateHandler;
private ArrayList<OverlayItem> mItems = new ArrayList<OverlayItem>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Specify the XML layout to use:
setContentView(R.layout.osmmap);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
mMapView = (MapView) findViewById(R.id.mapview);
// Setup the mapView controller:
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mMapView.setClickable(true);
mMapView.setUseDataConnection(false);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.getController().setZoom(12);
/* location manager */
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mUpdateHandler = new OsmGeoUpdateHandler(this);
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));
}
} // end onCreate()
public void updateCarPosition(GeoPoint aPoint)
{
if (mItemizedOverlay == null)
{
return;
}
OverlayItem overlayItem;
/* remove last position marker */
removeLastPosition(lastPosition);
overlayItem = new OverlayItem("Center", "Center", (GeoPoint) aPoint);
lastPosition = overlayItem;
mItemizedOverlay.addOverlay(overlayItem);
mMapView.getOverlays().add(mItemizedOverlay);
mMapView.getController().animateTo(aPoint);
}
}
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