关闭Openlayers 3 Geolocation

时间:2015-11-05 17:44:40

标签: geolocation openlayers-3

所以,我在Openlayers 3.9.0地图中关闭Geolocation功能的想法是有一个切换按钮,单击它时会停止跟踪并从地理定位图层中删除该功能

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);

嗯,这在技术上并不算是打开/关闭地理位置,因为它删除了所有的视觉元素,它实际上并没有打开/关闭API。是否存在这种可能性,或者上述情况是否足够?

1 个答案:

答案 0 :(得分:4)

经过一次小改动后确实如此。

假设代码中的geolocation引用ol.Geolocation的实例,调用geolocation.setTracking(false)将在浏览器地理位置API上调用geolocation clearWatch。相关代码为here

但是,setTracking需要一个布尔值。您发送字符串'false',该字符串被解释为真值(因为它是非空字符串)。从setTracking参数中删除引号,它应该按预期工作。

相关问题