我試圖用標記在地圖上顯示我的當前位置,在下面找到我的代碼。 但如果我的位置發生了變化(位置更新),上一個標記仍然出現。 如何刪除以前的標記。請幫助在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