我想根据位置搜索使用 long 和 lat 填充 2 个输入字段。我尝试了许多不同的方法来添加监听器,但无法让它工作。寻求帮助。
首先是我的 Javascript 和文档负责人
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?&sensor=true">
</script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
google.maps.event.addListener(marker, 'dragend', function (event) {
document.getElementById("latbox").value = this.getPosition().lat();
document.getElementById("lngbox").value = this.getPosition().lng();
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
然后我在这个下的body html如下
<body onload="initialize()" onunload="GUnload()">
<p>
Enter an address, The latitude/longitude will appear in the text fields after each search.
</p>
<p>
<input id="address" type="textbox" value="New York, New York">
<input type="button" value="Get Long and Lat" onclick="codeAddress()">
</p>
<div id="latlong">
<p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
<p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
</div>
<div id="map_canvas" style="width: 600px; height: 400px"></div>
</body>
</html>
我遇到问题的部分是这段代码
google.maps.event.addListener(marker, 'dragend', function (event) {
document.getElementById("latbox").value = this.getPosition().lat();
document.getElementById("lngbox").value = this.getPosition().lng();
});
在我进行搜索后,它不会在我的输入字段中填充任何内容。