androiddictionarylocationdrawableoverlayitem

Multiple drawables on my curent location


I have a MapActivity where i get the current location and i pin a drawable on it. The thing is that when my location is changed ( onchangedlocation method ) there appears another pinpoint on the map but the first one does not disappear. Another problem is that onchangedlocation does not pin another drawable until i touch the screen. What should I do to appear instantly. I want to show with this drawable the current location of the car so it should move properly, like a GPS or something.

Here is my activity with which I add the pinpoint.

public class CustomPinpoint extends ItemizedOverlay<OverlayItem>{


private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;


public CustomPinpoint(Drawable defaultMarker) {
    super(boundCenter(defaultMarker));
    // TODO Auto-generated constructor stub
}
public CustomPinpoint(Drawable m, Context context) {
    // TODO Auto-generated constructor stub
    this(m);
    c = context;
}

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



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

public void insertPinpoint(OverlayItem item){
    pinpoints.add(item);
    this.populate();
}
  }

and I use this in my oncreate method :

    lm= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria crit = new Criteria();

        towers = lm.getBestProvider(crit, false);
        final Location location = lm.getLastKnownLocation(towers);

        if(location != null){
            latit = (int) (location.getLatitude() *1E6);
            longi = (int) (location.getLongitude() *1E6);
            ourLocation = new GeoPoint(latit, longi);
            controller.animateTo(ourLocation);
            controller.setZoom(14);

            if//myItemizedOverlay.addItem(ourLocation, "myPoint1", "myPoint1");
            OverlayItem overlayItem = new OverlayItem(ourLocation, "What's up", "2nd String");
            CustomPinpoint custom = new CustomPinpoint(d, Map.this);
            custom.insertPinpoint(overlayItem);
            overlayList.add(custom);}
            map.setSatellite(false);
            map.setStreetView(true);

and this is my onlocationchanged

   public void onLocationChanged(Location l) {
    // TODO Auto-generated method stub
    latit = (int) (l.getLatitude() *1E6);
    longi = (int) (l.getLongitude() *1E6);
    GeoPoint ourLocation = new GeoPoint(latit, longi);
    OverlayItem overlayItem = new OverlayItem(ourLocation, "What's up", "2nd String");
    CustomPinpoint custom = new CustomPinpoint(d, Map.this);
    custom.insertPinpoint(overlayItem);
    overlayList.add(custom);
}

I thought that for the multiple drawables on my current location a solution would be to display only the last pinpoint(geopoint) added but I don't really know how to do this.

I would be very grateful if you could help me with both problems. Thank you !

PS: I would need the drawable looks like it is moving continuesly when the device is in a car or something which goes with some speed.


Solution

  • I don't see the definition of overlayList in your code but I guess it must be of type ArrayList<OverlayItem>

    So before you overlayList.add(custom) you need to call overlayList.clear(). That will leave you with just the latest position's marker. As far as updating the display, I would have thought it should do that when you .add. I suppose you could always call .invalidate() on the mapview to see if that helps