Google maps listener event acts like a click even though it is a mouseover

前端 未结 3 1396
后悔当初
后悔当初 2021-01-25 00:33

I am adding these two google.maps.event.addListener events

google.maps.event.addListener(markerAcademicCenter, \"mouseover\", function (e) {
   markerIconAcademi         


        
3条回答
  •  -上瘾入骨i
    2021-01-25 00:52

    The icon of a marker is not an MVCObject, the API will not observe changes of the icon-properties.

    You must modify the url of the icon and then call setIcon to apply the changes:

    google.maps.event.addListener(markerAcademicCenter, "mouseover", function (e) {
      var icon = this.getIcon();
      icon.url =  'url/to/icon';
      this.setIcon(icon);
    });
    

    But I wouldn't suggest it, when you use the icon for multiple markers changing the url(or other properties) will affect the original icon markerIconAcademicCenter (markers use a reference to the original object). You better create a copy with a modified url:

    google.maps.event.addListener(markerAcademicCenter, "mouseover", function (e) {
      var icon = this.getIcon(),copy={};
         for(var k in icon){
          copy[k]=icon[k];
         }
      copy.url=  'url/to/icon';     
      this.setIcon(copy);
    });
    

提交回复
热议问题