通过共享流程保存多个标记
我发现了有关如何在地图片段上添加多个标记的一些TUT。但是就我而言,看来它对我不起作用,我可以放置多个标记,但是每当我重新启动应用程序时,这些通过共享的标记都消失了。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
mMap.setMyLocationEnabled(true);
prefs = this.getSharedPreferences("location", 0);
locationCount = prefs.getInt("locationCount", 0);
if(locationCount != 0){
String lat = "";
String lng = "";
for (int i = 0; i < locationCount; i++) {
lat = prefs.getString("Lat" + i,"0");
lng = prefs.getString("Lng" + i, "0");
drawMaker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
}
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng latLng) {
locationCount++;
drawMaker(latLng);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lat" + Integer.toString((locationCount - 1)), Double.toString(latLng.latitude));
editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(latLng.longitude));
editor.putInt("locationCount", locationCount);
editor.commit();
Toast.makeText(MapsActivity.this, "Marker is added to the Map", Toast.LENGTH_SHORT).show();
}
});
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(@NonNull LatLng latLng) {
mMap.clear();
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
locationCount = 0;
}
});
I have found some tuts on how to do add multiple markers on your map fragment. But in my case, It seems that it doesn't work for me, well I can put multiple markers but whenever I restart the application those markers that was save through sharedpreferences is gone.
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
mMap.setMyLocationEnabled(true);
prefs = this.getSharedPreferences("location", 0);
locationCount = prefs.getInt("locationCount", 0);
if(locationCount != 0){
String lat = "";
String lng = "";
for (int i = 0; i < locationCount; i++) {
lat = prefs.getString("Lat" + i,"0");
lng = prefs.getString("Lng" + i, "0");
drawMaker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
}
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng latLng) {
locationCount++;
drawMaker(latLng);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lat" + Integer.toString((locationCount - 1)), Double.toString(latLng.latitude));
editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(latLng.longitude));
editor.putInt("locationCount", locationCount);
editor.commit();
Toast.makeText(MapsActivity.this, "Marker is added to the Map", Toast.LENGTH_SHORT).show();
}
});
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(@NonNull LatLng latLng) {
mMap.clear();
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
locationCount = 0;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论