所以,在花了一些时间之后,我想我会展示我最终是如何做到这一点的,以防其他人需要它或有更好的解决方案。
看来 cordova 在位置对象中有一个内置的“标题”属性。
https://github.com/apache/cordova-plugin-geolocation
var heading = $rootScope.position.heading;
首先,我通过从用户航向中减去 mapBearing(地图从北转向的度数)来确保标记始终指向航向方向,即使用户转动地图也是如此。
map.on('rotate', function(){
map.setLayoutProperty('drone', 'icon-rotate', heading - map.getBearing())
});
我创建一个图标,在用户位置,添加源并添加带有源的图层。
map.on('load', function () {
var point = {"type": "Point", "coordinates": [$rootScope.position.long, $rootScope.position.lat]};
map.addSource('drone', {type: 'geojson', data: point });
map.addLayer({
"id": "drone",
"type": "symbol",
"source": "drone"
}
});
接下来我检查标题是否实际可用,因为它似乎只在用户移动时返回一个值(目前仅在 Android 上测试过),如果是,则更新该点的标题。
if($rootScope.position.heading){
var heading = $rootScope.position.heading;
map.setLayoutProperty('drone', 'icon-rotate', $rootScope.position.heading);
};
最后我更新了点的位置,在“$watch”位置。
map.getSource('drone').setData(point);
这样,即使用户旋转地图,我也可以观察用户的航向,并且该点保持在轨道上。