Java 两个位置之间的多段线未捕捉到道路
我试图在安卓系统的谷歌地图上,在两个相距一定距离(比如超过100英里)的位置之间绘制一条平滑的多段线。我一直在使用方向和捕捉到道路API,但由于捕捉到道路API的100个坐标的限制,似乎几乎不可能绘制一条从一个位置到另一个位置的平滑多段线,该多段线遵循道路的平滑轮廓 我已经设法提取了方向的所有坐标,以使用返回的overview_点绘制多段线,并使用中的decode方法对其进行了解码,但地图上绘制的多段线在绝大多数情况下都不会捕捉到道路上。相反,我尝试使用Snap-to-Roads API并设置了100个坐标(允许的最大GPS点)的限制,这些坐标似乎都非常准确地捕捉到了从目的地a到B的道路(如果相距很远,则仅覆盖两个位置之间的一些距离) 基本上,我是否完全遗漏了一些东西,或者是使用从方向API的概览点检索到的GPS点来传播捕捉到道路API的100坐标分配的一种情况,即每XXX米绘制一个坐标 以下是我通过截击请求实现的大部分代码,减去Snap-to-Roads请求,后者实现起来相当简单:Java 两个位置之间的多段线未捕捉到道路,java,android,google-maps,google-polyline,Java,Android,Google Maps,Google Polyline,我试图在安卓系统的谷歌地图上,在两个相距一定距离(比如超过100英里)的位置之间绘制一条平滑的多段线。我一直在使用方向和捕捉到道路API,但由于捕捉到道路API的100个坐标的限制,似乎几乎不可能绘制一条从一个位置到另一个位置的平滑多段线,该多段线遵循道路的平滑轮廓 我已经设法提取了方向的所有坐标,以使用返回的overview_点绘制多段线,并使用中的decode方法对其进行了解码,但地图上绘制的多段线在绝大多数情况下都不会捕捉到道路上。相反,我尝试使用Snap-to-Roads API并设置了
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
}
});
StringRequest StringRequest=新的StringRequest(Request.Method.GET,
"https://maps.googleapis.com/maps/api/directions/json?
原点=开始位置\u此处和目的地=结束位置\u此处和键=API\u键\u此处“,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
JSONObject方向;
试一试{
方向=新的JSONObject(响应);
JSONArray routes=directions.getJSONArray(“routes”);
mCoordinates=newarraylist();
对于(int i=0;i
方向api中的编码点已“捕捉”到道路,因此无需使用任何其他api
使用此代码解析/解码路线中的点:
List<LatLng> pointsList = null;
JSONArray routeArray = obj.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
pointsList = PolyUtil.decode(encodedString);
列表点列表=null;
JSONArray ROUTARRAY=obj.getJSONArray(“路由”);
JSONObject routes=routeArray.getJSONObject(0);
JSONObject overviewPolylines=routes.getJSONObject(“overview_Polylines”);
字符串编码字符串=概览多段线。获取字符串(“点”);
pointsList=PolyUtil.decode(encodedString);
然后绘制多段线:
PolylineOptions options = new PolylineOptions().width(5).color(Color.MAGENTA).geodesic(true);
for (int i = 0; i < pointsList.size(); i++) {
LatLng point = pointsList.get(i);
options.add(point);
}
line = mMap.addPolyline(options);
PolylineOptions options=new PolylineOptions().width(5).color(color.MAGENTA).测地线(true);
对于(int i=0;i
结果是:
方向api中的编码点已“捕捉”到道路,因此无需使用任何其他api 使用此代码解析/解码路线中的点:
List<LatLng> pointsList = null;
JSONArray routeArray = obj.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
pointsList = PolyUtil.decode(encodedString);
列表点列表=null;
JSONArray ROUTARRAY=obj.getJSONArray(“路由”);
JSONObject routes=routeArray.getJSONObject(0);
JSONObject overviewPolylines=routes.getJSONObject(“overview_Polylines”);
字符串编码字符串=概览多段线。获取字符串(“点”);
pointsList=PolyUtil.decode(encodedString);
然后绘制多段线:
PolylineOptions options = new PolylineOptions().width(5).color(Color.MAGENTA).geodesic(true);
for (int i = 0; i < pointsList.size(); i++) {
LatLng point = pointsList.get(i);
options.add(point);
}
line = mMap.addPolyline(options);
PolylineOptions options=new PolylineOptions().width(5).color(color.MAGENTA).测地线(true);
对于(int i=0;i
结果是:
好的,如果有人遇到这个问题或类似的问题,我已经设法解决了这个问题,似乎要在地图上正确地绘制一条多段线(沿着道路的平滑轮廓)的方法是在每个steps对象中使用每个单独的多段线>点编码字符串。我似乎还误读了清楚地说明了每个步骤中的多段线编码字符串对象提供了每个步骤的近似平滑路径,我想其他人可能也错过了 基本上,overview_折线编码字符串仅提供路线的概览,因此不能在位置之间提供平滑路径。对于大部分由直线组成的路线(在英国显然不是很好),它可能是完美的,因此每个人