我试图在 Android 中的 Google map 上相距一定距离(比如超过 100 英里)的两个位置之间绘制一条平滑的多段线。我一直在关注这个 guide还有这个guide在使用 Directions 和 Snap to Roads API 方面,由于来自 Snap to Roads API 的 100 个坐标的限制,似乎几乎不可能绘制一条从一个位置到另一个位置的平滑折线,它遵循平滑的轮廓路。
我已经设法使用返回的 overview_points 提取方向的所有坐标以绘制折线,并使用 PolyUtil API 中的解码方法对其进行解码,但在绝大多数时间里, map 上绘制的多段线并未与道路对齐。相反,我尝试使用 Snap to Roads API 并设置 100 个坐标(允许的最大 GPS 点)的限制,所有坐标似乎都非常准确地捕捉到从目的地 A 到 B 的道路(仅覆盖两个位置之间的一些距离)如果相距很远)。
基本上,是不是我完全遗漏了什么,或者是想出一些解决方案,使用从 overview_points 来自 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
}
});
最佳答案
好吧,如果有人遇到这个问题或类似问题,我已经设法解决了这个问题,似乎在 map 上正确绘制一条遵循道路平滑轮廓的多段线的方法是使用每个单独的多段线> point 每个steps 对象中的编码字符串。看来我误读了 documentation这清楚地说明了每个 steps 对象中的折线编码字符串,提供了每个步骤的近似平滑路径,我想其他人可能也错过了。
基本上,overview_polyline 编码字符串只会提供路线概览,因此不会提供位置之间的平滑路径。它可能非常适合主要由直线组成的路线(显然在英国不太好),因此需要每个单独的折线编码字符串。我刚刚对此进行了测试,它运行完美,根本不需要 Snap to Roads API。
我为解决用例中的问题而修改的代码如下:
StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://maps.googleapis.com/maps/api/directions/json?origin=START_LOCATION&destination=END_LOCATION&key=API_KEY",
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 a = 0; a < routes.length(); a++) {
JSONObject routesObject = routes.getJSONObject(a);
JSONArray legsArray = routesObject.getJSONArray("legs");
for (int b = 0; b < legsArray.length(); b++) {
JSONObject legsObject = legsArray.getJSONObject(b);
JSONArray steps = legsObject.getJSONArray("steps");
for (int c = 0; c < steps.length(); c++) {
JSONObject stepsObject = steps.getJSONObject(c);
JSONObject polyLineObject = stepsObject.getJSONObject("polyline");
mCoordinates.addAll(PolyUtil.decode(polyLineObject.getString("points")));
}
}
PolylineOptions routeCoordinates = new PolylineOptions();
for (LatLng latLng : mCoordinates) {
routeCoordinates.add(new LatLng(latLng.latitude, latLng.longitude));
}
routeCoordinates.width(5);
routeCoordinates.color(Color.BLUE);
Polyline route = mGoogleMap.addPolyline(routeCoordinates);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO handle error
}
});
关于java - 两个位置之间的多段线未捕捉到道路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46479135/