Change icon of google map marker when onmouseover div (Google maps v3 api)

前端 未结 3 1685
陌清茗
陌清茗 2021-01-06 13:42

How do i change the icon of a marker on google maps when I mouseover the text in a div? I managed to change the marker icon onmouseover the marker in the map itself using

相关标签:
3条回答
  • 2021-01-06 14:19

    Add a onmouseover property to the div. Let's say it was called changeMarker.

    function changeMarker(marker) {
        var icon = new Google.maps.MarkerImage({ url:"http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=1|ffffff|c41200"});
        marker.setIcon(icon);
    }
    

    Your div could then look like:

    <div onmouseover="changeMarker(marker1)">
    

    I would recommend however caching the MarkerImage (since it seems pretty static) so that Google doesn't need to keep regenerating the graph image.

    You can set other properties of the image. See the documentation

    0 讨论(0)
  • 2021-01-06 14:34
    google.maps.event.addListener(marker1, 'mouseover', function () {
        marker1.setIcon('miniMarker.png');                      
     });
    

    first call initialize function, define marker1 and then use this code, You can also call this function from different ways like you want on div mouse over etc.

    0 讨论(0)
  • 2021-01-06 14:35

    I'm using Chrome. In the console, onmouseover the <div> I get the error:

    Uncaught ReferenceError: marker1 is not defined

    If you set variable like this:

    function a() {
      var marker1 = "foo";
    }
    
    alert(marker1);
    

    marker1 is not accessible at "window" level. You have to write it like this:

    var marker1;
    
    function a() {
      marker1 = "foo";
    }
    
    alert(marker1);
    
    0 讨论(0)
提交回复
热议问题