简体   繁体   English

OpenLayers - 在特定缩放级别单击标记缩放

[英]OpenLayers - On click marker zoom at certain zoom level

I want to create on click event on the marker which zooms at a specific level upon click.我想在标记上创建单击事件,该事件在单击时放大特定级别。 current code loads marker and upon click zooms to the maximum zoom level of marker.当前代码加载标记并在单击时缩放到标记的最大缩放级别。 I want to add a specific zoom level in code so when click event happens on the marker it should set view at that zoom level .i have raster tiles on the map at zoom level 16 and above so, when I click on the marker it should zoom to raster tiles and hide marker from there.我想在代码中添加一个特定的缩放级别,所以当点击事件发生在标记上时,它应该在那个缩放级别设置视图。我在地图上有缩放级别 16 及以上的光栅图块,所以,当我点击标记时,它应该缩放到光栅图块并从那里隐藏标记。

    var marker = new ol.layer.Vector({
      source: new ol.source.Vector({
          format: new ol.format.GeoJSON(),
          url: "data.js"  
      }),
      style: new ol.style.Style({
        image: new ol.style.Icon({
                anchor: [0.5, 0.5],
                anchorXUnits: 'fraction',
                anchorYUnits: 'pixels',
                scale:0.03,
                src: "img.png"
              })
          })  
    });
    map.addLayer(marker);
    map.on("singleclick", (event) => {        
        const feature = map.forEachFeatureAtPixel(event.pixel, (feature) => {
          return feature;
        });
        if (feature instanceof ol.Feature) {
              map.getView().fit(feature.getGeometry());       
         }
      });

Instead of using fit set the view's center and zoom而不是使用 fit 设置视图的中心和缩放

map.getView().setCenter(ol.extent.getCenter(feature.getGeometry().getExtent()));
map.getView().setZoom(16);

You can "hide" a feature by giving it a null style您可以通过给它一个空样式来“隐藏”一个功能

feature.setStyle(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM