2014-05-09 32 views
0

我處於一種需要使用帶有多個位置的api v3繪製Google地圖的情況。這些位置可以是像「新德里,印度」或110005這樣的靜態地址字符串,但它們會有多個 - 可以逗號分隔或數組 - 我不知道它們需要如何放置在代碼中。但簡單地說,我想繪製多個位置的gmap,因此給定的地圖一次將具有9或10個位置。怎麼做?你可以給我任何工作代碼在jsfiddle。現在我的代碼來繪製只有一個位置的GMAP(地址,而不是郵政編碼)給出如下 -如何使用帶有多個位置(地址以及郵政編碼)的api v3繪製谷歌地圖?

的JavaScript -

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false"></script> 
<script type="text/javascript"> 
var geocoder; 
var map; 
var marker; 
function initialize() 
{ 
    geocoder = new google.maps.Geocoder(); 
    var myOptions = { 
    zoom: 15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
} 

function codeAddress() 
{ 
    //var address = "Talwara Township, Punjab"; 
    var address = document.getElementById("txtaddress").value; 
    geocoder.geocode({'address': address}, function(results, status){  
     if(status == google.maps.GeocoderStatus.OK) 
     { 
      map.setCenter(results[0].geometry.location); 
      marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
      }); 

      var infowindow = new google.maps.InfoWindow({ 
      content: "New Delhi, India" 
      }); 
      infowindow.open(map, marker); 

      // Click the marker to Zoom to 9 
      google.maps.event.addListener(marker, 'click', function() { 
       map.setZoom(9); 
       map.setCenter(marker.getPosition()); 
      }); 
     } 
    }); 
} 
</script> 

HTML -

<body onLoad="initialize();codeAddress();"> 
<form method="get" action=""> 
<input type="text" id="txtaddress" name="txtaddress" size="30" value="New Delhi, India" /> 
<input type="submit" name="btnsubmit" value="Generate Map" /> 
</form> 
<br /><br /> 
<div id="map_canvas" style="width:500px; height:380px; border:1px solid #AFAFAF;"></div> 
</body> 

回答

0

你在問什麼是Batch GeoCoding將多個地址更改爲地理位置的過程稱爲批處理地理編碼。

Google Maps API is not directly supporting BatchGeocoding. 
By using Google Maps API , We do geocode a single address at a time. 

To acheive BatchGeoCoding, We need to iterate a list of Address.(Not too difficult I think) 

最佳做法:GeoCode Example

BatchGeocode參考:

以下是網上批量地理編碼的網站。有它的參考。

Ref 1Ref 2

Working Demo

工作演示只是概念驗證。它必須在許多方面進行改進。請看看這個參考文獻來產生更好的代碼。

+0

在工作演示中,代碼new google.maps.LatLng(-34.397,150.644);有經度和緯度。但我不知道他們屬於哪個城市?此外,工作演示不會顯示多個位置。 – 3185166

+0

@ user3185166:請嘗試學習代碼。它工作正常。爲了使它工作,至少試着複製並粘貼下面的「madurai:chennai:coimbatore」;這裏COlON(:)是分隔每個城市的分隔符。如果你不知道經度和經度,然後顯示整個地圖。更新了演示中的代碼。 – ArunRaj

相關問題