简体   繁体   English

Mapbox GL JS 轴承

[英]Mapbox GL JS Bearing

Is it possible in Mapbox GL JS to get the users bearing? Mapbox GL JS 是否有可能让用户承受? I would like to show the direction in which the user is facing, to assist them in navigating to nearby POI.我想显示用户面对的方向,以帮助他们导航到附近的 POI。 I understand that it is possible to set the bearing of the map and also get the current bearing of it, but i need the actual real life bearing of the user.我知道可以设置地图的方位并获取它的当前方位,但我需要用户的实际生活方位。 Kind of the same thing as on Google Maps:与谷歌地图上的类似: 在此处输入图片说明

The service is intended to run as an Ionic app on iOS and Android, and the assistance in bearing is a key feature in helping them locate nearby POI on a well populated map.该服务旨在作为 iOS 和 Android 上的 Ionic 应用程序运行,方位帮助是帮助他们在人口众多的地图上定位附近 POI 的关键功能。

You can get the user's bearing (if their device has such a sensor) by obtaining a Coordinates object from Gelocation#getCurrentPosition() and reading Coordinates#heading .您可以通过从Gelocation#getCurrentPosition()获取Coordinates对象并读取Coordinates#heading来获取用户的方位(如果他们的设备有这样的传感器)。

Mapbox GL JS has no built-in user interface for displaying a user's heading. Mapbox GL JS 没有用于显示用户标题的内置用户界面。 Building your own user interface is easy.构建您自己的用户界面很容易。 See this example which uses the symbol-rotation property .请参阅此示例,该示例使用symbol-rotation属性

So, after some time spend on this, i thought I'd show how i ended up doing this, in case someone else needs it or have a better solution.因此,在花了一些时间之后,我想我会展示我最终是如何做到这一点的,以防其他人需要它或有更好的解决方案。

It seems cordova has a built in "heading" property in the position object.似乎cordova在位置对象中有一个内置的“标题”属性。 https://github.com/apache/cordova-plugin-geolocation https://github.com/apache/cordova-plugin-geolocation

var heading = $rootScope.position.heading;

First, i make sure that the marker is always pointing in the heading direction, even when the user turns the map, by subtracting the mapBearing(degrees the map has turned from North), from the user heading.首先,我确保标记始终指向航向方向,即使用户转动地图,通过从用户航向减去 mapBearing(地图从北转向的度数)。

map.on('rotate', function(){
    map.setLayoutProperty('drone', 'icon-rotate', heading - map.getBearing())
});

I create an icon, at the users position, add the source and add the layer with the source.我在用户位置创建一个图标,添加源并添加带有源的图层。

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

Next i check that heading is actually available, since it only appears to return a value, when the user is moving(only tested on Android so far), and if it is, update the heading of the point.接下来我检查标题实际上是否可用,因为它似乎只在用户移动时返回一个值(目前仅在 Android 上测试过),如果是,则更新该点的标题。

if($rootScope.position.heading){
    var heading = $rootScope.position.heading; 
    map.setLayoutProperty('drone', 'icon-rotate', $rootScope.position.heading);
};

Finally i update the position of the point, in a "$watch" position.最后我更新点的位置,在“$watch”位置。

map.getSource('drone').setData(point);

This way, i can watch the users heading, and the point keeps on track, even when the user rotates the map.这样,即使用户旋转地图,我也可以观察用户的前进方向,并且该点保持在轨道上。

For the users coming here after 2020 (what an year lol), mapbox gl js now supports geolocation which not only provides user's heading but also a bunch of other helpful data:对于 2020 年之后(哈哈)来这里的用户,mapbox gl js 现在支持地理定位,它不仅提供用户的标题,还提供一堆其他有用的数据:

const geolocate = map.addControl(
 new mapboxgl.GeolocateControl({
   positionOptions: {
      enableHighAccuracy: true
   },
   trackUserLocation: true
 })
)

then listen for geolocate event:然后监听 geolocate 事件:

geolocate.on('geolocate', (e) => {
     console.log(e); 
});

this will give you following object:这将为您提供以下对象:

{
coords: {
    accuracy: number;
    altitude: number;
    altitudeAccuracy: number;
    heading: number;
    latitude: number;
    longitude: number;
    speed: number;
  };
  timestamp: number;
 

heading will give you direction of the user. heading将为您提供用户的方向。 As the geolocate control keeps triggering automatically so can get the user's direction as well as speed and altitude etc in real time and use that to display data driven symbols on map.由于地理定位控件不断自动触发,因此可以实时获取用户的方向以及速度和高度等,并使用它在地图上显示数据驱动的符号。

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

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