2013-08-30 81 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

相关问题