簡體   English   中英

如何通過 GoogleMaps 上的多個點繪制從 A 到 B 的路線?

[英]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.823277destination=-6.235759,106.823277如果所有位置都應該引用雅加達的有效位置。 因此,請仔細檢查為 Directions API 請求編寫 URL 的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM