【发布时间】:2017-09-28 22:24:17
【问题描述】:
我正在尝试在 Android 中的 Google 地图上相距一定距离(例如超过 100 英里)的两个位置之间绘制一条平滑的折线。在使用 Directions 和 Snap to Roads API 方面,我一直在关注这个 guide 和这个 guide,但由于 Snap to Roads API 的坐标限制为 100 个,看起来几乎不可能绘制从一个位置到另一个位置的平滑折线,遵循道路的平滑轮廓。
我已经成功提取了方向的所有坐标以使用返回的 overview_points 绘制折线,并使用PolyUtil API 中的解码方法对其进行了解码,但折线绘制在地图上绝大多数时间都不会被抢购一空。相反,我尝试使用 Snap to Roads API 并设置了 100 个坐标的限制(允许的最大 GPS 点),这些坐标似乎都非常准确地捕捉到从目的地 A 到 B 的道路(仅覆盖两个位置之间的一些距离)如果相距很远)。
基本上,我是否完全遗漏了什么,或者是想出一些解决方案来使用从 overview_points检索到的 GPS 点来传播 Snap to Roads API 的 100 个坐标分配> 来自 Directions API,即每 XXX 米绘制一个坐标。
这是我通过 Volley 请求减去 Snap to Roads 请求实现的大部分代码,后者的实现相当简单:
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://maps.googleapis.com/maps/api/directions/json?
origin=START_LOCATION_HERE&destination=END_LOCATION_HERE&key=API_KEY_HERE",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject directions;
try {
directions = new JSONObject(response);
JSONArray routes = directions.getJSONArray("routes");
mCoordinates = new ArrayList<>();
for (int i = 0; i < routes.length(); i++) {
JSONObject routesObject = routes.getJSONObject(i);
JSONObject overviewPolyline = routesObject.getJSONObject("overview_polyline");
String points = overviewPolyline.getString("points");
List<LatLng> coordinates = new ArrayList<>();
coordinates.addAll(PolyUtil.decode(points));
PolylineOptions routeCoordinates = new PolylineOptions();
for (LatLng latLng : coordinates) {
routeCoordinates.add(new LatLng(latLng.latitude, latLng.longitude));
}
routeCoordinates.width(5);
routeCoordinates.color(Color.BLUE);
Polyline route = mGoogleMap.addPolyline(routeCoordinates);
for (LatLng latLng : coordinates) {
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(latLng.latitude, latLng.longitude)));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO handle error
}
});
【问题讨论】:
-
你为什么要这样做?
String formattedResponse = points.replaceAll("'\\'", "'\'"); -
嗨丹尼尔,根据这个链接developers.google.com/maps/documentation/utilities/…:'请注意,反斜杠被解释为字符串文字中的转义字符。此实用程序的任何输出都应将反斜杠字符转换为字符串文字中的双反斜杠。我尝试了一些组合,这是让它工作的唯一方法之一,除非我完全误读了这个。
-
对不起,我的错误我现在已经删除了这行,没有它似乎也可以工作。但是,问题仍然存在,因为坐标不遵循道路轮廓。
标签: java android google-maps google-polyline