我正在使用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
你爲什麼不顯示,越來越位置信息的代碼。 – epascarello
這是一個錯字,你沒有關閉輸入欄位置? – putvande
@epascarello我已經包含了獲取位置細節的代碼摘錄。 – kurrodu