ios - Mapbox Basic Navigation App:如何将路线原点设置为用户位置?
问题描述
我从 Mapbox 教程中获得了一些用于 Turn-by-Turn 视图中的基本导航应用程序的代码。一切正常,我已经添加了一个 Waypoint。
在示例中,路线的原点设置为固定坐标。这与我的用例不兼容。Waypoints 和 Destination 也由坐标固定,这很好。但来源必须是“用户的位置”,这显然是可变的。
也许有人可以帮助我,将不胜感激。:)
import Foundation
import UIKit
import MapboxCoreNavigation
import MapboxNavigation
import MapboxDirections
class PlacemarktestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let origin = CLLocationCoordinate2DMake(37.77440680146262, -122.43539772352648)
let waypoint = CLLocationCoordinate2DMake(27.76556957793795, -112.42409811526268)
let destination = CLLocationCoordinate2DMake(37.76556957793795, -122.42409811526268)
***strong text***
let options = NavigationRouteOptions(coordinates: [origin, waypoint, destination])
Directions.shared.calculate(options) { [weak self] (session, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
guard let route = response.routes?.first, let strongSelf = self else {
return
}
// For demonstration purposes, simulate locations if the Simulate Navigation option is on.
// Since first route is retrieved from response `routeIndex` is set to 0.
let navigationService = MapboxNavigationService(route: route, routeIndex: 0, routeOptions: options)
let navigationOptions = NavigationOptions(navigationService: navigationService)
let navigationViewController = NavigationViewController(for: route, routeIndex: 0, routeOptions: options, navigationOptions: navigationOptions)
navigationViewController.modalPresentationStyle = .fullScreen
// Render part of the route that has been traversed with full transparency, to give the illusion of a disappearing route.
navigationViewController.routeLineTracksTraversal = true
strongSelf.present(navigationViewController, animated: true, completion: nil)
}
}
}
}
解决方案
@zeno,设置您可以使用的用户位置
mapView.setUserTrackingMode(.follow, animated: true)
不要忘记将NSLocationWhenInUseUsageDescription
密钥添加到信息 plist。
或者您可以获取坐标并使用手动设置它们CoreLocation
import CoreLocation
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestLocation() // Request a user’s location
文档在这里requestLocation
然后实现处理请求的用户位置的CLLocationManagerDelegate
方法。locationManager(:, didUpdateLocations:)
该方法在使用后会被调用一次locationManager.requestLocation()
。
func locationManager(
_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]
) {
if let location = locations.first {
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
}
}
推荐阅读
- node.js - 使用查找查询后,我想将子类别作为 json 对象附加到父类别
- c# - 为什么我的应用程序试图读取错误文件夹中的对象?
- php - 跟踪阅读电子邮件所花费的时间
- macos - php 文件未使用 nginx-macOs 打开
- c# - AutoMapper 和 CA1506:避免过度的类耦合
- unity3d - 测试使用本地私有 SerializedField 的 Unity 方法
- c# - .netcore 单元测试运行不正常
- c# - 应用程序仅在一台设备上崩溃。从图库访问图像时返回 null 而不是 uri
- node.js - 如果我不关心人类的可读性,使用“二进制”作为读取文件的编码是一种好习惯吗?
- office-js - 取消保护不在excel中工作添加在线office365