我试图用标记在地图上显示我的当前位置,在下面找到我的代码。 但如果我的位置发生了变化(位置更新),上一个标记仍然出现。 如何删除以前的标记。请帮助在Android上使用标记显示我的位置(上一个标记在位置更新时仍然出现)
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();
}
嗨,NicT,谢谢你的回答。 我的应用程序有两种标记类型,如果我使用.clear()方法,它将删除所有标记。如何删除特定标记(只是我的当前位置)并重新绘制标记,如果我移动到另一个位置。 – ekhasa 2012-02-15 15:21:11
你可以使用myOverlays.remove(int i)从列表中删除一个 – NickT 2012-02-15 15:41:17
如何从列表中获取索引?,我试图给出索引值为0 myOverlays.remove(0)但不起作用。 – ekhasa 2012-02-18 04:11:47