简体   繁体   English

如何通过 GoogleMaps 上的多个点绘制从 A 到 B 的路线?

[英]How to draw route from A to B through multiple points on GoogleMaps?

Imagine I want to go from A to B through 3 waypoints.想象一下,我想通过 3 个航点从 A 到 B。 I have this code, which forms the Google Maps direction URL:我有这个代码,它构成了谷歌地图的方向 URL:

fun getMapsApiDirectionsUrl(): String {
    val origin = "origin=" + currentLocation.latitude + "," + currentLocation.longitude;
    val waypoints = "waypoints=optimize:true|" + loc1.latitude + "," + loc1.longitude + "|" + loc2.latitude + "," + loc2.longitude + "|" + loc3.latitude + "," + loc3.longitude
    val destination = "destination=" + loc4.latitude + "," + loc4.longitude
    val sensor = "sensor=false"
    val key = "key="+API_KEY;
    val params = "$origin&$waypoints&$destination&$sensor&$key"
    val output = "json"

    val url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + params
    System.out.println("map url: "+url)
    return url
}

Opening the URL on browser gave this:在浏览器上打开 URL 给出了这个:

{ "routes" : [], "status" : "ZERO_RESULTS" } {“路线”:[],“状态”:“零结果”}

But if I only use 1 waypoint (instead of 3 like shown above), the direction will be displayed in 1 big JSON correctly, like this:但是如果我只使用 1 个航点(而不是上面显示的 3 个),方向将正确显示在 1 个大 JSON 中,如下所示:

{ "geocoded_waypoints" : [ { "geocoder_status" : "OK", "place_id" : "ChIJH87yjIXxaS4R5_ww8ZCufeo", "types" : [ "clothing_store", "establishment", "point_of_interest", "store" ] }, { "geocoder_status" : "OK", "place_id" : "ChIJ1wVXzIDxaS4RkyUYgf3ZV0c", "types" : [ "street_address" ] }, { "geocoder_status" : "OK", "place_id" : "ChIJrfOSGBXxaS4Ro595K_5ClCg", "types" : [ "establishment", "hospital", "point_of_interest" ] } ], "routes" : [ { "bounds" : { "northeast" : { "lat" : -6.2399232, "lng" : 106.8161445 }, "southwest" : { "lat" : -6.2684858, "lng" : 106.7929824 } }, "copyrights" : "Map data ©2019 Google", "legs" : [ { "distance" : { "text" : "1.2 km", "value" : 1205 }, "duration" : { "text" : "4 mins", "value" : 254 }, "end_address" : "Jl. Kemang Raya No.54, RT.8/RW.2, Bangka, Kec. Mampang Prpt., Kota Jakarta Selatan, Daerah Khusus Ibukota Jakarta 12730, Indonesia", "end_location" : { "lat" : -6.263443499999999, "lng" : 106.8160131.... {“geocoded_waypoints”:[{“geocoder_status”:“OK”,“place_id”:“ChIJH87yjIXxaS4R5_ww8ZCufeo”,“类型”:[“clothing_store”,“机构”,“point_of_interest”,“store_ge” ]}, : "OK", "place_id" : "ChIJ1wVXzIDxaS4RkyUYgf3ZV0c", "types": [ "street_address" ] }, { "geocoder_status" : "OK", "place_id" : "ChIJrfOSGBX5xaS4Ro5Cg"," “医院”,“point_of_interest”] } ],“路线”:[ {“边界”:{“东北”:{“纬度”:-6.2399232,“lng”:106.8161445},“西南”:{“纬度”: -6.2684858, "lng" : 106.7929824 } }, "copyrights" : "Map data ©2019 Google", "legs": [ { "distance" : { "text" : "1.2 km", "value" : 1205 }, "duration" : { "text" : "4 mins", "value" : 254 }, "end_address" : "Jl. Kemang Raya No.54, RT.8/RW.2, Bangka, Kec. Mampang Prpt., Kota Jakarta Selatan, Daerah Khusus Ibukota Jakarta 12730, Indonesia", "end_location" : { "lat" : -6.263443499999999, "lng" : 106.8160131....

So does it mean I can only use 1 waypoint?那么这是否意味着我只能使用 1 个航点?

There is no way from origin to destination via waypoints set.无法通过设置的航路点从起点到终点。 The second URL:第二个网址:

https://maps.googleapis.com/maps/api/directions/json?origin=-6.2684059,106.8103103&waypoints=optimize:true%7C-6.263459,106.815903%7C-6.240607,106.792831%7C-6.240607,106.792831&destination=-6.235759,6.823277&sensor=false&key=<YOUR_KEY>

contains unreachable destination destination=-6.235759,6.823277 in Atlantic Ocean.包含无法到达的目的地destination=-6.235759,6.823277在大西洋中。 Probably destination should be 106.823277 instead of 6.823277 : destination=-6.235759,106.823277 if all the positions should refer to valid locations in Jakarta.可能 destination 应该是106.823277而不是6.823277destination=-6.235759,106.823277如果所有位置都应该引用雅加达的有效位置。 So, double check code, that compose URL for Directions API request.因此,请仔细检查为 Directions API 请求编写 URL 的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:使用多个点绘制GoogleMaps路线 - Android: draw GoogleMaps road route using many points 如何从标记到另一个标记绘制路线方向? Googlemaps v2 Android - How to draw route directions from a marker to another one ? Googlemaps v2 Android 在点 googlemaps 之间添加路径或绘制路径 - add path or draw path between to points googlemaps 如何在起点和目的地之间绘制多条路线,googlemaps api? - How to draw more than one route, between origin and destination, googlemaps api? android如何在谷歌地图上用中间点绘制从起点到目的地的路线? - how to draw route from start point to destination point with intermediate points on google map in android? 如何绘制从ListView项获取的两个地理位置之间的标记和路线? - How to draw Marker and route between two geo points taken from ListView item? Android:在GoogleMap API v2中绘制从A到B的路线 - Android: draw route from A to B in GoogleMap API v2 如何在Android Java中的ImageView上绘制多个点 - How to draw multiple points on imageview in android java 如何在Google地图上显示完整的路线黑白两点? - How to display complete route b/w two points on google maps? 如何在OSM中的两点之间绘制路径? - How to draw a route path between two points in OSM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM