我正在使用
Android Google地图v2 API并将其设置为在长按时添加标记.我需要一种方法来保存这些标记,并在应用程序再次恢复时重新加载它们.最好的方法是什么?请帮忙
目前我添加标记如下:
map.addMarker(new MarkerOptions().position(latlonpoint) .icon(bitmapDescriptor).title(latlonpoint.toString()));
解决方法
我知道了!我可以通过将数组点列表保存到文件然后从文件中读回它来轻松完成此操作
我执行以下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(); }
并且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(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。