////Before editing
I am using the below code to redraw a Marker on android map, actually it redraw a marker but it doesn't remove the old one, I tried
mapView.invlaidate
but it didn't remove the old one.
Here is the onLocationChanged function:
@Override
public void onLocationChanged(Location location) {
// mapView.invalidate();
//getCurrantPoint();
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlay(currentOverlay);
mapOverlays.add(itemizedoverlay2);
mapOverlays.remove(currentOverlay);
Log.v("TAG", "Removeeeeeeeeeeeeeeeed");
///
//mapOverlays.add(itemizedoverlay);
}
Thanks in Advance.
/////After editing
if(itemizedoverlay2!=null)
{
mapOverlays.remove(itemizedoverlay2);
itemizedoverlay2 = new HelloItemizedOverlay(drawable2, cntxt);
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlay(currentOverlay);
mapOverlays.add(itemizedoverlay2);
}
else
{
itemizedoverlay2 = new HelloItemizedOverlay(drawable2, cntxt);
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlay(currentOverlay);
mapOverlays.add(itemizedoverlay2);
}
Big thanks to imran khan and agarwal; I used your answers to debug the problem.
try this:
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlayItem(currentOverlay);
mapOverlays.getOverlays().add(itemizedoverlay2);
mapOverlays.getOverlays().remove(itemizedoverlay2);
mapOverlays.invalidate();
Log.v("TAG", "Removeeeeeeeeeeeeeeeed");
//mapOverlays is your mapView obejct and itemizedoverlay2 is your LocationOverlay
remove LocationOverlay i.e itemizedoverlay2 instead of OverlayItem
try this:::
@Override
public void onLocationChanged(Location location) {
// mapView.invalidate();
//getCurrantPoint();
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.clear();
itemizedoverlay2.addOverlay(currentOverlay);
//in above line create new itemizedoverlay2 every time.
mapOverlays.clear();
mapOverlays.add(itemizedoverlay2);
///
//mapOverlays.add(itemizedoverlay);
}
Updated have a look:::
A typical custom overlay looks like this. it encapsulates the various OverlayItems displayed on the map in a list.
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(OverlayItem overlay) {
mOverlays.remove(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
@Override
public int size() {
return mOverlays.size();
}
}
Methods can be exposed to add / remove individual overlayitems, but also the remove all overlayitems (clear method).
Remove a single overlayitem
MyItemizedOverlay sitesOverlay = (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.removeOverlay(overlay);
Add a single overlayItem
MyItemizedOverlay sitesOverlay = (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.addOverlay(new OverlayItem(p, "title", "snippet"));
Remove all overlayItems
MyItemizedOverlay sitesOverlay = (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.clear();
Why to reinvent your own distance calculator, there is one built into the Location class.
Check out
distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.