如何使用 OpenLayers 区分用户和函数对地图移动的更改
How to differentiate changes in the movement of the map by the user and by a function, using OpenLayers
我有一个简单的功能,用户启动 GPS 和跟踪位置,地图视图跟随他。我想在用户平移地图时停用跟随功能,但保持跟踪位置功能处于激活状态,以便用户可以移动地图并仍然查看他的位置。我尝试使用 map events(moveend
、movestart
、change
),但无论地图如何移动它们都会触发。我还尝试了 click
事件,但是平移地图时不会触发此事件,只有在快速点击时才会触发。
有没有办法检查这种差异?
OpenLayers track position example供参考
我发现了一些与我的问题非常相似的相关问题:1 and 2。
引用第二个问题:
map.on('moveend', function(event) {
var mapView = map.getView(),
moveInitiatedProgrammatically = mapView.get('moveInitiatedProgrammatically') || false;
mapView.unset('moveInitiatedProgrammatically');
// evaluate moveInitiatedProgrammatically's value and behave accordingly...
});
map.getView().set('moveInitiatedProgrammatically', true);
map.getView().setCenter(coord);
It's not ideal for the following reasons:
- Introduces additional state information in the map's view.
- Carelessly replacing the map view will lose that state information.
- Requires setting a property before changing the view state and may be too easily forgotten.
However, it adresses my issue in the meantime.
遵循 , I believe I can differentiate one movement from another using the pointermove
事件中的 Jonatas Walker 解决方案,该事件未被跟踪功能触发。
map.on('pointermove', evt => {
if (evt.dragging){
console.info('dragging');
}
});
使用ol-extol/control/GeolocationBar
。您可以开始新的曲目。当你移动地图时,一个 center
按钮让你回到轨道的中心。
在线查看示例:https://viglino.github.io/ol-ext/examples/mobile/map.control.geolocationbar.html
我有一个简单的功能,用户启动 GPS 和跟踪位置,地图视图跟随他。我想在用户平移地图时停用跟随功能,但保持跟踪位置功能处于激活状态,以便用户可以移动地图并仍然查看他的位置。我尝试使用 map events(moveend
、movestart
、change
),但无论地图如何移动它们都会触发。我还尝试了 click
事件,但是平移地图时不会触发此事件,只有在快速点击时才会触发。
有没有办法检查这种差异?
OpenLayers track position example供参考
我发现了一些与我的问题非常相似的相关问题:1 and 2。
引用第二个问题:
map.on('moveend', function(event) {
var mapView = map.getView(),
moveInitiatedProgrammatically = mapView.get('moveInitiatedProgrammatically') || false;
mapView.unset('moveInitiatedProgrammatically');
// evaluate moveInitiatedProgrammatically's value and behave accordingly...
});
map.getView().set('moveInitiatedProgrammatically', true);
map.getView().setCenter(coord);
It's not ideal for the following reasons:
- Introduces additional state information in the map's view.
- Carelessly replacing the map view will lose that state information.
- Requires setting a property before changing the view state and may be too easily forgotten.
However, it adresses my issue in the meantime.
遵循 pointermove
事件中的 Jonatas Walker 解决方案,该事件未被跟踪功能触发。
map.on('pointermove', evt => {
if (evt.dragging){
console.info('dragging');
}
});
使用ol-extol/control/GeolocationBar
。您可以开始新的曲目。当你移动地图时,一个 center
按钮让你回到轨道的中心。
在线查看示例:https://viglino.github.io/ol-ext/examples/mobile/map.control.geolocationbar.html