简体   繁体   English

Openlayers - 添加控件:缩放/平移到当前位置

[英]Openlayers - Add control: zoom/pan to current location

I'm trying to add an ol.control button to the map that zooms/pans to my current location.我正在尝试将一个 ol.control 按钮添加到 map 以缩放/平移到我的当前位置。 I've extracted my location coordinates with:我已经提取了我的位置坐标:

var geolocation = new ol.Geolocation({
        projection: map.getView().getProjection(),
        tracking: true
    });
geolocation.getPosition(); //this shows the coordinates (e.g.[591374.2306195896, 6746799.171545821])

Now I want to use these as input for a "zoom to" ol.control button.现在我想将这些用作“缩放到”ol.control 按钮的输入。 As one would use ol.control.ZoomToExtent, which creates a control button.就像使用 ol.control.ZoomToExtent 一样,它会创建一个控制按钮。 eg:例如:

map.addControl(zoomToExtentControl);

zoomToExtentControl = new ol.control.ZoomToExtent({
  extent: [-1013450.0281739295, 3594671.9021477713, 6578887.117336057, 10110775.689402476],
  className: 'custom-zoom-extent',
  label: '🔍'
});

Please help me out:-)请帮帮我:-)

The extent array used for the ZoomToExtent can be dynamically updated with the accuracy extent from the geolocation用于 ZoomToExtent 的范围数组可以根据地理位置的准确范围动态更新

var geolocation = new ol.Geolocation({
        projection: map.getView().getProjection(),
        tracking: true
    });

var extent = ol.extent.createEmpty();

geolocation.on('change:accuracyGeometry', function() {
    geolocation.getAccuracyGeometry().getExtent(extent);
});

zoomToExtentControl = new ol.control.ZoomToExtent({
  extent: extent,
  className: 'custom-zoom-extent',
  label: '🔍'
});

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

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