2015-04-06 181 views
2

我在探索Google Maps Direction API,並注意到我得到一個overview_polylinepoints結果。我可以在此處的「編碼折線」字段中輸入該值:https://developers.google.com/maps/documentation/utilities/polylineutility。結果似乎是從開始位置到結束位置的實際行車路線。數據看起來很適合我。在Google地圖中繪製折線

我想在谷歌地圖中看到這個,但沒有在開始和結束位置之間的所有地圖標記。我如何獲取這些信息並將其繪製在Google地圖上?

+0

只需設置選項'蘇[DirectionsRenderer](https://developers.google.com/maps/documentation/javascript/reference?hl=zh-CN&csw=1#DirectionsRenderer)的「ppressMarkers」設置爲「true」,路線將被標記爲無標記 –

+0

方向請求看起來像?你目前如何顯示它? – geocodezip

回答

5

從谷歌地圖的JavaScript API開發人員指南:Geometry Library: Geometry Encoding

爲了解碼編碼的路徑,只需撥打decodePath()傳遞方法的編碼字符串。

需要說明的是反斜槓編碼的提防(更換任何發生在「\\」與你的編碼路徑字符串「\」,如果你有問題)

example fiddle

代碼片段:

var map; 
 
var bounds = new google.maps.LatLngBounds(); 
 

 
function initialize() { 
 
    map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(34, 108), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    var jsonData = { 
 
    "overview_polyline": { 
 
     "points": "e`[email protected]@[email protected]@[email protected]@v]_}@[email protected]@[email protected]][email protected][email protected][email protected]@~QebBrJq`[email protected]@[email protected]@}}[email protected]@o{Ba]m~EtVewAnBa`@[email protected]}dDufGqwA_|[email protected][email protected]{`Ee]qkB}[email protected]|[email protected]}At]{[email protected]}[email protected]@aeIm^{[email protected]`BkVueH}gDwuXu`[email protected][ybCo^[email protected]@yyAe`@[email protected][email protected]{[email protected]@e_Fq}@o`[email protected]}[email protected]`@[email protected]@[email protected][[email protected]@iiC}[email protected]_fBmuAypFyIiqA}BwyBy`@[email protected]{[email protected][email protected]{[email protected]{@s{[email protected]}[email protected][orAk]wrA_TyzAnFefAa\\guB_OmwAwF{[email protected]}[email protected]}Qo_RgMo|A|[email protected]@u{[email protected]_D{[email protected]@w\\[email protected]@[email protected][email protected]]urAyoD}[email protected]@[email protected]`Bq\\}[email protected]}[email protected]|@tBmlBebB{[email protected]{ConAedC}L}[email protected]@[email protected]}[email protected]}@[email protected]_bBg|@csBm_Bo|BkaC{[email protected]{[email protected]@[email protected]@_}@e`@[email protected]@etA}[email protected][wbBu]}gAuoDeeG{[email protected]@ugAu_L{xSshEe_KieDm|[email protected]{QuUg`BipF{[email protected]}NsvAiyFs}@izFjwAqrGtHkbCeB{[email protected]~CgSk|@mB}[email protected]_BgjFqqC}gH}`CwvG}cA{cB}[email protected]@[email protected][email protected]{uAmrDivDw|E{[email protected]@[email protected]}[email protected]@[email protected][email protected]_kCk}@smDkVg{Bk}@[email protected]_sEaPwdC~KazF{EcpCrEmeGl][email protected]{[email protected][email protected]@[email protected][email protected]@dBvA" 
 
    } 
 
    }; 
 
    var path = google.maps.geometry.encoding.decodePath(jsonData.overview_polyline.points); 
 
    console.log(path); 
 
    for (var i = 0; i < path.length; i++) { 
 
    bounds.extend(path[i]); 
 
    } 
 

 
    var polyline = new google.maps.Polyline({ 
 
    path: path, 
 
    strokeColor: '#FF0000', 
 
    strokeOpacity: 0.8, 
 
    strokeWeight: 2, 
 
    fillColor: '#FF0000', 
 
    fillOpacity: 0.35, 
 
    map: map 
 
     // strokeColor: "#0000FF", 
 
     // strokeOpacity: 1.0, 
 
     // strokeWeight: 2 
 
    }); 
 
    polyline.setMap(map); 
 
    map.fitBounds(bounds); 
 

 
} 
 

 

 
google.maps.event.addDomListener(window, "load", initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script> 
 
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>

+0

謝謝。爲什麼這沒有一個開始和結束標記?我錯看了折線來製作我的地圖嗎?還有什麼我可以做的只是呈現一個簡單的地圖,顯示旅行的開始和結束點? – StackOverflowNewbie

+0

是不是你的問題「沒有在開始和結束位置之間的所有地圖標記」?還是我誤解了? – geocodezip

+0

是的。我不希望所有這些**之間**。不過,我確實希望那些在開始和結束時。基本上,我希望它看起來像我從Google地圖獲取路線時的樣子。 – StackOverflowNewbie