2013-03-29 125 views
24

在我的域模型,对有问题的单位,我有:地方的位置标记

  • 名称(例如,水石韦克菲尔德)
  • 的街道地址(如61-62 Bishopgate步行)
  • 和邮政代码(例如WF1 1YB)

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

感谢

+0

一个细节博客:HTTP ://goo.gl/d8w1J0 –

+1

@SureshKamrushi你称之为“详细博客”,但它只是一个代码片断,根本没有解释它的任何作用。 – WongKongPhooey

回答

41

试试这个例子:

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> 
+0

感谢您的编辑。因此,[地址]输入将采用逗号分隔的名称,街道和邮政编码? – Ciwan

+0

对不起,我粘贴错误exmpale,看看编辑 – grigno

+0

直接尝试https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple它来自gmaps文档。 – grigno