Ric*_*ell 1 java android google-maps google-polyline
我试图在 Android 中的 Google 地图上在相距一定距离(例如超过 100 英里)的两个位置之间绘制平滑的折线。我一直在关注本指南和本指南,以使用方向和对齐道路 API,但由于 Snap to Roads API 的坐标限制为 100,似乎几乎不可能从沿着平滑的道路轮廓从一个位置到另一个位置。
我已经设法使用返回的overview_points提取方向的所有坐标来绘制折线,并使用来自PolyUtil API的 decode 方法对其进行解码,但在地图上绘制的折线在绝大多数情况下都没有被捕捉到道路上时间。相反,我尝试使用 Snap to Roads API 并设置了 100 个坐标的限制(允许的最大 GPS 点),这些坐标似乎都非常准确地捕捉到了从目的地 A 到 B 的道路(仅覆盖了两个位置之间的一些距离)如果相距很远)。
基本上,是否有我完全遗漏的东西,或者是想出一些分辨率来使用从 Directions API 中的overview_points检索到的 GPS 点来传播 Snap to Roads API 的 100 个坐标分配,即绘制坐标每 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
}
});
Run Code Online (Sandbox Code Playgroud)
好的,如果有人遇到这个问题或类似问题,我已经设法解决了这个问题,似乎在遵循平滑道路轮廓的地图上正确绘制多段线的方法是使用每个单独的多段线>每个内的点编码字符串步骤对象。似乎我也误读了明确说明每个步骤对象中的折线编码字符串提供每个步骤的近似平滑路径的文档,我想其他人也可能错过了。
基本上,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
}
});
Run Code Online (Sandbox Code Playgroud)