在Android Google地图v2上保存标记

6

我正在使用Android Google Maps v2 API,并且已经设置了长按添加标记。我需要一种方法来保存这些标记并在应用程序再次恢复时重新加载它们。最佳的方式是什么?请帮忙。

当前,我是按照以下方式添加标记的:

map.addMarker(new MarkerOptions().position(latlonpoint)
            .icon(bitmapDescriptor).title(latlonpoint.toString()));

我想保存标记。我想在某个地方(我不知道在哪里和如何)仅保存标记位置(纬度/经度值)。 - CrashOverride
我明白了。您想要的是,在A屏幕中设置标记后,您(或用户)离开A屏幕并进入B屏幕。然后当您从B屏幕再次回到A屏幕时,您希望看到A屏幕的状态与之前一样。对吗? - BBonDoo
是的,那完全正确。不仅仅是应用程序的不同屏幕,而且当应用程序退出并且您像第二天再次访问应用程序时也是如此。我应该能够重新获取旧标记。实际上,我已经找到了一个解决方案,我在下面自己发布了。但是,如果您有更好的解决方案,我全听着! - CrashOverride
啊哈,现在我能明白你说的了:不是恢复,而是在应用程序完全销毁后随时“重新启动”您的应用程序。在这种情况下,您下面写的看起来是正确的。更多信息,请参见此“如何保存数据”(http://developer.android.com/intl/ko/training/basics/data-storage/index.html)。 - BBonDoo
3个回答

6
我明白了!我可以通过将点的数组列表保存到文件中,然后再从文件中读取它们来轻松完成此操作。
我在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();
}

我稍微研究了一下,但 onSaveInstanceState 似乎只是一个临时的解决方案。它无法将数据更加永久地存储。 - CrashOverride
1
啊哈,现在我明白你的意思了:不是恢复,而是在应用程序完全销毁后随时“重新启动”您的应用程序。在这种情况下,您上面写的看起来是正确的。更多信息,请参见此数据保存方法:http://developer.android.com/intl/ko/training/basics/data-storage/index.html。 - BBonDoo
@CrashOverride 你好,你需要为此创建任何新的文本文件吗?并且你能发布loadMarkers方法吗? - Lincoln White
1
@lincolnWhite 我不需要创建任何新的文本文件。这段代码会负责创建文本文件。我不再能访问该代码库...这是我之前工作过的一个旧项目。但我相信loadMarkers应该通过遍历标记列表并按以下方式将标记添加到地图中: map.addMarker(new MarkerOptions()。position(latlonpoint) .icon(bitmapDescriptor)。title(latlonpoint.toString())); - CrashOverride
1
这对我来说比SharedPreferences或数据库好多了。代码写得不错,伙计。 - DevOpsSauce
显示剩余7条评论

0

在长按时将纬度和经度保存到数据库中

注意:-忽略地点,不需要它

 HistoryModel historyModel = new HistoryModel(place,sLatitude, sLongitude);
 DatabaseMethods.openDB(MapsActivity.this);
 DatabaseMethods.addHistory(historyModel);
 DatabaseMethods.closeDB();

在 onMapReady 回调函数中
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    DatabaseFunctions.openDB(MapsActivity.this);
    ArrayList<HistoryModel> historyModelArr = DatabaseFunctions.getHistory();
    DatabaseFunctions.closeDB();

    for (int i = 0; i < historyModelArr.size(); i++) {
        double latitude = Double.parseDouble(historyModelArr.get(i).getLatitude());
        double longitude = Double.parseDouble(historyModelArr.get(i).getLongitude());
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude)));
    }
}

使用的方法:

   public static ArrayList<HistoryModel> getHistory() {
    Cursor cursor = null;
    ArrayList<HistoryModel> historyModelArr = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + DBHelper.TABLE_HISTORY,
                null);

        if (cursor != null) {
            historyModelArr = new ArrayList<HistoryModel>();
            while (cursor.moveToNext()) {
                HistoryModel historyModel = new HistoryModel(cursor.getString(1),cursor.getString(2), cursor.getString(3));
                historyModelArr.add(historyModel);
            }
            return historyModelArr;
        } else {
            return historyModelArr;
        }

    } catch (Exception e) {
        Log.e(tag, "getHistory Error : " + e.toString());
        return historyModelArr;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}



public static void addHistory(HistoryModel historyModel) {

    ContentValues values;
    try {
        values = new ContentValues();
        values.put(DBHelper.HISTORY_PLACE, historyModel.getPlace());
        values.put(DBHelper.HISTORY_LATITUDE, historyModel.getLatitude());
        values.put(DBHelper.HISTORY_LONGITUDE, historyModel.getLongitude());
        db.insert(DBHelper.TABLE_HISTORY, null, values);
    } catch (Exception e) {
       
    }

}

模型类

public class HistoryModel {

private String place, latitude, longitude;

public HistoryModel(String place, String latitude, String longitude) {
    this.place = place;
    this.latitude = latitude;
    this.longitude = longitude;
}


public String getPlace() {
    return place;
}

public void setPlace(String place) {
    this.place = place;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
}
}

0
您可以按照以下方式为标记实现onLongClickListener
map.addMarker(new MarkerOptions()
    .position(latlonpoint)
    .icon(bitmapDescriptor)
    .title(latlonpoint.toString()));
map.setOnMapLongClickListener(new OnMapLongClickListener() {
    @Override
    public void onMapLongClick(LatLng p_point) {
        // TODO ...
    }
});

2
我认为你没有正确理解问题。我需要一种保存和重新加载我添加的标记的方法。也就是说,当用户退出应用程序时,它应该保存所有当前的标记,并在下次应用程序恢复时重新加载它们。实现onLongClickListener如何帮助解决这个问题? - CrashOverride
@CrashOverride... Grishu上面的回答是告诉你如何使用正确的ClickListener编写代码。他的答案是我们必须知道的应用onClickListner到代码的基本公式。就这样,我想。 - BBonDoo
@BBonDoo- 我完全同意你的观点。你是对的。非常感谢。我只提供了onLongClickListener的解决方案。 - GrIsHu
1
是的,我理解这是一个实现onLongClickListener的代码。谢谢你提供它,但那不是我需要或要求的。无论如何,我不想太离题。但再次感谢! - CrashOverride

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接