2013-05-31 74 views
1

當我點擊地圖以外的元素時,如何觸發「infoWindow」?在地圖之外觸發Google地圖標記

我有這樣的:

var locations = [ 
    ['Location 1', 59.917224,10.587052, 1], 
    ['Location 2', 59.912665,10.726153, 2] 
]; 

var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 9, 
    center: new google.maps.LatLng(59.913869,10.752245), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

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

var marker, i; 

for (i = 0; i < locations.length; i++) { 
    marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
    map: map 
    }); 

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
    return function() { 
     infowindow.setContent(locations[i][0]); 
     infowindow.open(map, marker); 
    } 
    })(marker, i)); 
} 

並具有HTML:

<ul> 
<li>Trigger Marker 1</li> 
<li>Trigger Marker 1</li> 
</ul> 

什麼好的建議?謝謝!

UPDATE

製造這裏的代碼的更新:

變種位置= [ [ '位置1',59.917224,10.587052,1], [ '位置2',59.912665, 10.726153,2] ];

map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 9, 
    center: new google.maps.LatLng(59.913869,10.752245), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

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

var marker, i; 

var markerMap = {} 

for (i = 0; i < locations.length; i++) { 
    markerMap[ "marker-" + i ] = marker 
    marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
    map: map 
    }); 

$(".marker-list").on('click', function() { 
var id = $(this).find('li').attr('data-id'); 
alert(id) 
infoWindow.open(map, markerMap[ id ]); 
}); 

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
    return function() { 
     infowindow.setContent(locations[i][0]); 
     infowindow.open(map, marker); 
    } 
    })(marker, i)); 
} 

HTML:

<ul class="marker-list"> 
<li data-id="marker-1">Trigger Marker 1</li> 
<li data-id="marker-2">Trigger Marker 1</li> 
</ul> 

回答

2

嘗試把標記來標記的具有任何id字段內部循環的對象例如:

//before loop declare object 
var markerMap = {} 
... 
//inside loop add marker to object with uuid (e.g mareker-1) 
markerMap[ "marker-" + i ] = marker 

然後同一ID PARAM添加到您的HTML標記

<ul class="marker-list"> 
    <li data-id="marker-1">Trigger Marker 1</li> 
    <li data-id="marker-2">Trigger Marker 2</li> 
</ul> 

最後(使用jQuery e.g)活動添加到li元素

$(".marker-list").click(function() { 
    var id = $(this).attr('data-id'); 
    infoWindow.open(map, markerMap[ id ]); 
}); 

我沒有測試此代碼。它只是一個想法。我希望你知道我在我的腦海

+0

感謝帕維爾!我明白你的主意!但無論如何我都無法工作,但我上面做了一個更新,你能看到有什麼可以改變的地方嗎? – Kim

+0

您是否已經測試了此評論下的修補程序? – Pawel

0

保存每一個陣列,然後在裏點擊剛剛使用corresponing標記從陣列

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

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
    return function() { 
     infowindow.setContent(locations[i][0]); 
     infowindow.open(map, marker); 
    } 
    })(marker, i)); 
} 
1

在此基礎上更新您的代碼:

markerMap[ "marker-" + i ] = marker 

這應該打開「標記1」的信息窗口:

google.maps.event.trigger(markerMap["marker-1"], "click");