admin管理员组文章数量:1418417
I would like to change the marker icon, when the user hovers his mouse over a div tag. I found a close solution, where the marker icon changes when the user hovers his mouse over the marker itself. But I would like to change it using div tags.
The code:
var icon1 = "imageA.png";
var icon2 = "imageB.png";
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
id: 1,
icon: icon1,
title: "some marker"
});
google.maps.event.addListener(marker, 'mouseover', function() {
marker.setIcon(icon2);
});
google.maps.event.addListener(marker, 'mouseout', function() {
marker.setIcon(icon1);
});
My set up shall look like this:
<div class="sth" onmouseover="ShowMarker(id)" />
And my JS sth like:
var icon1 = "imageA.png";
var icon2 = "imageB.png";
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
id: 1,
icon: icon1,
title: "some marker"
});
function ShowMarker(id) {
google.maps.event.addListener(marker, 'mouseover', function() {
marker[id].setIcon(icon2);
});
google.maps.event.addListener(marker, 'mouseout', function() {
marker[id].setIcon(icon1);
});
}
This code should change only the selected marker by id. Could anyone change my code to a working one? I would really appreciate it.
Thanks in advance.
I would like to change the marker icon, when the user hovers his mouse over a div tag. I found a close solution, where the marker icon changes when the user hovers his mouse over the marker itself. But I would like to change it using div tags.
The code:
var icon1 = "imageA.png";
var icon2 = "imageB.png";
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
id: 1,
icon: icon1,
title: "some marker"
});
google.maps.event.addListener(marker, 'mouseover', function() {
marker.setIcon(icon2);
});
google.maps.event.addListener(marker, 'mouseout', function() {
marker.setIcon(icon1);
});
My set up shall look like this:
<div class="sth" onmouseover="ShowMarker(id)" />
And my JS sth like:
var icon1 = "imageA.png";
var icon2 = "imageB.png";
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
id: 1,
icon: icon1,
title: "some marker"
});
function ShowMarker(id) {
google.maps.event.addListener(marker, 'mouseover', function() {
marker[id].setIcon(icon2);
});
google.maps.event.addListener(marker, 'mouseout', function() {
marker[id].setIcon(icon1);
});
}
This code should change only the selected marker by id. Could anyone change my code to a working one? I would really appreciate it.
Thanks in advance.
Share Improve this question edited Jul 3, 2015 at 15:05 duncan 31.9k15 gold badges81 silver badges101 bronze badges asked Jul 3, 2015 at 12:55 TiborTibor 992 silver badges11 bronze badges 1- possible duplicate of Change icon of google map marker when onmouseover div (Google maps v3 api) – geocodezip Commented Jul 3, 2015 at 16:05
1 Answer
Reset to default 5You have to make a listener on the div you want :
<div class="sth" onmouseover="hover(1)" onmouseout="out(1)">MARKER 1</div>
<div class="sth" onmouseover="hover(2)" onmouseout="out(2)">MARKER 2</div>
Then push your markers in another variable, and loop on it :
var allMarkers = [];
var marker = new ...();
allMarkers.push(marker);
function hover(id) {
for ( var i = 0; i< allMarkers.length; i++) {
if (id === allMarkers[i].id) {
allMarkers[i].setIcon(icon2);
break;
}
}
}
...
I've done a demo, with ments. Hope it helps :)
本文标签: javascriptChange marker icon on mouseover a divStack Overflow
版权声明:本文标题:javascript - Change marker icon on mouseover a div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745276867a2651238.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论