从名称,地址和邮政编码放置标记 [英] Place Markers from Name, Address and Post Code

查看:120
本文介绍了从名称,地址和邮政编码放置标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  • 地点名称(例如Waterstones Wakefield)

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


从上述三条信息中,我怎样才能在地图上放置一个标记?我正在使用Google Maps API 3。



谢谢 试试

试试这个例子:



原始文件



HTML

 < body onload =initialize()> 
< div>
< input id =addresstype =textvalue =Sydney,NSW>
< input type =buttonvalue =Geocodeonclick =codeAddress()>
< / div>
< div id =map-canvasstyle =height:90%; top:30px>< / div>
< / body>

JS

 < script> 
var geocoder;
var map;
函数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);
}

函数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不成功:'+ status';
}
});
}
< / script>


In my domain model, for the entities in question, I have the:

  • Name of the place (e.g. Waterstones Wakefield)
  • The Street Addresses (e.g. 61-62 Bishopgate Walk)
  • And the Post Code (e.g. WF1 1YB)

From the above three pieces of information, how can I get a Marker placed on the Map? I am using Google Maps API 3.

Thanks

解决方案

Try this example:

HERE THE ORIGINAL

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>

这篇关于从名称,地址和邮政编码放置标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆