开发者

Remove last drawable from OverlayItem in Google Maps API?

开发者 https://www.devze.com 2023-04-12 03:22 出处:网络
I\'m trying to make so when location is changed the drawable gets printed to the users location and it works perfect but when the location was changed second time the drawable from the first location

I'm trying to make so when location is changed the drawable gets printed to the users location and it works perfect but when the location was changed second time the drawable from the first location is still visible. I want to only display the latest location not all the the places when location is changed.

I tried to remove the overlayItem on start of the onLocationChanged method and assigning the overlayItem again when location was called but it didn't work.

Here is the code for the onLocationChanged

public void onLocationChanged(Location location) {
    if (location != null) {     
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        userLocation = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);


        OverlayItem usersLocationIcon = new OverlayItem(userLocation, null, null);
        LocationPin myLocationPin = new LocationPin(userIcon, MainActivity.this);
        myLocationPin.getLocation(usersLocationIcon);
        overlayList.add(myLocationPin);
    }

}

And here is the code for LocationPin class

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class LocationPin extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> locationPoint = new ArrayList<OverlayItem>();
    private Context context;


    public LocationPin(Drawable arg0) {
        super(boundCenter(arg0));
        // TODO Auto-generated constructor stub
    }

    public LocationPin(Drawable marker, Context context) {
        this(marker);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return locationPoint.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return locationPoint.size();
    }

    public void getLocation(OverlayItem item){
        locationPoint.开发者_StackOverflowadd(item);
        this.populate();
    }

}


Just the way you added overlay's to the Mapview, you remove the previous overlay like this :

mapView.getOverlays().remove(oldUsersLocationIcon);

when you get a new location, then you call mapView.Invalidate()

If you haven't kept a track of the old locations use :

mapView.getOverlays().clear();

to clear all the overlay items on your MapView


If you want to remove only last overlay item you can remove by

mapView.getOverlays().remove(mapView.getOverlays().size()-1);

and then call below method to redraw your map

mapView.Invalidate()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号