[英]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.823277
: destination=-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.