使用Google Maps API(路线)获取中途停留城镇

14

我对Google Maps API(3.0)还不熟悉。我想基于Maps API的方向查询获取途经城市,并且目前尚未找到任何有关此特定主题的文档。

https://developers.google.com/maps/documentation/javascript/directions#Steps

我正在根据A地址和B地址获取方向。我想知道用户在旅程中会经过哪些城市。我找到了一些关于途经点(停靠点)的信息,但这些只涉及每个停留或街道变化。目前为止,我还没有找到任何核心对象...

2个回答

9

我的做法:

  1. 使用RouteBoxer查找包含路径的边界(较小的边界)
  2. 使用Places API返回城市大厅、地方政府办公室或图书馆等类型的地点(也许需要微调),位于这些边界内。
  3. 从这些响应中取出城镇数据,对其进行排序以获取唯一结果,然后使用它们。

概念证明,需要调整位置类型、距离和搜索区域。根据使用情况和路线长度,最好在路上的一些定期间隔点上使用反向地理编码器(100米或1000米),这可能更快,并且不会使用Places API配额。


添加了概念验证。 - geocodezip
这是一个很好的答案,但是如果要处理长路线问题,你会怎么做呢?比如从纽约到蒙特利尔,这是一段374英里的路程,途中几乎没有任何城市可以参考。把它分成374个一英里的点,并且对每个点使用反向地理编码器,这样可行吗? - Eugene
这个概念验证对我从纽约到蒙特利尔都有效。这只是一个概念验证,如果您有其他限制/考虑因素,您可能需要进行修改。如果您对此有疑问,应该提出一个新问题。 - geocodezip
非常感谢您的帮助!我现在会详细研究它,看看是否可以用它来解决我的特定问题。我将关闭我的其他问题,并再次表示感谢! - Eugene

1
我找到了一个替代方案。我们可以使用Here Maps Android GuideHere Maps API request来获取两个点之间的主要城市。我使用了API请求。
API响应返回主要城市的经度和纬度。通过这些数据,我们可以使用Google API的Geocoder来查找城市名称。
Here maps API请求URL将会像这样:
https://route.api.here.com/routing/7.2/calculateroute.json ?app_id={YOUR_APP_ID} &app_code={YOUR_APP_CODE} &waypoint0=geo!52.5,13.4 &waypoint1=geo!52.5,13.45 &mode=fastest;car;traffic:disabled

回应:(route[0].leg[0].maneuver 包含主要要点)

{
"response": {
    "metaInfo": {
        "timestamp": "2019-08-25T11:32:54Z",
        "mapVersion": "8.30.99.156",
        "moduleVersion": "7.2.201933-4954",
        "interfaceVersion": "2.6.66",
        "availableMapVersion": [
            "8.30.99.156"
        ]
    },
    "route": [
        {
            "waypoint": [
                {
                    "linkId": "+842950268",
                    "mappedPosition": {
                        "latitude": 7.6005434,
                        "longitude": 80.0758708
                    },
                    "originalPosition": {
                        "latitude": 7.5999999,
                        "longitude": 80.0769999
                    },
                    "type": "stopOver",
                    "spot": 0.4595336,
                    "sideOfStreet": "neither",
                    "mappedRoadName": "Kuliyapitiya-Hettipola Road",
                    "label": "Kuliyapitiya-Hettipola Road - B243",
                    "shapeIndex": 0,
                    "source": "user"
                },
                {
                    "linkId": "-843192951",
                    "mappedPosition": {
                        "latitude": 7.4799508,
                        "longitude": 80.3700687
                    },
                    "originalPosition": {
                        "latitude": 7.48,
                        "longitude": 80.3699999
                    },
                    "type": "stopOver",
                    "spot": 0.3064516,
                    "sideOfStreet": "right",
                    "mappedRoadName": "",
                    "label": "",
                    "shapeIndex": 313,
                    "source": "user"
                }
            ],
            "mode": {
                "type": "fastest",
                "transportModes": [
                    "car"
                ],
                "trafficMode": "disabled",
                "feature": []
            },
            "leg": [
                {
                    "start": {
                        "linkId": "+842950268",
                        "mappedPosition": {
                            "latitude": 7.6005434,
                            "longitude": 80.0758708
                        },
                        "originalPosition": {
                            "latitude": 7.5999999,
                            "longitude": 80.0769999
                        },
                        "type": "stopOver",
                        "spot": 0.4595336,
                        "sideOfStreet": "neither",
                        "mappedRoadName": "Kuliyapitiya-Hettipola Road",
                        "label": "Kuliyapitiya-Hettipola Road - B243",
                        "shapeIndex": 0,
                        "source": "user"
                    },
                    "end": {
                        "linkId": "-843192951",
                        "mappedPosition": {
                            "latitude": 7.4799508,
                            "longitude": 80.3700687
                        },
                        "originalPosition": {
                            "latitude": 7.48,
                            "longitude": 80.3699999
                        },
                        "type": "stopOver",
                        "spot": 0.3064516,
                        "sideOfStreet": "right",
                        "mappedRoadName": "",
                        "label": "",
                        "shapeIndex": 313,
                        "source": "user"
                    },
                    "length": 40642,
                    "travelTime": 2159,
                    "maneuver": [
                        {
                            "position": {
                                "latitude": 7.6005434,
                                "longitude": 80.0758708
                            },
                            "instruction": "Head <span class=\"heading\">northeast</span> on <span class=\"street\">Kuliyapitiya-Hettipola Road</span> <span class=\"number\">(B243)</span>. <span class=\"distance-description\">Go for <span class=\"length\">604 m</span>.</span>",
                            "travelTime": 52,
                            "length": 604,
                            "id": "M1",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.6054466,
                                "longitude": 80.0782084
                            },
                            "instruction": "Turn <span class=\"direction\">right</span> onto <span class=\"next-street\">Wariyapola-Hettipola Road</span> <span class=\"number\">(B079)</span>. <span class=\"distance-description\">Go for <span class=\"length\">18.9 km</span>.</span>",
                            "travelTime": 910,
                            "length": 18937,
                            "id": "M2",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.6197481,
                                "longitude": 80.2457178
                            },
                            "instruction": "Turn <span class=\"direction\">left</span> onto <span class=\"next-street\">Wariyapola-Hettipola Road</span> <span class=\"number\">(B079)</span>. <span class=\"distance-description\">Go for <span class=\"length\">25 m</span>.</span>",
                            "travelTime": 5,
                            "length": 25,
                            "id": "M3",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.6198554,
                                "longitude": 80.2459216
                            },
                            "instruction": "Take the <span class=\"exit\">3rd exit</span> from Wariyapola Roundabout roundabout onto <span class=\"next-street\">Kurunegala-Padeniya Road</span> <span class=\"number\">(AA010)</span>. <span class=\"distance-description\">Go for <span class=\"length\">19.6 km</span>.</span>",
                            "travelTime": 1039,
                            "length": 19569,
                            "id": "M4",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.4887598,
                                "longitude": 80.3620291
                            },
                            "instruction": "Continue straight ahead. <span class=\"distance-description\">Go for <span class=\"length\">364 m</span>.</span>",
                            "travelTime": 36,
                            "length": 364,
                            "id": "M5",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.4871719,
                                "longitude": 80.3648937
                            },
                            "instruction": "Take the <span class=\"exit\">2nd exit</span> from Kurunegala Roundabout roundabout. <span class=\"distance-description\">Go for <span class=\"length\">192 m</span>.</span>",
                            "travelTime": 21,
                            "length": 192,
                            "id": "M6",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.486732,
                                "longitude": 80.3665352
                            },
                            "instruction": "Take the <span class=\"exit\">3rd exit</span> from A10 roundabout onto <span class=\"next-street\">Kandy Road</span> <span class=\"number\">(A10)</span>. <span class=\"distance-description\">Go for <span class=\"length\">880 m</span>.</span>",
                            "travelTime": 88,
                            "length": 880,
                            "id": "M7",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.4804449,
                                "longitude": 80.3704727
                            },
                            "instruction": "Turn <span class=\"direction\">right</span>. <span class=\"distance-description\">Go for <span class=\"length\">71 m</span>.</span>",
                            "travelTime": 8,
                            "length": 71,
                            "id": "M8",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": 7.4799508,
                                "longitude": 80.3700687
                            },
                            "instruction": "Arrive at your destination on the right.",
                            "travelTime": 0,
                            "length": 0,
                            "id": "M9",
                            "_type": "PrivateTransportManeuverType"
                        }
                    ]
                }
            ],
            "summary": {
                "distance": 40642,
                "trafficTime": 2159,
                "baseTime": 2159,
                "flags": [
                    "builtUpArea"
                ],
                "text": "The trip takes <span class=\"length\">40.6 km</span> and <span class=\"time\">36 mins</span>.",
                "travelTime": 2159,
                "_type": "RouteSummaryType"
            }
        }
    ],
    "language": "en-us"
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接