【发布时间】:2014-12-11 19:40:50
【问题描述】:
如何使用来自另一个活动的实时数据在地图上添加更多标记并绘制更多折线? 我正在使用地图教程。 但我只能在 oncreate 中添加标记,不能从另一个活动传递实时数据
我怎样才能添加多个标记来定期从另一项活动中获取经纬度和经度信息
【问题讨论】:
标签: android google-maps marker google-polyline
如何使用来自另一个活动的实时数据在地图上添加更多标记并绘制更多折线? 我正在使用地图教程。 但我只能在 oncreate 中添加标记,不能从另一个活动传递实时数据
我怎样才能添加多个标记来定期从另一项活动中获取经纬度和经度信息
【问题讨论】:
标签: android google-maps marker google-polyline
真正取决于如何将数据从其他活动传递到使用 Google Maps v2 API 的活动。我能想到的最简单的方法是使用Gson(自动链接到文档(toJson 方法))
Gson 允许您序列化 Gson 的对象into its equivalent Json representation。根据我的经验,对象不应包含非泛型类型,例如其他对象。这可能是您的标记对象的示例。
class MarkerObject {
public String name = "";
public String snippet = "";
public double lat = 0.0;
public double lng = 0.0;
public String markerImg = "";
public MarkerObject() {}
}
要将其序列化为 Json 字符串并通过 Intent 传递,请执行以下操作:
MarkerObject exampleOfObject = new MarkerObject();
exampleOfObject.name = "Test Marker";
exampleOfObject.snippet = "Description/snipper woooh!";
exampleOfObject.lat = 1.0;
exampleOfObject.lng = 1.0;
Intent intent = new Intent(this, YourMapActivity.class);
intent.putExtra("exampleMarker", new Gson().toJson(exampleOfObject));
startActivity(intent);
并在您的地图活动中通过onCreate() 方法将其读出。
在方法的底部可能如下所示:
Intent intent = getIntent();
String jsonMarker = intent.getStringExtra("exampleMarker", "");
if(jsonMarker != "") {
MarkerObject exampleOfObject = (MarkerObject) new Gson().fromJson(jsonMarker);
MarkerOptions exampleMarkerOptions = new MarkerOptions();
exampleMarkerOptions.title(exampleOfObject.name);
exampleMarkerOptions.snippet(exampleOfObject.snippet);
exampleMarkerOptions.position(new LatLng(exampleOfObject.lat, exampleOfObject.lng));
// Change 'map' into the variable name of your GoogleMap object.
map.addMarker(exampleMarkerOptions);
}
我自己没有测试过,但是根据文档,它应该可以工作。如果您想添加/修改/删除某些内容,请随时编辑此答案。
【讨论】: