2013-07-28 75 views
0

我試圖使用谷歌地圖實用程序插件(InfoBox)爲谷歌地圖標記添加信息框。我面臨着定義內容價值的問題。我所得到的是一個「未定義」,而不是實際的文本。谷歌地圖信息框內容「未定義」錯誤

請幫我看看這個問題。謝謝。

<script> 

var addressArray = ['Beatrice, US', '1790 Inman Valley', 'Connaught Place, New Delhi']; 
var userInfox = ['My Name', 'Shannell ', 'XtraWize']; 

var geocoder = new google.maps.Geocoder(); 
var markerBounds = new google.maps.LatLngBounds(); 

function mapInit() { 

    var originLat = '54'; 
    var originLong = '-2'; 
    var latlng = new google.maps.LatLng(originLat, originLong); 

    var mapOptions = { 
     zoom: 2, 
     center: latlng, 
     navigationControl: true, 
     mapTypeControl: false, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    addMarkers(); // add the markers and info windows to the map 

} 


function addMarkers() { 
    for (var i = 0; i < addressArray.length; i++) { 

     geocoder.geocode({ 'address': addressArray[i] }, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var marker = new google.maps.Marker({ 
         map: map, 
         position: results[0].geometry.location 
         }); 
       markerBounds.extend(results[0].geometry.location); 
       map.fitBounds(markerBounds); 

       var boxText = document.createElement('div'); 
       boxText.style.cssText = 'border: 1px solid black; background: #DDD; padding: 5px;'; 
       boxText.innerHTML = '<bold>'+ userInfox[i] +' &mdash; Random Text</bold>'; 

       var boxOptions = { 
        content: boxText, 
        disableAutoPan: false, 
        maxWidth: 0, 
        pixelOffset: new google.maps.Size(-140, 0), 
        zIndex: null, 
        boxStyle: { 
         background: 'url(tipbox.gif) no-repeat', 
         opacity: 0.75, 
         width: '280px' 
        }, 
        closeBoxMargin: '2px 2px 2px 2px', 
        closeBoxURL: 'http://www.google.com/intl/en_us/mapfiles/close.gif', 
        infoBoxClearance: new google.maps.Size(1, 1), 
        isHidden: false, 
        pane: 'floatPane', 
        enableEventPropagation: false 
       }; 

       var ib = new InfoBox(boxOptions); 
       google.maps.event.addListener(marker, 'click', function (e) { 
        ib.open(map, this); 
       }); 

      } 
     }); 
    } 
} 


google.maps.event.addDomListener(window, 'load', mapInit); 

</script> 

在userInfox [I]上面的代碼中,對於所有標記,i的值爲「3」。不知道爲什麼?

+0

您的代碼似乎是正確的...但檢查它是否指的是'i'值正常......可能是[通過XML谷歌地圖信息窗口多個地址]其嵌套.. –

+0

可能重複(http://stackoverflow.com/questions/13278368/google-map-info-window-multiple-addresses-via-xml)([使用函數閉包的工作示例](http://www.geocodezip.com/v3_SO_simpleMap_InfoBoxUndefinedContentA。 html)) – geocodezip

+0

我找不到問題。 「i」的值對addressArray [i]有效,但對於引用用戶列表的其他數組則失敗。 – xtrawize

回答

0

下面是解決方案。我沒有保存「我」的價值。所以我總是以循環中的最後一個值結束。

function addMarkers() { 
for (var i = 0; i < addressArray.length; i++) { 

(function(j) { 

geocoder.geocode({ 'address': addressArray[j] }, function(results, status) { 
if (status == google.maps.GeocoderStatus.OK) { 
     var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
     }); 
markerBounds.extend(results[0].geometry.location); 
map.fitBounds(markerBounds); 
var boxText = document.createElement('div'); 
boxText.style.cssText = 'border: 1px solid black; background: #DDD; padding: 5px;'; 
boxText.innerHTML = j + '<strong>'+ userInfox[j] + ' &mdash; ' + addressArray[j]; 

var boxOptions = { 
    content: boxText, 
    disableAutoPan: false, 
    maxWidth: 0, 
    pixelOffset: new google.maps.Size(-140, 0), 
    zIndex: null, 
    boxStyle: { 
    background: 'url(tipbox.gif) no-repeat', 
    opacity: 0.75, 
    width: '280px'}, 
    closeBoxMargin: '2px 2px 2px 2px', 
    closeBoxURL: 'http://www.google.com/intl/en_us/mapfiles/close.gif', 
    infoBoxClearance: new google.maps.Size(1, 1), 
    isHidden: false, 
    pane: 'floatPane', 
    enableEventPropagation: false 
    }; 

    var ib = new InfoBox(boxOptions); 
    google.maps.event.addListener(marker, 'click', function (e) { 
    ib.open(map, this); 
    }); 
    } 
    }); 
}) (i); 
} 
}