27

在我的域模型中,对于有问题的实体,我有:

  • 地点名称(例如 Waterstones Wakefield)
  • 街道地址(例如 61-62 Bishopgate Walk)
  • 和邮政编码(例如 WF1 1YB)

从以上三条信息中,我怎样才能得到一个放置在地图上的标记?我正在使用谷歌地图 API 3。

谢谢

4

1 回答 1

49

试试这个例子:

这里是原版

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>
于 2013-03-29T13:15:24.090 回答