两个位置之间的多段线未捕捉到道路

Polyline between two locations not snapped to road

我正在尝试在 Android 中的 Google 地图上的两个相距一定距离(比如超过 100 英里)的位置之间绘制一条平滑的多段线。在使用方向和捕捉道路 API 方面,我一直在关注这个 guide and also this guide,但是由于捕捉到道路 API 的 100 个坐标的限制,它似乎是几乎不可能沿着道路的平滑轮廓从一个位置到另一个位置绘制一条平滑的多段线。

我已经设法使用返回的 overview_points 提取方向的所有坐标以绘制多段线,并使用 [=] 中的解码方法对其进行解码12=],但地图上绘制的多段线绝大部分时间都没有捕捉到道路。相反,我尝试使用“捕捉到道路”API 并设置 100 个坐标的限制(允许的最大 GPS 点),这些坐标似乎都非常准确地捕捉到从目的地 A 到 B 的道路(仅覆盖一些距离如果相距较远,则在两个位置之间)。

基本上,是不是我完全遗漏了什么,或者是想出一些解决方案,使用从 overview_points 来自方向 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
        }

    });

方向 api 的编码点已经 "snapped" 到道路,因此无需使用任何额外的 API。

使用此代码 parse/decode 路线中的点:

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);

然后绘制折线:

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);

这是结果:

好吧,如果有人遇到这个问题或类似问题,我已经设法解决了这个问题,似乎在地图上正确绘制一条遵循道路平滑轮廓的折线的方法是使用每个人 polyline > point 每个 steps 对象中的编码字符串。我似乎还误读了 documentation,它清楚地说明了每个 步中的折线编码字符串 对象提供了每个步骤的近似平滑路径,我想其他人也可能有错过了。

基本上,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
        }

    });