私は、長いクリックを使用して共有環境設定にマーカーを追加できるカスタムマップを実装しました。私の地図では、ボタンを押したときにメソッド「Clear Marker」を呼び出すボタンがあります。マップを終了し、変更を行うために再度開く必要があります。マップを即座に更新したいのですが、共有された環境設定に追加された最後のアイテムを削除する方法もありますか? IEは前のマーカーをクリアしますが、すべてのマーカーが1つ追加されるわけではありませんマーカーを格納するために使用されるコードは共有環境設定を使用して一度に1つのマーカーを削除する
@Override
public void onMapLongClick(LatLng latLng) {
addressEditText = (EditText) findViewById(R.id.editTextAddMarker);
title12 = addressEditText.getText().toString();
if (title12.length() > 2) {
MarkerOptions markerOpt1 = new MarkerOptions()
.title(title12)
.anchor(0.5f, 0.5f);
markerOpt1.position(latLng);
mMap.addMarker(markerOpt1);
Toast.makeText(this, "Marker Added", Toast.LENGTH_LONG).show();
locationCount++;
/** Opening the editor object to write data to sharedPreferences */
SharedPreferences.Editor editor = sharedPreferences.edit();
// Storing the latitude for the i-th location
editor.putString("lat" + Integer.toString((locationCount - 1)), Double.toString(latLng.latitude));
// Storing the longitude for the i-th location
editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(latLng.longitude));
//editor.putString("title", addressEditText.getText().toString());
editor.putString("title" + (locationCount-1), addressEditText.getText().toString());
// Storing the count of locations or marker count
editor.putInt("locationCount", locationCount);
/** Saving the values stored in the shared preferences */
editor.commit();
} else if (title12.length() < 1) {
Toast.makeText(this, "Enter title at the top left.", Toast.LENGTH_LONG).show();
}
}
私は、共有環境設定からデータを取得し、最後にマーカーを除去するために使用するコード(私のonMapReady()
// Opening the sharedPreferences object
sharedPreferences = getSharedPreferences("location", 0);
// Getting number of locations already stored
locationCount = sharedPreferences.getInt("locationCount", 0);
// If locations are already saved
if (locationCount != 0) {
String lat = "";
String lng = "";
String title13 = "";
// Iterating through all the locations stored
for (int i = 0; i < locationCount; i++) {
// Getting the latitude of the i-th location
lat = sharedPreferences.getString("lat" + i, "0");
// Getting the longitude of the i-th location
lng = sharedPreferences.getString("lng" + i, "0");
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
title13 = sharedPreferences.getString("title" + i, "0");
//Toast.makeText(this, lat + "," + lng, Toast.LENGTH_LONG).show();
double lat3 = Double.valueOf(lat).doubleValue();
double lng3 = Double.valueOf(lng).doubleValue();
position1 = new LatLng(lat3, lng3);
drawMarker(position1,title13);
}
}
でマップ上にそれを描くです問題はここにあります)ボタンが押された後でマーカーがマップから削除されないのは、ユーザーがボタンを押したままにしてマップに戻るときだけです。また、それは共有の設定をクリアする、誰も私はちょうど最後の項目を削除することができます知っている?
private void clearMarker() {
// Opening the editor object to delete data from sharedPreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
// Clearing the editor
editor.clear();
// Committing the changes
editor.commit();
}
}
上のボタンを介して "のonClick" と呼ばれます。誰もこれを達成する方法を知っていますか?すべての援助は非常に高く評価されます。
正確に何を変更または追加する必要があるのか説明できますか?またはこれを達成するために私が従うべきステップは?私はまだプログラミング/アンドロイドでスキルを伸ばしていますし、長い道のりがあります。正しい方向のヒントがあれば幸いです。 – MichaelCS
答えを編集して詳細を追加しました。それが役に立てば幸い :) –