保存在Android谷歌地图V2标志 [英] Save markers on Android google maps v2
本文介绍了保存在Android谷歌地图V2标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Android的谷歌地图API V2,并将它设置为在长按添加标记。我需要一种方法来保存这些标记和重新加载应用程序再次恢复时。会是怎样做到这一点的最好方法是什么?请帮忙
目前我添加标记如下:
map.addMarker(新MarkerOptions()。位置(latlonpoint)
.icon(bitmapDescriptor).title伪(latlonpoint.toString()));
解决方案
我知道了!我可以很容易地通过点的数组列表保存到一个文件,然后从文件中读取他们回来做
我做了以下的onPause
:
尝试{
//模式:MODE_PRIVATE,MODE_WORLD_READABLE,MODE_WORLD_WRITABLE
的FileOutputStream输出= openFileOutput(latlngpoints.txt
Context.MODE_PRIVATE);
DataOutputStream类DOUT =新DataOutputStream类(输出);
dout.writeInt(listOfPoints.size()); //保存行数
为(经纬度点:listOfPoints){
dout.writeUTF(point.latitude +,+ point.longitude);
Log.v(写,point.latitude +,+ point.longitude);
}
dout.flush(); //冲洗流...
dout.close(); // ...并关闭。
}赶上(IOException异常EXC){
exc.printStackTrace();
}
和 onResume
:我做相反的事情
尝试{
的FileInputStream输入= openFileInput(latlngpoints.txt);
喧嚣的DataInputStream =新的DataInputStream(输入);
INT SZ = din.readInt(); //读取行数
的for(int i = 0; I< SZ;我++){
字符串str = din.readUTF();
Log.v(读,STR);
串[]字符串数组= str.split(,);
双纬度= Double.parseDouble(字符串数组[0]);
双经度= Double.parseDouble(字符串数组[1]);
listOfPoints.add(新经纬度(纬度,经度));
}
din.close();
loadMarkers(listOfPoints);
}赶上(IOException异常EXC){
exc.printStackTrace();
}
I am using Android Google maps v2 API and have it set up to add markers on long click. I need a way to save these markers and reload them when the app resumes again. What will be the best way to do this? Please help
Currently I add markers as follows:
map.addMarker(new MarkerOptions().position(latlonpoint)
.icon(bitmapDescriptor).title(latlonpoint.toString()));
解决方案
I got it! I can easily do this via saving the array list of points to a file and then reading them back from file
I do the following 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();
}
And onResume
: I do the opposite
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();
}
这篇关于保存在Android谷歌地图V2标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文