簡體   English   中英

標記內容(infoWindow)谷歌地圖

[英]Marker content (infoWindow) Google Maps

我正在嘗試將infoWindow添加到Google地圖上的多個標記中。 我最接近的是讓infoWindow在所有標記上顯示數組中可以看到的最后一個地址。 我在下面粘貼的代碼不起作用,我得到一個“Uncaught TypeError:無法讀取未定義的屬性'4'”。 我確定這是一個范圍問題,但我在這里繞圈子可以做一些幫助:

var hotels = [
            ['ibis Birmingham Airport', 52.452656, -1.730548, 4, 'Ambassador Road<br />Bickenhill<br />Solihull<br />Birmingham<br />B26 3AW','(+44)1217805800','(+44)1217805810','info@ibisbhamairport.com','http://www.booknowaddress.com'],
            ['ETAP Birmingham Airport', 52.452527, -1.731644, 3, 'Ambassador Road<br />Bickenhill<br />Solihull<br />Birmingham<br />B26 3QL','(+44)1217805858','(+44)1217805860','info@etapbhamairport.com','http://www.booknowaddress.com'],
            ['ibis Birmingham City Centre', 52.475162, -1.897208, 2, 'Ladywell Walk<br />Birmingham<br />B5 4ST','(+44)1216226010','(+44)1216226020','info@ibisbhamcity.com','http://www.booknowaddress.com']
        ];

        for (var i = 0; i < hotels.length; i++) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(hotels[i][1], hotels[i][2]),
                map: map,
                icon: image,
                title: hotels[i][0],
                zIndex: hotels[i][2]
            });

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

            google.maps.event.addListener(marker, 'click', function () {
                var markerContent = hotels[i][4];
                infoWindow.setContent(markerContent);
                infoWindow.open(map, this);
            });
        }

謝謝你的期待。

我們已經解決了這個問題,雖然我們認為在for之外的addListener沒有任何區別,但似乎。 這是答案:

使用infoWindow中的信息創建一個新函數:

function addInfoWindow(marker, message) {

            var infoWindow = new google.maps.InfoWindow({
                content: message
            });

            google.maps.event.addListener(marker, 'click', function () {
                infoWindow.open(map, marker);
            });
        }

然后使用數組ID和要創建的標記調用該函數:

addInfoWindow(marker, hotels[i][3]);

雖然這個問題已經得到解答,但我認為這種方法更好: http//jsfiddle.net/kjy112/3CvaD/在StackOverFlow google地圖上提取這個問題- 給出坐標打開標記infowindow

每個標記都有一個“infowindow”條目:

function createMarker(lat, lon, html) {
    var newmarker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lon),
        map: map,
        title: html
    });

    newmarker['infowindow'] = new google.maps.InfoWindow({
            content: html
        });

    google.maps.event.addListener(newmarker, 'mouseover', function() {
        this['infowindow'].open(map, this);
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM