在android上使用标记显示我的位置(位置更新时仍会出现上一个标记)

时间:2012-02-12 15:23:30

标签: android map overlay

我尝试使用标记在地图上显示我当前的位置,在下面找到我的代码。 但如果我的位置发生变化(位置更新),则仍会显示上一个标记。 如何删除以前的标记。请帮忙

public void UpdateMyPosition (Location location){
String addressString = "No location found";

if (location != null) {
    // Update the map location.
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    GeoPoint geoPoint = new GeoPoint((int) (latitude * 1E6),(int) (longitude * 1E6));
    mapController.animateTo(geoPoint);

    Drawable drawable = this.getResources().getDrawable(R.drawable.red);
    MapsOverlay itemizedoverlay2 = new MapsOverlay(drawable, this);

    List<Overlay> myOverlays = mapView.getOverlays();
    OverlayItem overlayitem2 = new OverlayItem(geoPoint, "", "");

    itemizedoverlay2.addOverlay(overlayitem2);
    myOverlays.add(itemizedoverlay2);

    mapView.postInvalidate();

    Geocoder gc = new Geocoder(this, Locale.getDefault());
    try {
      List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
      StringBuilder sb = new StringBuilder();
      if (addresses.size() > 0) {
        Address address = addresses.get(0);
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
          sb.append(address.getAddressLine(i));
      }
      addressString = sb.toString();
    } catch (IOException e) {}
  } else {
    addressString = "No location found";
  }

  Toast.makeText(getBaseContext(),addressString, Toast.LENGTH_SHORT).show();
}

3 个答案:

答案 0 :(得分:1)

myOverlays.clear();

myOverlays.add(itemizedoverlay2);

答案 1 :(得分:0)

使用捆绑的MyLocationOverlay可能会更好,在这种情况下,绘图和更新将自动执行。

答案 2 :(得分:0)

另一个想法是不删除旧标记,而是改变其位置。

相关问题