Save markers on Android google maps v2

女生的网名这么多〃 提交于 2019-12-12 08:09:43

问题


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()));

回答1:


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();
}



回答2:


You can implement the onLongClickListener for the marker as below :

map.addMarker(new MarkerOptions()
    .position(latlonpoint)
    .icon(bitmapDescriptor)
    .title(latlonpoint.toString()));
map.setOnMapLongClickListener(new OnMapLongClickListener() {
    @Override
    public void onMapLongClick(LatLng p_point) {
        // TODO ...
    }
});


来源:https://stackoverflow.com/questions/14494102/save-markers-on-android-google-maps-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!