我正在使用 Android Google maps v2 API 并将其设置为在长按时添加标记。我需要一种方法来保存这些标记并在应用程序再次恢复时重新加载它们。最好的方法是什么?请帮忙
目前我添加标记如下:
map.addMarker(new MarkerOptions().position(latlonpoint)
.icon(bitmapDescriptor).title(latlonpoint.toString()));
我正在使用 Android Google maps 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();
}
您可以onLongClickListener
按如下方式实现标记:
map.addMarker(new MarkerOptions()
.position(latlonpoint)
.icon(bitmapDescriptor)
.title(latlonpoint.toString()));
map.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng p_point) {
// TODO ...
}
});
首先在数据库中长按保存经纬度
注意:-忽略不需要的地方
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;
}
}