2013-08-30 82 views
1

我正在使用Google地理位置API來獲取用戶在客戶端的位置並嘗試將其傳遞到服務器端以執行位置查詢。將用戶地理位置從客戶端傳遞到服務器端

我無法獲取用戶位置/ pos變量(lat,lng)到服務器端,因爲只有在用戶接受使用他的當前位置(這是一個實例)後纔會將該值分配給輸入變量(位置)從服務器接收'GET'方法,反之亦然。

我真的很感謝從客戶端到服務器端獲取lat,lng的一些幫助。

下面是我在geolocation.html使用

<form id = "geolocation" action="" method="GET"> 
<input type="text" id = "location" name="location" value=""> 
<input type="submit" /> 
</form> 

這裏的a link到谷歌地理位置API的形式。 以下是獲取用戶地理位置並將其分配給id = location的HTML輸入字段的代碼摘錄,以便我可以在服務器端使用GET方法來獲取地理位置。

if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
    var pos = new google.maps.LatLng(position.coords.latitude, 
            position.coords.longitude); 

    var infowindow = new google.maps.InfoWindow({ 
     map: map, 
     position: pos, 
     content: 'Location found using HTML5.' 
    }); 

    // the below code has been inserted to assign a JS variable to HTML field called 'location' 
document.getElementById('location').value = pos 
+0

你爲什麼不顯示,越來越位置信息的代碼。 – epascarello

+0

這是一個錯字,你沒有關閉輸入欄位置? – putvande

+0

@epascarello我已經包含了獲取位置細節的代碼摘錄。 – kurrodu

回答

0

您必須添加成功/錯誤回調觸發一個事件當用戶接受,分享他們的位置:

navigator.geolocation.getCurrentPosition(success, error); 
function success(position){ 
    var lat = position.coords.latitude; 
    var lon = position.coords.longitude; 
    document.getElementById('location').value = lat + ',' + lon; 
} 

function error() { 
    // do something with the error message 
} 
+0

我已添加屬於地理位置API的觸發器。這裏是[鏈接](https://developers.google.com/maps/documentation/javascript/examples/map-geolocation)到Google地理位置API。 – kurrodu

相關問題