0

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.

4

2 回答 2

3
 In the onlocationchaged method,add the following line,so that it will make your previous pin disappear..if(!mapOverlays.isEmpty()) 
 { 
 mapOverlays.clear(); 
 mapView.invalidate();}
于 2012-05-24T19:23:02.580 回答
1

我在您的代码中没有看到 overlayList 的定义,但我想它必须是类型ArrayList<OverlayItem>

因此,在您使用overlayList.add(custom) 之前,您需要调用overlayList.clear()。这将只留下最新位置的标记。就更新显示而言,我认为它应该在您 .add 时执行此操作。我想您可以随时在地图视图上调用 .invalidate() 以查看是否有帮助

于 2012-05-24T18:59:27.033 回答