在 Android 上使用标记显示我的位置(位置更新时,之前的标记仍然出现)

发布于 2025-01-04 23:59:04 字数 1474 浏览 1 评论 0原文

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

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();
}

I tried to show my current position on map using a marker, find my code below.
but if my position is changed (position updated), the previous marker is still appear.
how to remove previous marker. please help

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉月流沐 2025-01-11 23:59:04

放在

myOverlays.clear();

前面

myOverlays.add(itemizedoverlay2);

Put

myOverlays.clear();

before

myOverlays.add(itemizedoverlay2);
緦唸λ蓇 2025-01-11 23:59:04

也许使用捆绑的 MyLocationOverlay,在这种情况下将自动执行绘制和更新。

Maybe it'll be better to use bundled MyLocationOverlay, in this case drawing and updating will be performed automatically.

睫毛上残留的泪 2025-01-11 23:59:04

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

An other idea would be to not remove the old marker, but to change its position instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文