【问题标题】:Save markers on Android google maps v2在 Android 谷歌地图 v2 上保存标记
【发布时间】:2013-01-24 04:42:07
【问题描述】:

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

目前我添加标记如下:

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

【问题讨论】:

  • 我想保存标记。我想只在某处保存标记位置(纬度/经度值)(我不知道在哪里以及如何)。
  • 我明白了..你想要的是,在 A 屏幕中设置标记后,您(或用户)离开 A 屏幕并进入 B 屏幕,然后当您再次从 B 回到 A 屏幕时屏幕,您想查看 A 屏幕的状态。对吧??
  • 是的。那是完全正确的。而不仅仅是应用程序的不同屏幕。但是当应用程序退出并且您像第二天一样重新访问该应用程序时也是如此。我应该能够取回旧的标记。我实际上找到了一个解决方案,我将自己贴在下面。但是,如果您有更好的解决方案,我会全力以赴!
  • 啊哈,现在我可以理解您所说的:不是恢复,而是在您的应用完全销毁后随时“重新启动”您的应用。在这种情况下,您在下面写的内容看起来是正确的。更多信息,see this HOW TO SAVE data.

标签: android google-maps google-maps-markers android-maps savestate


【解决方案1】:

我明白了!我可以通过将点的数组列表保存到文件中然后从文件中读取它们来轻松地做到这一点

我做以下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();
}

【讨论】:

  • 我认为你的代码是一个很好的试用,但我认为你最好使用Activity的生命周期,根据Android开发指南这是非常简单的方法。考虑using the Activity lifecycle after reading this Android Development Guide:click here。我想这就是你想要做的,
  • 我调查了一下,但 onSaveInstanceState 似乎是一个临时修复。它不会更永久地存储数据。
  • 啊哈,现在我可以理解您所说的:不是恢复,而是在您的应用完全销毁后随时“重新启动”您的应用。在这种情况下,你上面写的看起来是正确的。更多信息,see this HOW TO SAVE data.
  • @lincolnWhite 我不必创建任何新的文本文件。该代码负责创建文本文件。我不再有权访问代码库......这是我从事的一个较旧的项目。但我相信 loadMarkers 应该遍历标记列表并将标记添加到地图中,如下所示map.addMarker(new MarkerOptions().position(latlonpoint) .icon(bitmapDescriptor).title(latlonpoint.toString()));
  • 这对我来说比 SharedPreferences 或数据库要好得多。不错的代码,伙计。
【解决方案2】:

您可以为标记实现onLongClickListener,如下所示:

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

【讨论】:

  • 我认为您没有正确理解这个问题。我需要一种方法来保存和重新加载我添加的标记。也就是说,当一个人退出应用程序时,它应该保存所有当前标记并在应用程序再次恢复时重新加载它们。实现 onLongClickListener 对此有何帮助?
  • @CrashOverride...Grishu 上面的回答是让你知道如何使用 ClickListener 的正确编写代码。他的答案是我们必须知道的基本公式,以便将 onClickListener 应用于代码。我想就是这样。
  • @BBonDoo- 我完全同意你的看法。你说的对。非常感谢 。我只为 onLongClickListener 提供了解决方案。
  • 是的,我知道这是实现 onLongClickListener 的代码。谢谢你,但这不是我需要或要求的。无论如何,我不想过多地谈论话题。但再次感谢!
【解决方案3】:

先在数据库中长按保存经纬度

注意:-忽略不需要的地方

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

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 2016-10-12
    • 1970-01-01
    • 2013-03-09
    • 2015-03-14
    • 2018-05-26
    • 2012-12-07
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多