我知道在关于将自定义添加的标记保存到地图之前我已经问了类似这样的问题所以当退出应用程序并返回地图时,标记应该仍然在那里,但我正在训练自己并且发现它很难理解一些事情
以下是SavePreferences()
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("listSize", markerList.size());
for(int i = 0; i <markerList.size(); i++){
editor.putFloat("lat"+i, (float) markerList.get(i).getPosition().latitude);
editor.putFloat("long"+i, (float) markerList.get(i).getPosition().longitude);
editor.putString("title"+i, markerList.get(i).getTitle());
}
editor.commit();
}
这是LoadPreferences()
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
int size = sharedPreferences.getInt("listSize", 0);
for(int i = 0; i < size; i++){
double lat = (double) sharedPreferences.getFloat("lat"+i,0);
double longit = (double) sharedPreferences.getFloat("long"+i,0);
String title = sharedPreferences.getString("title"+i,"NULL");
markerList.add(googleMap.addMarker(new MarkerOptions().position(new LatLng(lat, longit)).title(title)));
}
}
}
然后我有了这个:
private List<Marker> markerList;
public MapActivity(){
if(markerList == null){
markerList = new ArrayList<Marker>();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_googlemaps);
所以以上所有我理解,但我不明白的是我如何告诉应用将onMapLongClick
标记保存到上面的代码中,这是我的onMapLongClick
代码:< / p>
@Override
public void onMapLongClick(LatLng point) {
thePoint=point;
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(thePoint).icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker)));
markerId = marker.getId(); //this is for the image taken by the camera intent to display full size image for marker.
基本上,如何将onMapLongClick
保存到共享首选项?
修改
我这里的代码是onCreate
方法吗?
LoadPreferences();
Intent intent = getIntent();
Bundle data = intent.getExtras();
String label = data.getString("title");
int newLatitude = data.getInt("firstPoint");
int newLongitude = data.getInt("secondPoint");
有人可以帮助我吗?