我有这段代码来显示谷歌地图:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(40.714364, -74.005972),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
var locations = [
['New York', 40.714364, -74.005972, 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png']
];
var marker, i;
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
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,
icon: locations[i][3]
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="googlemap" style="width: 100%; height: 500px;"></div>
<a href="#">Open Info Window</a>
这不是最终的代码,因为我想添加更多标记。
问题是,我需要外部链接来打开标记的信息窗口。
例如:
链接1打开标记1的信息窗口 链接2打开标记2的信息窗口 等...
我需要一个像这样的链接:
<a href="javascript:marker(0)">Open Info Window One</a>
这是我在jsfiddle中的代码: http://jsfiddle.net/fJ4jG/3/
我找到了几个解决方案,但我不知道如何将此代码与我的代码一起使用。 它应该像这样工作: http://www.geocodezip.com/v3_MW_example_map2.html
感谢您的帮助!
答案 0 :(得分:48)
该示例的作用是创建一个存储标记的数组。因此,当创建标记时,它们会被推送到该标记数组。当您单击链接时,您将发送一个索引,该索引具有对数组中标记的引用的函数。
所以JavaScript看起来像这样:
var markers = [];
// The array where to store the markers
function initialize() {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(40.714364, -74.005972),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
var locations = [
['New York', 40.714364, -74.005972, 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png']
];
var marker, i;
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
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,
icon: locations[i][3]
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
// Push the marker to the 'markers' array
markers.push(marker);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
// The function to trigger the marker click, 'id' is the reference index to the 'markers' array.
function myClick(id){
google.maps.event.trigger(markers[id], 'click');
}
在您的HTML a
标记中添加myClick
函数,如下所示:
<a href="#" onclick="myClick(0);">Open Info Window</a>
答案 1 :(得分:10)
如我的例子所示。
添加全局数组以保存对google.maps.Marker对象的引用:
var gmarkers = [];
在创建时将标记推到该数组上
gmarkers.push(marker);
触发所需标记上的“点击”事件:
<a href="javascript:google.maps.event.trigger(gmarkers[0],'click');">Open Info Window</a>
答案 2 :(得分:0)
使用此代码我已经尝试过了
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(locations[i][0]);
infoWindow.open(map, marker);
});