2012-07-05 270 views
0

所以我讀了一堆關於閉包問題,我假設這是什麼,但我不知道如何解決它。谷歌地圖添加標記與AJAX

問題:基本上,我只得到1標記,我假設是因爲我使用相同的「標記」變量?但我不確定,也沒有看到解決問題的簡單解決方案。我確信有一些顯而易見的事情,我會喜歡一個真正瞭解Javascript的人的手,而不是那些直到我得到我需要的結果的人。

謝謝!

<script> 
var geocoder; 
var map; 
var markersArray = []; 
var plocation = []; 

function initialize() { 
geocoder = new google.maps.Geocoder(); 
var latlng = new google.maps.LatLng(39.50, -98.35); 
var myOptions = { 
    zoom: 4, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
} 

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

function codeAddress() { 
    //Delete Existing Markers 
    clearOverlays(); 
    deleteOverlays(); 
    //Geocode and Add the New One + Results. 
    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); 
     //add the center marker 
     var patientslocation =results[0].geometry.location; 
     addMarker(patientslocation, "Patient"); 
     //Zoom in on the Region. 
     map.setZoom(10); 
     //Call to Our API 
     $.getJSON("map/search", { provider_type: "01", loc: '"'+patientslocation+'"' },function(data) { 
      //Parse Results 
      var htmlstring = ""; 
      var arraylength = data.length-1; 
      console.log("Result Count (base 0): "+arraylength) 
      $(data).each(function(i,val){ 

       //Build HTML String for Side Bar 
       if (val.name){ 
        htmlstring = htmlstring + "<h3>"+val.name+"</h3>"; 
       } 
       if (val.address){ 
        htmlstring = htmlstring + val.address +"<br/>"; 
       } 
       if (val.phone){ 
        htmlstring = htmlstring +val.phone +"<br/>"; 
       } 
       //Build HTML Strings for Each Marker Hover 

       //Load the Array of Markers 
       plocation[i] = new google.maps.LatLng(val.lat, val.lng); 
       console.log(i +" : " +plocation[i]); 

       if(i === arraylength){ 
        console.log("Load plocations called") 
        load_plocations(); 
       } 
      }); 
      $('#prov_list').html(htmlstring); 

     }); 

} else { 
    alert("Geocode was not successful for the following reason: " + status); 
} 
}); 
} 

function load_plocations(){ 
    $(plocation).each(function(k,v){ 
     console.log("Calling AddMarker: "+v) 
     addMarker(v,k); 
    }); 
} 

function addMarker(location, name) { 
    console.log("Adding Marker: "+location) 
    marker = new google.maps.Marker({ 
     position: location, 
     map: map 
    }); 
    markersArray.push(marker); 
} 
//Clears the Markers from the map. 
function clearOverlays() { 
    console.log("Clearing Overlays"); 
    if (markersArray) { 
     for (i in markersArray) { 
      markersArray[i].setMap(null); 
     } 
    } 
} 

// Deletes all markers in the array by removing references to them 
function deleteOverlays() { 
    console.log("Deleting Overlays"); 
    if (markersArray) { 
     for (i in markersArray) { 
      markersArray[i].setMap(null); 
     } 
     markersArray.length = 0; 
    } 
} 

回答

2

嘗試使用聲明VAR 標記

var marker = new google.maps.Marker({ 

這將使本地範圍內的對象,從而爲每個迭代一個新的。

沒有VAR,它成爲全局(即作用域爲窗口對象)。我不是100%確定的,但我認爲這可能會像你寫的那樣在每次迭代中覆蓋相同的標記,而不是創建一個新標記。

+0

就是這樣。謝謝! – DiscontentDisciple 2012-07-06 14:58:52

1

地理編碼器受速率限制和配額限制。 「動態地」對多個點進行地理編碼以顯示一個網頁從來不是一個好主意,這正是您在循環中試圖做的事情,這隻會對少量標記起作用。這裏是an example (using the v2 API),它對通過AJAX傳輸的xml中的地址列表進行地理編碼。

Here is an article that is part of the google maps API v3 documentation討論地理編碼策略。

最好的解決方案是,如果提前知道點數,將它們離線地理編碼並存儲頁面加載時使用的座標。如果它們是由用戶輸入的,則使用API​​內置的客戶端地理編碼器。

+0

我只是即時對用戶輸入進行地理編碼。其他點從JSON調用返回。因此,每個用戶輸入只會觸發一次地圖api。儘管感謝您的領導。 – DiscontentDisciple 2012-07-06 15:00:01