【发布时间】:2014-07-14 13:01:51
【问题描述】:
如何在不添加新标记的情况下更新我在 android 的 Google Maps v2 中的标记位置? 每次我的位置发生变化时,我都必须更新用户在谷歌地图中的位置,但它总是在地图上创建一个新点,如何避免这种行为?
这是我的更新方式
public void onLocationChanged(Location location) {
this.localizacao = location;
loc = location;
if (mAdapter.getCount() == 0){
getAdvsFromServer();
}
LatLng me = new LatLng(location.getLatitude(), location.getLongitude());
if (eu != null){
Log.d("marker_eu", "removed");
eu.remove();
}
eu = map.addMarker(new MarkerOptions().position(me).title("Eu"));
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);
eu.setIcon(BitmapDescriptorFactory.fromBitmap(bm));
map.moveCamera(CameraUpdateFactory.newLatLng(me));
//map.moveCamera(CameraUpdateFactory.newLatLngZoom(me, 13));
}
更新: 这样它也总是会创建一个新点:
public void onLocationChanged(Location location) {
this.localizacao = location;
loc = location;
if (mAdapter.getCount() == 0){
getAdvsFromServer();
}
if (ac != null){
ac.setLayoutLocalizacaoVisible(View.GONE, View.VISIBLE);
}
LatLng me = new LatLng(location.getLatitude(), location.getLongitude());
// if (eu != null){
// Log.d("marker_eu", "removed");
// eu.remove();
// }
if (eu == null){
eu = map.addMarker(new MarkerOptions().position(me).title("Eu"));
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);
eu.setIcon(BitmapDescriptorFactory.fromBitmap(bm));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(me, 13));
}else {
eu.setPosition(me);
map.moveCamera(CameraUpdateFactory.newLatLng(me));
}
【问题讨论】:
-
在 onLocationChanged() 中尝试
map.clear();! -
map.clear() 将清除比我想要的更多。我以前试过这个,只是让应用程序崩溃。我只是不知道为什么不删除该点,因为正在调用该方法。
标签: android google-maps-android-api-2 marker