[英]How to draw route from A to B through multiple points on GoogleMaps?
想象一下,我想通过 3 个航点从 A 到 B。 我有这个代码,它构成了谷歌地图的方向 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
}
在浏览器上打开 URL 给出了这个:
{“路线”:[],“状态”:“零结果”}
但是如果我只使用 1 个航点(而不是上面显示的 3 个),方向将正确显示在 1 个大 JSON 中,如下所示:
{“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....
那么这是否意味着我只能使用 1 个航点?
无法通过设置的航路点从起点到终点。 第二个网址:
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>
包含无法到达的目的地destination=-6.235759,6.823277
在大西洋中。 可能 destination 应该是106.823277
而不是6.823277
: destination=-6.235759,106.823277
如果所有位置都应该引用雅加达的有效位置。 因此,请仔细检查为 Directions API 请求编写 URL 的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.