javascript - Change marker icon on mouseover a div - Stack Overflow

I would like to change the marker icon, when the user hovers his mouse over a div tag. I found a close

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
Add a ment  | 

1 Answer 1

Reset to default 5

You 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 :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745276868a4620093.html

相关推荐

  • javascript - Change marker icon on mouseover a div - Stack Overflow

    I would like to change the marker icon, when the user hovers his mouse over a div tag. I found a close

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信