在地图、Android、Google API 上添加单个标记

发布于 2024-10-31 20:52:16 字数 3339 浏览 5 评论 0原文

在我的应用程序中,我从远程服务器接收坐标,并且我想在地图上的坐标上标记位置,这在 onClick 方法内按需发生。问题是,当我更新位置时,我最终会在地图上看到多个标记,而不是只有一个当前位置。在添加下一个标记之前有什么方法可以删除上一个标记吗?

我按照本教程中的步骤操作: http://developer.android.com /resources/tutorials/views/hello-mapview.html

我的代码如下:

public class AppTwoAndroid extends MapActivity {
private Button refreshButton;
double lat, lon;
ConnectionHandler conhandler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    Log.i("AppTwo", "making connectionhandler object");
    conhandler = new ConnectionHandler();
    conhandler.execute();

    Log.i("AppTwo", "making button");
    this.refreshButton = (Button)this.findViewById(R.id.close);

    final List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    final AppTwoAndroidItemizedOverlay itemizedoverlay = new AppTwoAndroidItemizedOverlay(drawable);

    refreshButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("AppTwo", "inside onclick");
            if (mapOverlays.contains(itemizedoverlay) == true) {
                mapOverlays.remove(itemizedoverlay);
            }
            conhandler.write();
            lat = conhandler.retLat();
            lon = conhandler.retLon();
            lat = lat * 1e6;
            lon = lon * 1e6;
            int ilat = (int) lat;
            int ilon = (int) lon;
            GeoPoint point = new GeoPoint(ilat ,ilon);
            OverlayItem overlayitem = new OverlayItem(point, null, "AppOne");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);


            Toast.makeText(getBaseContext(), "lat is: " + lat + " lon is: " + lon
                    ,  Toast.LENGTH_SHORT).show();

            }
    });
}
@Override
     protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

我的 AppTwoAndroidItemizedOverlay 类是:

public class AppTwoAndroidItemizedOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;

public AppTwoAndroidItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

public AppTwoAndroidItemizedOverlay(Drawable defaultMarker, Context context) {
      super(defaultMarker);
      mContext = context;
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

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

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

}

On my application i receive coordinates from a remote server and i want to mark the location on the coordinates on a map, this happens on demand inside onClick method. The problem is that when i update the location i end up with multiple markers on the map instead of just one, the current location. is there any way to remove the previous marker before adding the next one?

I followed the steps in this tutorial : http://developer.android.com/resources/tutorials/views/hello-mapview.html

And my code goes like this :

public class AppTwoAndroid extends MapActivity {
private Button refreshButton;
double lat, lon;
ConnectionHandler conhandler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    Log.i("AppTwo", "making connectionhandler object");
    conhandler = new ConnectionHandler();
    conhandler.execute();

    Log.i("AppTwo", "making button");
    this.refreshButton = (Button)this.findViewById(R.id.close);

    final List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    final AppTwoAndroidItemizedOverlay itemizedoverlay = new AppTwoAndroidItemizedOverlay(drawable);

    refreshButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("AppTwo", "inside onclick");
            if (mapOverlays.contains(itemizedoverlay) == true) {
                mapOverlays.remove(itemizedoverlay);
            }
            conhandler.write();
            lat = conhandler.retLat();
            lon = conhandler.retLon();
            lat = lat * 1e6;
            lon = lon * 1e6;
            int ilat = (int) lat;
            int ilon = (int) lon;
            GeoPoint point = new GeoPoint(ilat ,ilon);
            OverlayItem overlayitem = new OverlayItem(point, null, "AppOne");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);


            Toast.makeText(getBaseContext(), "lat is: " + lat + " lon is: " + lon
                    ,  Toast.LENGTH_SHORT).show();

            }
    });
}
@Override
     protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

and my AppTwoAndroidItemizedOverlay class is :

public class AppTwoAndroidItemizedOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;

public AppTwoAndroidItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

public AppTwoAndroidItemizedOverlay(Drawable defaultMarker, Context context) {
      super(defaultMarker);
      mContext = context;
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

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

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

}

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

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

发布评论

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

评论(3

要走就滚别墨迹 2024-11-07 20:52:17

在代码之前添加此语句....

mMap.clear();

    mMap.clear();

    LatLng latLng = new LatLng(gps.getLatitude(),  gps.getLongitude());
    mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new 
    LatLng(gps.getLatitude(),  gps.getLongitude()), 15));
    mMap.addMarker(new MarkerOptions().position(latLng).title(""));
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

Add this statement before the code....

mMap.clear();

    mMap.clear();

    LatLng latLng = new LatLng(gps.getLatitude(),  gps.getLongitude());
    mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new 
    LatLng(gps.getLatitude(),  gps.getLongitude()), 15));
    mMap.addMarker(new MarkerOptions().position(latLng).title(""));
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
别把无礼当个性 2024-11-07 20:52:16

在onClick方法中只需给出

mapOverlays.clear();

这应该清除任何现有的标记。

希望这有效:)

In the onClick method just give

mapOverlays.clear();

this should clear any existing markers.

Hope this works :)

昔梦 2024-11-07 20:52:16

在这部分代码中:

   if (mapOverlays.contains(itemizedoverlay) == true) {
        mapOverlays.remove(itemizedoverlay);
    }

当您从mapOverlay结构中删除Overlay时,您并没有真正清除overlay,因此当您添加其他项目并将其重新添加到mapOverlay时,将会有2个标记。

如果您只想要一个标记,请执行具有 setOverlayItem 的覆盖,而不是具有“添加”逻辑的列表。 (意思是只用一个项目进行覆盖,当您添加另一个项目时,只需替换旧的)

希望它有帮助! :D

In this part of code:

   if (mapOverlays.contains(itemizedoverlay) == true) {
        mapOverlays.remove(itemizedoverlay);
    }

When your removing the Overlay from the mapOverlay structure you are not really clearing the overlay, so when you add other item and re-add it to the mapOverlay there will be 2 markers.

If you just want a single marker do an Overlay that has setOverlayItem instead of a list with an 'adding' logic. (meaning do a overlay with just an item that when you add another, just replaces the old one)

Hope it helped! :D

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