【发布时间】:2015-04-16 22:18:13
【问题描述】:
我有一个表单,它使用 onsubmit 属性通过 Google Map API 将地址转换为地理编码。因为我知道地理编码是异步的。有时它有效,有时则无效。任何人都可以帮助我吗? 非常感谢!
jsp 文件:
<body>
<form name="addLocation" method="POST" action="addLocation.htm" commandName="location" onsubmit="changeAdress()">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="${location.name}"/></td>
</tr>
<tr>
<td>Adress</td>
<td><input type="text" id="adress" name="adress"/></td>
</tr>
<tr>
<td><input type="hidden" id="geolocation" name="geolocation"/></td>
</tr>
</table>
<input type="hidden" name="id" value="${location.id}"/>
<input type="submit" name="action" value="Save"/>
<input type="submit" name="action" value="Cancel"/>
</form>
</body>
脚本:
function changeAdress() {
var adress = document.getElementById("adress").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': adress}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
document.addLocation.geolocation.value = latitude + " " + longitude;
document.addLocation.submit();
} else alert("geocode failed:" + status);
});
return false;
}
你可以自己尝试一下:http://jbossewss-leuvenspeaks.rhcloud.com/Test/newLocation.htm
【问题讨论】:
标签: javascript jsp google-maps-api-3 geolocation geocode