Android MapView无法删除标记

时间:2011-01-08 12:26:38

标签: android android-mapview marker

我正在使用locationManager和ItemizedOverlay来绘制我的位置标记,问题是当触发onLocationChanged时我正在绘制新标记而不是最后一个移动到新位置,这是我的onLocationChanged代码:

public void onLocationChanged(Location location) {
  myOverlay object1 = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);

  if(location!=null){
   MyMap.invalidate();
   GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
   MyController.animateTo(MyPos);
   object1.addPoint(MyPos,"Ma position","Ma position");
   MyMap.getOverlays().add(object1);
  }

 }

你能帮我解决这个问题吗?

非常感谢。

MapActivity:

public class Main extends MapActivity implements LocationListener {
  /** Called when the activity is first created. */
  MapView MyMap;
  MapController MyController;

    @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      MyMap=(MapView)findViewById(R.id.MyGMap);
      MyMap.setBuiltInZoomControls(true);

      try {
      getEventsFromAnXML(this);

     } catch (XmlPullParserException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     }
     int j=0;

     myOverlay object = new myOverlay(getResources().getDrawable(R.drawable.marker),MyMap);
     while( j<agencies.size()){
         GeoPoint point = new GeoPoint(microdegrees(agencies.get(j).getLatitude()),microdegrees(agencies.get(j).getLongitude()));
         String Tit;
         String Des;
         Tit=agencies.get(j).getTspTitle();
         Des=agencies.get(j).getTspTitle() + "\nAgence: " + agencies.get(j).getTitle() +
             "\nTél: " + agencies.get(j).getPhone();
         object.addPoint(point,Tit,Des);
         j=j+1;
     }


     MyMap.getOverlays().add(object);
     MyController=MyMap.getController();
     MyController.setZoom(12);

     LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

  }

  private int microdegrees(double value){
      return (int)(value*1000000);
  }


  boolean na=false;
  List<Agency> agencies = new ArrayList<Agency>();
  Agency agency=new Agency();
  int i=0;
  String TempTspTitle;
  String TempTspPhone;
  Boolean TempTspEnabled;
  private String getEventsFromAnXML(Activity activity)
  throws XmlPullParserException, IOException
  {
   StringBuffer stringBuffer = new StringBuffer();
   Resources res = activity.getResources();
   XmlResourceParser xpp = res.getXml(R.xml.hotels);
   xpp.next();
   int eventType = xpp.getEventType();

   while (eventType != XmlPullParser.END_DOCUMENT)
   {
    if(eventType == XmlPullParser.START_DOCUMENT)
    {
     stringBuffer.append("--- Start XML ---");
    }
    else if(eventType == XmlPullParser.START_TAG)
    {
        if (xpp.getName().equals("DataBase")){
            String name=xpp.getAttributeValue(null, "name");
            agency.setTspTitle(name);
            TempTspTitle=name;
            na=true;
        }

        if (xpp.getName().equals("DataBaseEnabled")){
            xpp.next();
            agency.setTspEnabled(Boolean.parseBoolean(xpp.getText()));
            TempTspEnabled=Boolean.parseBoolean(xpp.getText());
            xpp.nextTag();
        }
        if (xpp.getName().equals("Title")){
            xpp.next();
            agency.setTitle(xpp.getText());
            na=false;
            xpp.nextTag();
        }
        if (xpp.getName().equals("Address")){
            xpp.next();
            agency.setAddress(xpp.getText());
            xpp.nextTag();
        }

        if (xpp.getName().equals("Phone") && na==true){
            xpp.next();
            agency.setTspPhone(xpp.getText());
            TempTspPhone=xpp.getText();
            xpp.nextTag();
        }else{
            if (xpp.getName().equals("Phone") && na==false){
                xpp.next();
                agency.setPhone(xpp.getText());
                xpp.nextTag();
            }

        }

        if (xpp.getName().equals("Fax")){
            xpp.next();
            agency.setFax(xpp.getText());
            xpp.nextTag();
        }

        if (xpp.getName().equals("e-Mail")){
            xpp.next();
            agency.setMail(xpp.getText());
            xpp.nextTag();
        }
        if (xpp.getName().equals("Latitude")){
            xpp.next();
            agency.setLatitude(Double.parseDouble(xpp.getText()));
            xpp.nextTag();
        }
        if (xpp.getName().equals("Longitude")){
            xpp.next();
            agency.setLongitude(Double.parseDouble(xpp.getText()));
        }


    }
    else if(eventType == XmlPullParser.END_TAG)
    {
        if (xpp.getName().equals("DataBase") ){
            agency = new Agency();
        }else if (xpp.getName().equals("Agency")){
            agencies.add(i,agency);
            i=i+1;
            agency = new Agency();
            agency.setTspTitle(TempTspTitle);
            agency.setTspPhone(TempTspPhone);
            agency.setTspEnabled(TempTspEnabled);
        }

    }


    eventType = xpp.next();
   }
   stringBuffer.append("\n" + "Size: " + agencies.size());
   return stringBuffer.toString();
  }

  @Override
    protected boolean isRouteDisplayed() {
        return false;
    }


    public void onLocationChanged(Location location) {
        myOverlay object1 = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);

        if(location!=null){
            MyMap.getOverlays().remove(object1);
            MyMap.invalidate();
            GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
            MyController.animateTo(MyPos);
            object1.addPoint(MyPos,"Ma position","Ma position");
            MyMap.getOverlays().add(object1);
        }

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

和myOverlay类:

public class myOverlay extends ItemizedOverlay<OverlayItem>{

    List<OverlayItem> points= new ArrayList<OverlayItem>();
    private Drawable marker=null;  
    private Context c;
    public myOverlay(Drawable marker, MapView mapView) {
        super(marker);
        this.marker=marker;
        c=mapView.getContext();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return (points.get(i));
    }

    public void draw(Canvas canvas,MapView mapView, boolean shadow){
        super.draw(canvas, mapView, shadow);
        boundCenterBottom(marker);
    }

    @Override
    public int size() {
        return points.size();
    }

    public void addPoint(GeoPoint point, String Titre, String Desc){
        this.points.add(new OverlayItem(point,Titre,Desc));
        populate();
    }

    public void removePoint(){
        this.points.clear();
        populate();
    }

    @Override
    protected boolean onTap(int i){
        Toast.makeText(c, points.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
        return (true);
    }

  }      

    }

3 个答案:

答案 0 :(得分:3)

根据戴夫的建议,这解决了这个问题:

  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,"Ma position","Ma position");
     }

感谢..

答案 1 :(得分:1)

getOverlays()返回MapView中Overlay对象的List。每次更新位置时,您的代码都会在此列表中添加另一个叠加层。您需要做的是删除上一个叠加层,或每次完全清空列表。

因此,在添加新叠加层之前,请在方法中调用getOverlays().clear()

编辑,或许这可能会指向正确的方向?我不知道你的myOverlay课程究竟是如何工作的,所以希望你能填补空白!

protected myOverlay mMyOverlay; 

public void onLocationChanged(Location location) {
  if(mMyOverlay == null) {
      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);
      mMyOverlays.setPoint(MyPos,"Ma position","Ma position");
   }
}

答案 2 :(得分:1)

使用ItemizedOverlay,并且如Dave建议的那样,跟踪您想要移动的那个,然后删除跟踪的OverlayItem并添加新的OverlayItem或获取跟踪的,获取其标记或MapPoint并重置其lat / lon坐标到新的位置坐标。您可能还必须调用mapView.invalidate()来强制重绘。