我的网站上有一个HTML表单,其中包含GPS坐标字段。如何获取坐标以便我可以在Google地图上绘制它们并将它们保存到数据库中?
答案 0 :(得分:1)
我建议您查看Google Maps V3 API的Geocoder部分。我自己刚刚完成了这个问题。构建表单时,包括lat和long的隐藏输入(无论你的数据库中的字段名称是什么),如下所示:
<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>
你可能需要稍微玩一下。一定要在这里搜索谷歌地理编码器,因为我发现我的解决方案是我从这个网站直接得到的。