javascript - 删除/禁用 google.maps.Data 类中功能子集的事件监听器

标签 javascript google-maps-api-3 mouseevent geojson

我正在尝试更改 google.maps.Data 类中要素的图标表示,以便在单击某个要素后,新图标会一直保留到单击另一个要素为止。 . .all 同时保持 mouseovermouseout 事件监听器对其余功能处于事件状态。

除了使单击的功能图标保持为 wx_click.png 之外,我已经完成了所有操作,一旦我将鼠标移开它,样式就会恢复(根据代码)。我不想删除任何其他功能的 mouseoutmouseover 监听器,只是被单击的那个。

/* ~~ Mouseover listener ~~ */
map.data.addListener('mouseover', function(event) {
    map.data.revertStyle();
    map.data.overrideStyle(event.feature, {
        icon: '/img/wx_mo.png', 
        title: 'weather station',
    })
});

/* ~~ Mouseout listener ~~*/
mouseoutEventListener = map.data.addListener('mouseout', function(event) {
    map.data.revertStyle();
});

/* ~~ Click listener ~~ */
map.data.addListener('click', function(event) {

    map.data.overrideStyle(event.feature, {icon: '/img/wx_click.png'});

// do other stuff
});

最佳答案

在点击处理程序中:

将点击的标记存储在一个变量中。 当此变量不等于单击的特征时,恢复存储特征的样式。

在 mouseover/mouseout-handlers 中:

仅当当前特征不等于存储的特征时才更改图标/恢复当前特征的样式。

/* ~~ Mouseover listener ~~ */
map.data.addListener('mouseover', function(event) {
    if(map.get('activeFeature')!=event.feature ){
    map.data.overrideStyle(event.feature, {
        icon: '/img/wx_mo.png'
    });
    }
});

/* ~~ Mouseout listener ~~*/
mouseoutEventListener = map.data.addListener('mouseout', function(event) {
    if(map.get('activeFeature')!=event.feature ){
      map.data.revertStyle(event.feature);
    }
});

/* ~~ Click listener ~~ */
map.data.addListener('click', function(event) {
    var activeFeature=map.get('activeFeature');
    if(activeFeature && activeFeature!=event.feature ){
      map.data.revertStyle(activeFeature);
    }
    map.set('activeFeature',event.feature);
    map.data.overrideStyle(event.feature, {
      icon: '/img/wx_click.png'
    });

// do other stuff
});

演示:

function initialize() {
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 6,
    center: {
      lat: 0,
      lng: 0
    }
  });
  map.data.setStyle({
    icon: 'http://maps.gstatic.com/mapfiles/markers2/marker.png'
  });


  /* ~~ Mouseover listener ~~ */
  map.data.addListener('mouseover', function(event) {
    if (map.get('activeFeature') != event.feature) {
      map.data.overrideStyle(event.feature, {
        icon: 'http://maps.gstatic.com/mapfiles/markers2/icon_green.png'
      });
    }
  });

  /* ~~ Mouseout listener ~~*/
  mouseoutEventListener = map.data.addListener('mouseout', function(event) {
    if (map.get('activeFeature') != event.feature) {
      map.data.revertStyle(event.feature);
    }
  });

  /* ~~ Click listener ~~ */
  map.data.addListener('click', function(event) {
    var activeFeature = map.get('activeFeature');
    if (activeFeature && activeFeature != event.feature) {
      map.data.revertStyle(activeFeature);
    }
    map.set('activeFeature', event.feature);
    map.data.overrideStyle(event.feature, {
      icon: 'http://maps.gstatic.com/mapfiles/markers2/boost-marker-mapview.png'
    });

    // do other stuff
  });

  // Load GeoJSON.
  map.data.addGeoJson({
    "type": "FeatureCollection",
    "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [0, 0]
      }
    }, {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [1, 1]
      }
    }, {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-1, -1]
      }
    }]
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
<div id="map-canvas"></div>

关于javascript - 删除/禁用 google.maps.Data 类中功能子集的事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29760560/

相关文章:

c# - Unity - 如何编写控制台

javascript - 鼠标滚轮使用部分标签滚动到部分。如何?

javascript - 如何从 Aurelia 中的函数更新属性

javascript - Ajax 接收来自服务器的响应并将其保存在变量中以供以后 PhP 使用

ajax - Google Maps API 仅在使用 AJAX 时抛出 "Uncaught ReferenceError: google is not defined"

javascript - 我怎样才能将焦点放回具有较低 z-index 的对象,尝试在 map 顶部创建一系列透明 div

javascript - 在 JavaScript 中使用正则表达式删除最后一个破折号后的单词

javascript - 在新创建的窗口中创建一个 div

google-maps - 使用Google Maps进行地址验证-检查地址

c# - WPF弹出窗口捕获鼠标双击事件