マーカーを使用して現在の位置を地図上に表示しようとしました。 私の位置が変更された場合(位置が更新された場合)、前のマーカーは引き続き表示されます。 前のマーカーを削除する方法。アンドロイドのマーカーを使用して自分の位置を表示します(位置の更新時に以前のマーカーが表示されます)
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、お返事ありがとうございます。 私のアプリケーションには2つのマーカータイプがあります。もし私が.clear()メソッドを使用すると、すべてのマーカーが削除されます。私が別の場所に移動した場合、どのようにspesificマーカー(現在の位置のみ)を削除し、マーカーを再描画するかを指定します。 – ekhasa
myOverlays.remove(int i)を使用してリストから1つを削除することができます – NickT
リストからインデックスを取得する方法は?、インデックス値は0 myOverlays.remove(0)ですが、動作しませんでした。 – ekhasa