我在项目中,我存储标记纬度,经度,标题共享偏好,并从储值加载它们来实现这样的功能。
声明变量
SharedPreferences sharedPreferences;
int locationCount = 0;
初始化sharedPreferences:
sharedPreferences = getSharedPreferences("location", 0);
现在用这个方法来存储标记值:
public void Save_marker(Marker m) {
//pass market object as parameter
++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(m.getPosition().latitude));
// Storing the longitude for the i-th location
editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(m.getPosition().longitude));
// Storing the count of locations or marker count
editor.putInt("locationCount", locationCount);
editor.putString("title", m.getTitle());
/** Saving the values stored in the shared preferences */
editor.commit();
}
现在用于获取存储的标记值可以使用:
public void Get_markers() {
String title_name = "";
Integer image_icon = R.drawable.currentlocation_icon; //marker icon for map
sharedPreferences = getSharedPreferences("location", 0);
// Getting number of locations already stored
locationCount = sharedPreferences.getInt("locationCount", 0);
String[] location_name = new String[locationCount];
if (locationCount != 0) {
String lat = "";
String lng = "";
// 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");
title_name = sharedPreferences.getString("title", "");
// Getting the longitude of the i-th location
lng = sharedPreferences.getString("lng" + i, "0");
location_name[i] = title_name;
// Drawing marker on the map
drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)), title_name);
}
private void drawMarker(LatLng point, String title_marker) {
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
mMap.addMarker(markerOptions);
//here mMap is your google map object
}
全面落实看到此链接: http://www.androidtrainee.com/adding-multiple-marker-locations-in-google-maps-android-api-v2-and-save-it-in-shared-preferences/
声明一个'私有列表标记=新的ArrayList <>();',并添加'Marker's给它做'markers.add(MMAP。 addMarker(markerOptions));' –
antonio