2014-01-23 114 views
0

我正在使用Google Maps API v3地理編碼服務。地理編碼部分正常工作。我在文本框中輸入一個郵政編碼,然後點擊提交按鈕。然後在我輸入文本框的地方顯示藍色標記。然後它加載半徑20公里的地方,並用紅色標記顯示。這一切都很好。但後來我在infowindow上遇到了一些問題。Google地圖infowindow無法在標記點擊上工作

如果我點擊標記(紅色或藍色),它什麼都不會發生。我無法弄清楚。我使用這個代碼infowindow:

$.ajax({ 
    url: 'data.php', 
    type: 'POST', 
    data: {xmldata: jqXHR.responseText}, 
    success: function(locations, textStatus, jqXHR){ 

     var split = locations.split(","); 

     var checkForDouble = split.filter(function(elem, pos) { 
      return split.indexOf(elem) == pos; 
     }); 

    for(var i = 0; i < split.length; i++){ 
     geocoder.geocode(
      {'address': checkForDouble[i]}, 
      function(results, status){ 
       if(results != null){ 
        marker = new google.maps.Marker({ 
         map: map, 
         position: results[0].geometry.location 
        }); 
       } 
      } 
     ); 

     var infocontent = checkForDouble[i];           
     var infowindow = new google.maps.InfoWindow(); 

     google.maps.event.addListener(marker, 'click', (function(marker, infocontent) { 
      return function(){ 
       infowindow.setContent(infocontent); 
       infowindow.open(map, marker); 
      } 
     })(marker, infocontent)); 
    }         
} 

有人可以給我一個提示,我犯了錯誤嗎?

+1

一個猜測,因爲沒有其他代碼。 1. infocontent是空的/未定義的2.(最有可能的),因爲beocode是異步你的標記和infowindow沒有正確連接。在標記創建後,將您的事件偵聽器和兩行infowindow移至地理編碼功能。 –

+0

同意你。在我看來第二個選項是正確的 – szpic

+1

我測試了它。 infocontent不是未定義的。你的第二個解決方案是指我應該在關閉'marker = new google.maps.Marker({'? – Therk

回答

1

您還需要在地理編碼調用上關閉函數。

var infowindow = new google.maps.InfoWindow(); 

function createMarker(address) 
    var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location 
       }); 
    var infocontent = address;           

    google.maps.event.addListener(marker, 'click', function() { 
      infowindow.setContent(infocontent); 
      infowindow.open(map, marker); 
    }); 
    return marker; 
} 
function geocodeAddress(address) { 
    geocoder.geocode(
     {'address': address, 
     function(results, status){ 
      if((results != null) && (status == google.maps.GeocoderStatus.OK)) { 
       var marker = createMarker(address); 
      } else alert("geocode failed on "+address+", status="+status); 
     } 
    ); 
} 

$.ajax({ 
    url: 'data.php', 
    type: 'POST', 
    data: {xmldata: jqXHR.responseText}, 
    success: function(locations, textStatus, jqXHR){ 

     var split = locations.split(","); 

     var checkForDouble = split.filter(function(elem, pos) { 
      return split.indexOf(elem) == pos; 
     }); 

    for(var i = 0; i < split.length; i++){ 
     geocodeAddress(checkForDouble[i]); 
    }        
} 
+0

')之後立即使用infowindow函數。是否可以在不更改結構的情況下進行操作?:) – Therk

0

您必須在聽衆周圍使用closure,因爲它將在未來使用您的參數。你必須將數據傳遞給匿名函數。 通過此代碼,您可以獲得您的結構。

$.ajax({ 
    url: 'data.php', 
    type: 'POST', 
    data: {xmldata: jqXHR.responseText}, 
    success: function(locations, textStatus, jqXHR){ 

     var split = locations.split(","); 

     var checkForDouble = split.filter(function(elem, pos) { 
      return split.indexOf(elem) == pos; 
     }); 

    for(var i = 0; i < split.length; i++){ 
     geocoder.geocode(
      {'address': checkForDouble[i]}, 
      function(results, status){ 
       if(results != null){ 
        marker = new google.maps.Marker({ 
         map: map, 
         position: results[0].geometry.location 
        }); 
       } 
      } 
     ); 

     (function (infocontent){ 
      google.maps.event.addListener(marker, 'click', function() { 
        infowindow = new google.maps.InfoWindow(); 
        infowindow.setContent(infocontent); 
        infowindow.open(map, marker); 
       } 
      }); 
     })(checkForDouble[i]) 

    }         
} 
相關問題