获取GPS地图的GPS坐标

问题描述:

我在我的网站上有一个带有GPS坐标的字段的HTML表单。我如何获得坐标以便我可以在Google地图上绘制它们,并将它们保存到数据库中?获取GPS地图的GPS坐标

+0

确切的坐标是什么?...左上角,中心等。? – Engineer 2012-03-16 20:08:22

+1

你能更清楚地陈述这个问题吗?你是否接受用户输入的坐标?或者你想确定用户的坐标? – 2012-03-16 20:08:42

+0

我zqnt用户点击地图和坐标到形式 – ziz194 2012-03-17 01:13:45

我建议你看一下Google Maps V3 API的Geocoder部分。我自己只是经历了这个问题。当建立表单,包括隐藏输入lat和长(无论在你的数据库的字段名)是这样的:

<input type="hidden" name="data[Location][lat]" id="LocationLat"/> 
<input type="hidden" name="data[Location][lon]" id="LocationLon"/> 

在您的地址输入,你会说这样的事情:

<input name="data[Location][address]" onChange="getLatLong(address)" size="50" maxlength="255" type="text"/> 

您可以在其中包含对称为getLatLong()的JavaScript函数的调用。
这里是下面的函数的例子:

<script type="text/javascript" > 

var geocoder; 
var add = document.getElementById("LocationAddress"); 

function getLatLong(address){ 
geocoder = new google.maps.Geocoder(); 


    geocoder.geocode({ 'address':address},function(results, status){ 

      if (status == google.maps.GeocoderStatus.OK) { 
      document.getElementById("LocationLat").value=results[0].geometry.location.lat(); 
       document.getElementById("LocationLon").value=results[0].geometry.location.lng(); 


      } else { 
      alert("Geocode was not successful for the following reason: " + status); 

} 
    }); 

} 
</script> 

您可能需要摆弄它一下。一定要在这里搜索谷歌地理编码器,因为我发现我的解决方案与我从这个网站直接得到的零件。

+0

形式的地址字段谢谢,帮助很多:)) – ziz194 2012-03-16 21:25:40

+0

不客气。欢迎来到堆栈溢出! – huzzah 2012-03-16 21:26:44

+0

对不起,但我如何添加地图来定位坐标 – ziz194 2012-03-17 01:12:46