2014-03-13 15 views
0

我有一個相對簡單的Google Maps實現。它從瀏覽器請求用戶的地理位置信息並(a)對其進行地理編碼,(b)從數據庫中拉出N個最近的位置記錄(當前在25英里內的最多20個位置),(c)將用戶的位置和位置記錄繪製爲標記,並將關於存儲爲contentString的位置(地址等)的信息在infowindow中查看。如何將緯度/長度值從google maps infowindow傳遞到javascript函數?

Screenshot

我想要做的是通過經/緯度值的位置的JavaScript函數獲得的方向,並繪製他們在地圖上。我的想法是把它們作爲一種形式的contentString的一部分:

<form> 
<input type="hidden" value="' + location[i].latitude + ' name="sitelat"/> 
<input type="hidden" value="' + location[i].longitude + ' name="sitelon"/> 
<input type="button" onClick="GetDirs()" value="Directions"> 
</form> 

這將在函數檢索:

function GetDirs() 
{ 
    var userlatlng = new google.maps.LatLng(userlat, userlon) 

    sitelat = document.getElementById(sitelat); 
    sitelon = document.getElementById(sitelon); 

    var sitelatlng = new google.maps.LatLng(sitelat, sitelon) 

    // continue to get directions from userlatlng to sitelatlng 

} //GetDirs 

有700條記錄在數據庫中,所有20的可能可以在任何給定時間在地圖上填充,所有這些內容在contentString中都有「sitelat」和「sitelon」屬性。

我的心理障礙是如何編寫一個函數,從正確的infowindow中檢索緯度/經度。我錯過了什麼?

+1

你甚至不需要一個形式,只需要創建一個按鈕,並通過緯度和經度參數到onClick函數 – JoseM

回答

2

更改HTML只是

<button 
    onClick="GetDirs(' + location[i].latitude + ', ' + location[i].longitude + ')" 
>Directions</button> 

而且你的功能,現在只需

function GetDirs(sitelat,sitelon) 
{ 
    var userlatlng = new google.maps.LatLng(userlat, userlon); 
    var sitelatlng = new google.maps.LatLng(sitelat, sitelon); 

    // continue to get directions from userlatlng to sitelatlng 

} //GetDirs 
+0

謝謝 - 這有竅門。 – John

相關問題