从名称,地址和邮编位置标记(Place Markers from Name, Address and

2019-08-18 06:04发布

在我的域模型,对有问题的单位,我有:

  • 这个地方的名字(例如水石韦克菲尔德)
  • 的街道地址(如61-62 Bishopgate城)
  • 和邮政代码(例如WF1 1YB)

从上面的三条信息,我怎样才能放置在地图上的标记? 我使用谷歌地图API 3。

谢谢

Answer 1:

试试下面这个例子:

原来这里

HTML

      <body onload="initialize()">
        <div>
          <input id="address" type="text" value="Sydney, NSW">
          <input type="button" value="Geocode" onclick="codeAddress()">
        </div>
        <div id="map-canvas" style="height:90%;top:30px"></div>
      </body>

JS

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


文章来源: Place Markers from Name, Address and Post Code