• iOS添加Mapbox地图库


    配置凭据

    注册并导航到Account页面。你将需要:

    • 公共访问令牌:

    从帐户的tokens页面,你可以复制默认的公共令牌或单击"create a token"按钮来创建新的公共令牌。

    • 带有Downloads:Read范围的秘密访问令牌:

    从你帐户的tokens页面中,单击"create a token"按钮。

    在创建令牌页面上,为你的令牌命名,并确保Downloads:Read范围旁边的框被选中。

    单击页面底部的"create token"按钮以创建你的令牌。

    你创建的令牌是一个秘密令牌,这意味着你将只有一次机会将其复制到安全的地方。

    配置秘密令牌

    你的秘密令牌允许你直接从Mapbox下载SDK。

    要使用你的秘密令牌,你需要将其存储在主目录(而不是项目文件夹)的.netrc文件中。
    为.netrc配置0600权限:

    chmod 0600 ~/.netrc

    要设置下载SDK所需的凭据,请将以下条目添加到你的.netrc文件中:

    1. machine api.mapbox.com
    2. login mapbox
    3. password YOUR_SECRET_MAPBOX_ACCESS_TOKEN

    配置公共令牌

    要配置你的公共访问令牌,请打开项目的Info.plist文件,并添加一个MBXAccessToken密钥,其值是你的公共访问令牌。

    添加依赖项

    MapBox地图库

    Mapbox Maps可以通过Swift Package Manager(SPM)使用。要使用SPM添加Mapbox Maps SDK,你需要配置你的环境以从Mapbox下载它,请确保你已将秘密令牌添加到你的.netrc文件中。

    打开Xcode项目,然后转到File > Swift Packages > Add Package Dependency,输入GitHub - mapbox/mapbox-maps-ios: Interactive, thoroughly customizable maps for iOS powered by vector tiles and Metal作为URL,按Enter键拉入软件包,然后单击Add Package

    在代码中可以用import MapboxMaps引入依赖包。

    MapBox导航库

    Mapbox Navigation SDK可以通过Swift Package Manager(SPM)使用。要使用SPM添加Mapbox Navigation SDK,你需要配置你的环境以从Mapbox下载它,请确保你已将秘密令牌添加到你的.netrc文件中。

    打开Xcode项目,然后转到File > Swift Packages > Add Package Dependency,输入https://github.com/mapbox/mapbox-navigation-ios.git作为URL,按Enter键拉入软件包,然后单击Add Package

    Dependency Rule设置为Up to Next Major Version,并输入2.14.0作为最小版本

    选择MapboxNavigation库,如果不需要任何UI组件,则选择MapboxCoreNavigation。

    在代码中可以用import MapboxNavigation引入依赖包。

    隐私和权限

    用户必须先授予您的应用程序权限,然后才能访问有关其位置的信息。添加下列配置到Info.plist。

    向用户简要说明应用程序将如何使用其位置数据进行临时访问:

    1. <key>NSLocationWhenInUseUsageDescriptionkey>
    2. <string>Your precise location is used to calculate turn-by-turn directions, show your location on the map, and help improve the map.string>

    添加LocationAccuracyAuthorizationDescription作为NSLocationTemporaryUsageDescriptionDictionary字典的元素,为用户提供简要解释为什么应用程序中的功能需要其确切位置:

    1. <key>NSLocationTemporaryUsageDescriptionDictionarykey>
    2. <dict>
    3.   <key>LocationAccuracyAuthorizationDescriptionkey>
    4.   <string>Please enable precise location. Turn-by-turn directions only work when precise location data is available.string>
    5. dict>

    添加地图

    1. import UIKit
    2. import MapboxMaps
    3. class MapViewController: UIViewController {
    4. internal var mapView: MapView!
    5. override public func viewDidLoad() {
    6. super.viewDidLoad()
    7. let myResourceOptions = ResourceOptions(accessToken: "your_public_access_token")
    8. let myMapInitOptions = MapInitOptions(resourceOptions: myResourceOptions)
    9. mapView = MapView(frame: view.bounds, mapInitOptions: myMapInitOptions)
    10. mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    11. self.view.addSubview(mapView)
    12. }
    13. }

    在地图上添加标记

    1. let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
    2. // Initialize a point annotation with a geometry ("coordinate" in this case)
    3. var pointAnnotation = PointAnnotation(coordinate: coordinate)
    4. // Make the annotation show a red pin
    5. pointAnnotation.image = .init(image: UIImage(named: "red_pin")!, name: "red_pin")
    6. pointAnnotation.iconAnchor = .center
    7. pointAnnotation.iconSize = 0.5 //icon image scale
    8. // Create the `PointAnnotationManager` which will be responsible for handling this annotation
    9. let pointAnnotationManager = mapView.annotations.makePointAnnotationManager()
    10. // Add the annotation to the manager in order to render it on the map.
    11. pointAnnotationManager.annotations = [pointAnnotation]

    在地图上添加路线

    1. // Define two or more geographic coordinates to connect with a line.
    2. // Line from New York City, NY to Washington, D.C.
    3. let lineCoordinates = [
    4. CLLocationCoordinate2DMake(40.7128, -74.0060),
    5. CLLocationCoordinate2DMake(38.9072, -77.0369)
    6. ]
    7. // Create the line annotation.
    8. var lineAnnotation = PolylineAnnotation(lineCoordinates: lineCoordinates)
    9. lineAnnotation.lineColor = StyleColor(.blue)
    10. // Create the `PolylineAnnotationManager` which will be responsible for handling this annotation
    11. let lineAnnnotationManager = mapView.annotations.makePolylineAnnotationManager()
    12. // Add the annotation to the manager.
    13. lineAnnnotationManager.annotations = [lineAnnotation]

    设置相机位置

    Maps SDK的相机是指用户在地图上方的观测点。

    相机的位置和行为由其属性定义:

    • center:相机指向的经度和纬度。
    • bearing:地图的视觉旋转。轴承值是相机指向的指南针方向,以向用户显示哪个方向是“向上”。例如,90°的方位将地图定向,使东面朝上。
    • pitch:地图的视觉倾斜。0°的间距垂直于表面,直视地图,而60°等更大值则朝向地平线。
    • zoom:缩放级别指定相机与所查看功能的距离。在缩放级别0时,视口显示大陆和海洋。11的中间值显示城市级别的细节,在更高的缩放级别,地图开始显示建筑物和兴趣点。
    • padding:地图每个边缘的插图。影响渲染center的位置。
    • anchor:地图坐标系中关于应应用zoombearing的点。与center相互排斥。

    在地图初始化时设置相机

    1. // Define center coord, zoom, pitch, bearing
    2. let cameraOptions = CameraOptions(center: CLLocationCoordinate2D(latitude: 40.7135, longitude: -74.0066),
    3. zoom: 15.5,
    4. bearing: -17.6,
    5. pitch: 45)
    6. // Pass camera options to map init options
    7. let options = MapInitOptions(cameraOptions: cameraOptions)
    8. // Pass options when initializing the map
    9. mapView = MapView(frame: view.bounds, mapInitOptions: options)

    地图初始化后设置相机

    1. let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
    2. let options = CameraOptions(center: coordinate, zoom: 10)
    3. //不带动画
    4. mapView.mapboxMap.setCamera(to: options)
    5. //带动画
    6. mapView.camera.fly(to: options, duration: 3)

    根据设备位置设置相机

    1. if let locationCoordinate = self.mapView?.location.latestLocation?.coordinate {
    2. mapView.mapboxMap.setCamera(to: CameraOptions(center: locationCoordinate, zoom: 15))
    3. }

    获取道路限速

    1. import MapboxCoreNavigation
    2. import MapboxNavigation
    3. class LocationManager {
    4. private let passiveLocationManager = PassiveLocationManager()
    5. private lazy var passiveLocationProvider = PassiveLocationProvider(locationManager: passiveLocationManager)
    6. init() {
    7. NotificationCenter.default.addObserver(self, selector: #selector(didUpdatePassiveLocation), name: Notification.Name.passiveLocationManagerDidUpdate, object: nil)
    8. passiveLocationProvider.startUpdatingLocation()
    9. }
    10. @objc func didUpdatePassiveLocation(_ notification: Notification) {
    11. let speedLimitObj = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.speedLimitKey]
    12. if (speedLimitObj != nil) {
    13. let speedLimit: Measurement<UnitSpeed> = speedLimitObj! as! Measurement<UnitSpeed>
    14. print("speedLimit:", speedLimit, speedLimit.value)
    15. }
    16. }
    17. }

  • 相关阅读:
    基于TCP的聊天系统
    【Linux】之Jumpserver堡垒机添加linux主机资产
    C#实现二叉排序树定义、插入、构造
    Flink CEP 超时事件处理:使用侧输出流和 Scala Lambda 版本编程
    GeoJson格式标准规范
    buu(ssti模板注入、ssrf服务器请求伪造)
    Echarts异步数据与动画加载
    lwip多网卡自适应选择
    Windows如何设置Tomcat开机启动?
    C++指针和地址偏移在HotSpot VM中的应用
  • 原文地址:https://blog.csdn.net/watson2017/article/details/132966800