关闭 Openlayers 3 地理定位
Turn off Openlayers 3 Geolocation
因此,我在 Openlayers 3.9.0 地图中关闭地理定位功能的想法是有一个切换按钮,单击它会停止跟踪并从地理定位图层中删除该功能
geolocation.setTracking('false');
featuresOverlay.getSource().clear();
然后再次打开它,它会打开跟踪,向地理定位图层添加一个功能,设置其坐标并重新居中地图
geolocation.setTracking('true');
featuresOverlay.getSource().addFeature(positionFeature);
var coordinates = geolocation.getPosition();
positionFeature.setGeometry(coordinates ? new ol.geom.Point(coordinates) : null);
view.setCenter(coordinates);
好吧,这在技术上不算是转动 on/off 地理定位,因为它删除了所有视觉元素,它实际上并没有转动 on/off API。有没有这种可能,或者以上就够了?
当然可以,经过一个小改动。
假设您代码中的 geolocation
引用了 ol.Geolocation
的实例,调用 geolocation.setTracking(false)
将在浏览器地理定位 geolocation
上调用 clearWatch
API。相关代码为here.
但是,setTracking
需要一个布尔值。您发送字符串 'false'
,它被解释为真值(因为它是一个非空字符串)。从 setTracking
参数中删除引号,它应该按预期工作。
因此,我在 Openlayers 3.9.0 地图中关闭地理定位功能的想法是有一个切换按钮,单击它会停止跟踪并从地理定位图层中删除该功能
geolocation.setTracking('false');
featuresOverlay.getSource().clear();
然后再次打开它,它会打开跟踪,向地理定位图层添加一个功能,设置其坐标并重新居中地图
geolocation.setTracking('true');
featuresOverlay.getSource().addFeature(positionFeature);
var coordinates = geolocation.getPosition();
positionFeature.setGeometry(coordinates ? new ol.geom.Point(coordinates) : null);
view.setCenter(coordinates);
好吧,这在技术上不算是转动 on/off 地理定位,因为它删除了所有视觉元素,它实际上并没有转动 on/off API。有没有这种可能,或者以上就够了?
当然可以,经过一个小改动。
假设您代码中的 geolocation
引用了 ol.Geolocation
的实例,调用 geolocation.setTracking(false)
将在浏览器地理定位 geolocation
上调用 clearWatch
API。相关代码为here.
但是,setTracking
需要一个布尔值。您发送字符串 'false'
,它被解释为真值(因为它是一个非空字符串)。从 setTracking
参数中删除引号,它应该按预期工作。