0

我有一份我想要在Google地圖上繪製的學校列表。我使用Google's Geocoding Service查找給定郵政編碼的lng/lat,一旦成功檢索到此信息,我想放棄一個標記,並添加適當的事件偵聽器,以便在單擊給定標記時打開信息框。使用Google Maps API v3進行地理編碼 - 將原始請求鏈接到響應

當我向地理編碼器提出請求時,它在學校的上下文中,當我收到回調時,我失去了上下文。您將從下面的代碼中看到,我已經想出了一個笨拙的解決方案,儘管當geocoder結果截斷郵編時偶爾會失敗。

我應該使用類似jQuery's Deferred Object的東西來解決這個問題嗎?

var geocoder; 
var map; 
var infowindow 
var iterator = 0; 
geosearch = new Array(); 

function drop() { 
    for (var i = 0; i < schools.length; i++) { 
    setTimeout(function() { // delay added to prevent being throttled 
     addMarker(); 
     iterator++; 
    }, i * 1000); 
    } 
} 

function addMarker() { 
    address = schools[iterator].addresses[0].address.zip; 
    geosearch[address] = schools[iterator]; // this is how I'm keeping track of initial request 

    geocoder.geocode({ 'address': address }, function(results, status) { 
    var school = geosearch[results[0].address_components[0].short_name]; // loading the school associated with the initial request, which only works if the postcode completely matches up - clunky! 

    if (status == google.maps.GeocoderStatus.OK) { 

     // each school has tags, I want to set a marker if certain tags exist 
     if ($.inArray('D', school.tags) > 0) { 
     var image = 'map_markers/brown_MarkerD.png'; 
     } else if ($.inArray('C', school.tags) > 0) { 
     var image = 'map_markers/red_MarkerC.png'; 
     } else if ($.inArray('B', school.tags) > 0) { 
     var image = 'map_markers/yellow_MarkerB.png'; 
     } else if ($.inArray('A', school.tags) > 0) { 
     var image = 'map_markers/green_MarkerA.png'; 
     } else { 
     var image = 'map_markers/blue_MarkerZ.png'; 
     } 

     // add the marker to the map, using result 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location, 
      draggable: false, 
      icon: image, 
      shadow: 'http://www.google.com/mapfiles/arrowshadow.png', 
      animation: google.maps.Animation.DROP 
     }); 

     // adds listening on marker so that popup box appears when clicked 
     google.maps.event.addListener(marker, 'click', (function(marker, school) { 
     return function() { 
      infowindow.setContent(
      '<a href="https://vitalcpd.highrisehq.com/companies/'+school.id+'" target="_blank">'+school.name+'</a>' 
      +'<address>' 
      +school.addresses[0].address.street+'<br />' 
      +school.addresses[0].address.city+'<br />' 
      +school.addresses[0].address.state+'<br />' 
      +school.addresses[0].address.zip+'<br />' 
      +school.addresses[0].address.country+'<br />' 
      +'</address>'); 
      infowindow.open(map, marker); 
     } 
     })(marker, school)); 

    } else { 
     console.log("* NOT found: " + status); 
    } 
    }); 
} 

function initialise() { 
    geocoder = new google.maps.Geocoder(); 
    infowindow = new google.maps.InfoWindow(); 
    var latlng = new google.maps.LatLng(54.82659788452641,-3.417279296874991); 
    var mapOptions = { 
    zoom: 6, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

    drop(); // loops through schools to add marker 
} 

回答

0

我在這裏遇到的問題只是一個範圍問題,特別是我在addMarker()函數中引用學校的方式。我不是通過使用全局iterator變量引用schools數組中的學校,而是通過這個學校,這樣正確的學校總是在此範圍內創建的回調中引用。

var geocoder; 
var map; 
var infowindow 
var iterator = 0; 

function drop() { 
    for (var i = 0; i < schools.length; i++) { 
    setTimeout(function() { 
     addMarker(schools[iterator]); // pass in the school as an argument 
     iterator++; 
     $('#current_school').text(iterator); // taken this out of addMarker() 
    }, i * 1000); 
    } 
} 

function addMarker(school) { 
    geocoder.geocode({ 'address': school.addresses[0].address.zip }, function(results, status) { 
    ... // the inners from here remain the same 
    }); 
} 
1

我建議離線地址解碼並將座標存儲到數據庫(或存儲地址的任何地方)。然後使用座標顯示標記。

我也建議審查this article on geocoding strategies from the documentation

要回答你的問題,我建議使用javascript函數閉包與回調函數的地址相關聯。

+0

感謝您的回答。我同意緩存座標會更明智。目前,我更關心將回撥響應與我用來發起地理編碼請求的學校相關聯。我知道這與範圍/封閉有關,並且專門針對這一點。我想知道jQuery Deferred是否是一個組織這個範圍的框架?對於提供回調的非阻塞請求,解決方案必須是通用的。 – 2012-07-24 08:22:40

+0

函數閉包(geocodeAddress函數接受地址和任何其他所需信息並將它們保存在函數閉包中)是我過去使用過的解決方案,並建議其他人解決此問題。 jQuery Deferred似乎不會有幫助(除非它真的減緩了地理編碼過程),但我對此一無所知。 – geocodezip 2012-07-24 12:24:47

+0

我實際上並不需要使用閉包,它實際上比這更簡單,正如我在答案中詳細描述的那樣。 – 2012-07-26 14:52:59

相關問題