Amarnath
Amarnath

Reputation: 1111

How to remove a specific ItemizedOverlay from a MapView?

I have 2 instances of ItemizedOverlay in my application, one for CurrentLocation and another for something else. I have added these 2 overlays to a MapView.

I want to update the CurrentLocation overlay when the refresh button of the map is tipped. If I did, it means it shows the previous current location also, and the new one also. How do I remove the previous one?

But I need to show another overlay also.

I have subclassed the ItemizedOverlay:

MyItemizedOverlay currentOverlay = new MyItemizedOverlay(pushPinDrawable);
MyItemizedOverlay anotherOverlay = new MyItemizedOverlay(drawable);

The code for the refresh button:

if (currentGeo != null) {
    mapView.getOverlays().remove(currentOverlay);
    mapVite();
    mapOverlays = mapView.getOverlays();
    myLocationOverlay = new OverlayItem(currentGeo, "My Location", mapTime.format(new Date(myLocationTime)));   
                                    // (currentGeo is the updated current geo point.)
    currentOverlay.addOverlay(myLocationOverlay);
    mapOverlays.add(currentOverlay);
    mapController.animateTo(currentGeo);
} else {
    Toast.makeText(this, "Your Current Location is temporarily unavailable", Toast.LENGTH_LONG).show();
}

Any idea?

Upvotes: 1

Views: 494

Answers (2)

MKJParekh
MKJParekh

Reputation: 34301

Berfore adding the CurrentOverlay in mapOverlays array, you can surely do something like this,

if(mapOverlays.contains(itemizedOverlay))
{
   mapOverlays.remove(itemizedOverlay);
}
mapOverlays.add(itemizedOverlay);

Upvotes: 1

Vipul
Vipul

Reputation: 28093

You should have some like this

public void onLocationChanged(Location location) {
        if (mMyOverlay == null) {
            mMyOverlay = new myOverlay(getResources().getDrawable(R.drawable.arrow), MyMap);
            MyMap.getOverlays().add(mMyOverlay);
        } else {
            MyMap.getOverlays().remove(mMyOverlay);
            MyMap.invalidate();
            mMyOverlay = new myOverlay(getResources().getDrawable(R.drawable.arrow), MyMap);
            MyMap.getOverlays().add(mMyOverlay);
        }
        if (location != null) {
            MyMap.invalidate();
            GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()), microdegrees(location.getLongitude()));
            MyController.animateTo(MyPos);
            mMyOverlay.addPoint(MyPos, "My position", "My position");
        }

Upvotes: 1

Related Questions