【发布时间】:2018-01-19 18:29:54
【问题描述】:
在我的应用程序中,用户有两个选项来放置标记:1) 通过onMapLongClick 方法,2) 通过使用语音命令。它们都可以正常工作,但我注意到一个错误,该错误仅在通过主页按钮关闭应用程序几个小时后才出现。
如果我等了几个小时再回到应用程序,标记仍然存在;但是,我两次分别等了大约 5-6 个小时,标记不见了。
我在这里做错了什么?这对我的用户来说非常糟糕,因为该应用程序旨在记住一个位置,即使在关闭应用程序数小时(或可能数天)之后也是如此。一次只有一个标记,而不是多个标记。
这里是存储或检索 LatLng 对象的位置:
onMapReady:
//Get Lat and Lng of marker and place on map if marker is there && if app has already been launched
if (prefs.getBoolean("firstrun", false)) {
try {
FileInputStream input = openFileInput("latlngpoints.txt");
DataInputStream din = new DataInputStream(input);
int sz = din.readInt();
for (int i = 0; i < sz; i++) {
String str = din.readUTF();
String[] stringArray = str.split(",");
double lat = Double.parseDouble(stringArray[0]);
double lon = Double.parseDouble(stringArray[1]);
LatLng newLatLng = new LatLng(lat, lon);
m = mMap.addMarker(new MarkerOptions()
.position(newLatLng)
.title("My Ride")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker30x48)));
markerDeleted = false;
}
} catch (IOException e) {}
onActivityResult 用于语音识别按钮:
ArrayList<LatLng> markerLoc = new ArrayList<>();
markerLoc.add(new LatLng(latLng.latitude, latLng.longitude));
try {
FileOutputStream output = openFileOutput("latlngpoints.txt", Context.MODE_PRIVATE);
DataOutputStream dout = new DataOutputStream(output);
dout.writeInt(markerLoc.size());
for (LatLng point : markerLoc) {
dout.writeUTF(point.latitude + "," + point.longitude);
}
dout.flush();
dout.close();
} catch (IOException e) {
//Log.d(TAG, "onActivityResult: " + e.getMessage());
}
onMapLongClick:
ArrayList<LatLng> markerLoc = new ArrayList<>();
markerLoc.add(new LatLng(latLng.latitude, latLng.longitude));
try {
FileOutputStream output = openFileOutput("latlngpoints.txt", Context.MODE_PRIVATE);
DataOutputStream dout = new DataOutputStream(output);
dout.writeInt(markerLoc.size());
for (LatLng point : markerLoc) {
dout.writeUTF(point.latitude + "," + point.longitude);
//Log.d(TAG, "onMapLongClick: " + String.valueOf(point.latitude) + "," + String.valueOf(point.longitude));
}
dout.flush();
dout.close();
} catch (IOException e) {
//Log.d(TAG, "onMapLongClick: " + e.getMessage());
}
我是否也应该在我的 onPause 和 onResume 方法中做一些事情?除了停止指南针的传感器侦听器并检查是否启用了位置服务之外,它们几乎是空的。
/*
Stop sensor listeners
Also check for no Network/GPS
*/
@Override
protected void onPause() {
super.onPause();
stopSensorListeners();
}
@Override
protected void onResume() {
super.onResume();
startSensorListeners();
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//AlertDialog to inform user GPS is off and take them to their Settings activity
buildAlertMessageNoGps();
}
}
如果这是保持持久性存储的更好方法,我很乐意创建一个数据库。
【问题讨论】:
标签: android google-maps google-maps-markers