【问题标题】:Add geocode long / lat to input field with event listener使用事件侦听器将地理编码 long / lat 添加到输入字段
【发布时间】:2012-10-19 13:41:03
【问题描述】:

我想根据位置搜索使用 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();
        });

在我进行搜索后,它没有在我的输入字段中填充任何内容。

【问题讨论】:

  • 当然,我明白你的意思。我现在修复了一些我一定错过的......

标签: javascript google-maps google-maps-api-3 latitude-longitude


【解决方案1】:

dragend 监听器中的代码只有在标记被拖动时才会运行。如果您希望在地理编码器返回值时更新这些字段,您需要添加代码来执行此操作:

 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
        });
        document.getElementById("latbox").value = marker.getPosition().lat();
        document.getElementById("lngbox").value = marker.getPosition().lng();

        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);
      }
    });
  }

【讨论】:

  • 感谢您的快速回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 2021-04-17
  • 1970-01-01
  • 2010-10-25
  • 2019-05-18
  • 2021-10-26
相关资源
最近更新 更多