【问题标题】:Hardcode a postal address for use with Geocoding硬编码邮政地址以用于地理编码
【发布时间】:2014-01-26 12:57:15
【问题描述】:

我一直在查看 Google 地图开发网站上的地理编码示例。我找到了这个示例,并希望将邮政编码作为实验硬编码,而不是使用输入框来获取地址(总体目标是让 xml 将邮政地址放在此处)。这只是在“地址”标签内对邮政地址进行硬编码的情况吗?我确实尝试过,但没有高兴。希望这有意义吗?

<script>
    var geocoder;
    var map;

    function initialize() {

        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);

        var mapOptions = {
            zoom: 8,
            center: latlng
        }

        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
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }

        });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
    <div id="panel">
      <input id="address" type="textbox" value="Sydney, NSW">
      <input type="button" value="Geocode" onclick="codeAddress()">
    </div>
    <div id="map-canvas"></div>
</body>

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

【问题讨论】:

  • 你试过 var address = "NY" 吗?

标签: javascript google-maps google-maps-api-3 geocoding


【解决方案1】:

如果我理解的很好,你可以这样尝试:

http://jsfiddle.net/7g7qG/

var geocoder;
var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  codeAddress('London')
}

function codeAddress(address) {
  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
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

【讨论】:

  • 正是我需要的!我不是最擅长解释问题,但你明白了,谢谢!
  • 附加问题,是否可以使用 for 循环而不是硬编码地址?我有一个带有邮政编码的 xml 文件,并且想使用相同的地理编码打印地图上的每个点,这是一种合适的方法吗?
  • 变量可以放在'codeAdress'括号内吗?您提供的链接没有太大帮助,抱歉。 @文图拉
  • stackoverflow.com/questions/5413122/… 尝试搜索“谷歌地图标记很多”
猜你喜欢
  • 1970-01-01
  • 2014-06-08
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多