Jquery 单击链接时动态更改google地图标记文本
我尝试了许多方法来更改标记中的标签文本,当单击一个链接时,但没有得到任何结果。我有以下代码:Jquery 单击链接时动态更改google地图标记文本,jquery,google-maps,Jquery,Google Maps,我尝试了许多方法来更改标记中的标签文本,当单击一个链接时,但没有得到任何结果。我有以下代码: var map = new google.maps.Map(document.getElementById('googleMap'), { center: {lat: 51.5078788, lng: -0.0877321}, zoom: 16, disableDefaultUI: true, mapTypeId: google.maps.MapType
var map = new google.maps.Map(document.getElementById('googleMap'), {
center: {lat: 51.5078788, lng: -0.0877321},
zoom: 16,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
然后创建自定义标记:
var icons = {
main: {
icon: 'marker.png'
},
locationone: {
icon: 'marker-1.png'
},
locationtwo: {
icon: 'marker-2.png'
}
};
并设置以下功能:
var features = [
{
position: new google.maps.LatLng(51.5078788, -0.0877321),
type: 'main'
}, {
position: new google.maps.LatLng(51.5078788, -0.0877321),
type: 'locationone',
textone: 'default text',
textwo: 'onclick replace with this text',
texthree: 'onclick replace with this text'
}, {
position: new google.maps.LatLng(51.5078788, -0.0877321),
type: 'locationtwo',
textone: 'default text',
textwo: 'onclick replace with this text',
texthree: 'onclick replace with this text'
}
};
然后我在地图上画出它们:
var marker;
var i;
for (i = 0; i < features.length; i++) {
marker = new google.maps.Marker({
position: features[i].position,
icon: icons[features[i].type].icon,
map: map,
label: { color: '#ffffff', fontWeight: 'bold', fontSize: '14px', text: String(features[i].textone) }
});
};
var标记;
var i;
对于(i=0;i
我有三个链接:
<a href="#">Show the label for textone</a>
<a href="#">Show the label for textwo</a>
<a href="#">Show the label for texthree</a>
当我点击上面的一个链接时,我希望它能替换标签文本。这可能吗?我该怎么做?12小时后,我找到了解决方案。我希望这能帮助别人。使用onclick事件,传递标记和特性,但将代码放在return函数中,否则无论您是否单击它,事件都将触发。将下面的代码放在标记器函数下的for循环中:
$(".class-on-ahref").click(function(marker, i){
return function() {
var label = marker.getLabel();
label.text = String(features[i].textwo);
marker.setLabel(label);
}
}(marker, i));
为每个链接创建onclick事件。这是可能的。相关问题:,谢谢您的评论。我需要的是获得一个dom元素来与映射通信。你上面的链接使用的是谷歌的onclick事件,如果我需要点击标记的话,这很好。