Cra*_*ide 6 android google-maps savestate google-maps-markers android-maps
我正在使用Android Google地图v2 API并将其设置为在长按时添加标记.我需要一种方法来保存这些标记,并在应用程序再次恢复时重新加载它们.最好的方法是什么?请帮忙
目前我添加标记如下:
map.addMarker(new MarkerOptions().position(latlonpoint)
.icon(bitmapDescriptor).title(latlonpoint.toString()));
Run Code Online (Sandbox Code Playgroud)
我知道了!我可以通过将数组点列表保存到文件然后从文件中读回它来轻松完成此操作
我做以下事情onPause:
try {
// Modes: MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITABLE
FileOutputStream output = openFileOutput("latlngpoints.txt",
Context.MODE_PRIVATE);
DataOutputStream dout = new DataOutputStream(output);
dout.writeInt(listOfPoints.size()); // Save line count
for (LatLng point : listOfPoints) {
dout.writeUTF(point.latitude + "," + point.longitude);
Log.v("write", point.latitude + "," + point.longitude);
}
dout.flush(); // Flush stream ...
dout.close(); // ... and close.
} catch (IOException exc) {
exc.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
而且onResume:我反其道而行之
try {
FileInputStream input = openFileInput("latlngpoints.txt");
DataInputStream din = new DataInputStream(input);
int sz = din.readInt(); // Read line count
for (int i = 0; i < sz; i++) {
String str = din.readUTF();
Log.v("read", str);
String[] stringArray = str.split(",");
double latitude = Double.parseDouble(stringArray[0]);
double longitude = Double.parseDouble(stringArray[1]);
listOfPoints.add(new LatLng(latitude, longitude));
}
din.close();
loadMarkers(listOfPoints);
} catch (IOException exc) {
exc.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6527 次 |
| 最近记录: |