gpt4 book ai didi

javascript - 谷歌地图标记作为链接

转载 作者:数据小太阳 更新时间:2023-10-29 05:43:01 25 4
gpt4 key购买 nike

我有以下代码,但它不起作用,链接只显示在最后一点(阿根廷),有帮助吗?

<div id="map" style="width: 980px; height: 420px;margin:10px 0px 30px;"></div>

<script type="text/javascript">
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(0,0),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var usa = new google.maps.LatLng(37.09024, -95.712891);
var brasil = new google.maps.LatLng(-14.235004, -51.92528);
var argentina = new google.maps.LatLng(-38.416097, -63.616672);

var marker = new google.maps.Marker({
position: usa,
url: '/destinos/exibir/pais_id/3',
title: 'Estados Unidos',
map: map
});

var marker = new google.maps.Marker({
position: brasil,
url: '/destinos/exibir/pais_id/2',
title: 'Brasil',
map: map
});

var marker = new google.maps.Marker({
position: argentina,
url: '/destinos/exibir/pais_id/1',
title: 'Argentina',
map: map
});

google.maps.event.addListener(marker, 'click', function() {
window.location.href = marker.url;
});

</script>

最佳答案

jsfiddle带有修改增强 @hsz 示例的演示:

问题是您已经声明了 3 次标记,并且只在最后声明的标记上附加了点击事件。因此,您必须为 3 个不同的标记声明 3 个不同的名称,并为每个标记附加 onclick 事件。如果你在数组或其他东西中这样做会更好

var markers = [];

markers[0] = new google.maps.Marker({
position: usa,
url: '/destinos/exibir/pais_id/3',
title: 'Estados Unidos',
map: map
});

markers[1] = new google.maps.Marker({
position: brasil,
url: '/destinos/exibir/pais_id/2',
title: 'Brasil',
map: map
});

markers[2] = new google.maps.Marker({
position: argentina,
url: '/destinos/exibir/pais_id/1',
title: 'Argentina',
map: map
});

for ( i = 0; i < markers.length; i++ ) {
google.maps.event.addListener(markers[i], 'click', function() {
window.location.href = this.url; //changed from markers[i] to this
});
}

关于javascript - 谷歌地图标记作为链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5107410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com