2012-11-08 15 views
0

如何將地理編碼與已知座標組合?比方說,我想顯示座標數組中的標記。javascript google api:將地理編碼與座標組合

function codeAddress() 
{ 
    var latlng = new google.maps.LatLng(42.745334, 12.738430); 
    var address = document.getElementById("location").value; 

    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 

      var mapOptions = { 
      zoom: 11, 
      center: results[0].geometry.location, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
      } 

     map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location, 
       icon: image_icon 
      }); 

     }   

    }); 

} 

所以我經緯度座標和值的地址,我如何結合這兩種入地圖?

謝謝!

回答

0
function codeAddress() 
{ 

    // The following sets up your array. Most likely you will retrieve the data 
    // from elsewhere, so this is just meant as an illustration. 
    var coords = [];   
    coords.push({lat:100,long:200, caption:'yo'}); 
    coords.push({lat:150,long:250, caption:'yo yo'}); 
    coords.push({lat:200,long:250, caption:'yo ho ho'}); 

    var address = document.getElementById("location").value; 

    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 

      var mapOptions = { 
      zoom: 11, 
      center: results[0].geometry.location, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
      } 

         map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location, 
       icon: image_icon 
      }); 


      var myLatlng; 

      // loop through all the elements in the array, and add markers for each to the map object you've already created. 
      for (i=0;i<coords.length;i++){ 
       myLatlng = new google.maps.LatLng(coords[i].lat,coords[i].lng); 
       marker = new google.maps.Marker({ 
        position: myLatlng, 
        map: map, 
        title:coords[i].caption 
       }); 
      }     


     }  


    }); 

}