我试图在用户点击屏幕时保存标记的位置,我已按照正确的步骤操作如何使用共享首选项并检索它们。但是当我绘制一个位置并关闭并重新打开应用程序时。该应用程序崩溃。
这是我的MapActivity:
//Keys for Sharedpreferences
public static final String SHARED_PREF_NAME = "plot";
public static final String LONGTITUDE = "long";
public static final String LATITUDE = "lat";
public static final String PLOTTED= "plotted";
private boolean plotted= false;
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng philippines = new LatLng(13, 123);
mMap.moveCamera(CameraUpdateFactory.newLatLng(philippines));
mMap.animateCamera(CameraUpdateFactory.zoomTo(5.0f));
if (ActivityCompat.checkSelfPermission(mapsPage.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mapsPage.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
mMap.clear();
mMap.addMarker(new MarkerOptions().position(point).draggable(false));
mMap.moveCamera(CameraUpdateFactory.newLatLng(point));
String lat = Double.toString(point.latitude);
String lon = Double.toString(point.longitude);
SharedPreferences sharedPreferences = mapsPage.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
editor.putString(LONGTITUDE, lon);
editor.putString(LATITUDE, lat);
editor.commit();
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
checkedout = sharedPreferences.getBoolean(LOGGEDIN_SHARED_PREF, false);
if(checkedout)
{
SharedPreferences sharedPreferencess = getSharedPreferences(mapsPage.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String lon = sharedPreferencess.getString(mapsPage.LONGTITUDE,"Not Available");
String lat = sharedPreferencess.getString(mapsPage.LATITUDE,"Not Available");
double lo = Double.parseDouble(lon);
double la = Double.parseDouble(lat);
LatLng resumedPosition = new LatLng(lo,la);
mMap.clear();
mMap.addMarker(new MarkerOptions().position(resumedPosition).draggable(false));
mMap.moveCamera(CameraUpdateFactory.newLatLng(resumedPosition));
}
}
提前感谢您的帮助! :d
答案 0 :(得分:1)
您似乎正在尝试在onResume()
上移动地图,但我猜想在Android调用onResume()
时地图尚未准备就绪。所以为了解决这个问题,我会将其移到onMapReady()
方法上。
//Keys for Sharedpreferences
public static final String SHARED_PREF_NAME = "plot";
public static final String LONGTITUDE = "long";
public static final String LATITUDE = "lat";
public static final String PLOTTED= "plotted";
private boolean plotted= false;
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng philippines = new LatLng(13, 123);
mMap.moveCamera(CameraUpdateFactory.newLatLng(philippines));
mMap.animateCamera(CameraUpdateFactory.zoomTo(5.0f));
if (ActivityCompat.checkSelfPermission(mapsPage.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mapsPage.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
mMap.clear();
mMap.addMarker(new MarkerOptions().position(point).draggable(false));
mMap.moveCamera(CameraUpdateFactory.newLatLng(point));
String lat = Double.toString(point.latitude);
String lon = Double.toString(point.longitude);
SharedPreferences sharedPreferences = mapsPage.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
editor.putString(LONGTITUDE, lon);
editor.putString(LATITUDE, lat);
editor.commit();
}
});
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
checkedout = sharedPreferences.getBoolean(LOGGEDIN_SHARED_PREF, false);
if(checkedout)
{
SharedPreferences sharedPreferencess = getSharedPreferences(mapsPage.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String lon = sharedPreferencess.getString(mapsPage.LONGTITUDE,"Not Available");
String lat = sharedPreferencess.getString(mapsPage.LATITUDE,"Not Available");
double lo = Double.parseDouble(lon);
double la = Double.parseDouble(lat);
LatLng resumedPosition = new LatLng(lo,la);
mMap.clear();
mMap.addMarker(new MarkerOptions().position(resumedPosition).draggable(false));
mMap.moveCamera(CameraUpdateFactory.newLatLng(resumedPosition));
}
}
@Override
protected void onResume() {
super.onResume();
}