我正在尝试使用 MapBox 响应用户在 iOS 应用程序中的位置变化,但未调用 didUpdateUserLocation
。为什么不调用 didUpdateUserLocation
?
ViewController.swift
import UIKit
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
@IBOutlet weak var upButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let styleURL = NSURL(string: "mapbox://styles/jmeyers919/cj8w00yxvfrqr2rpehxd47up1") // MGLStyle.darkStyleURL()
let mapView = MGLMapView(frame: view.bounds, styleURL: styleURL as URL?)
mapView.delegate = self
mapView.logoView.isHidden = true
mapView.attributionButton.isHidden = true
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Note that we have changed the center coordinate to New York City for this guide
// mapView.setCenter(CLLocationCoordinate2D(latitude: 44.0475276, longitude: -123.08927319), zoomLevel: 16, animated: false)
mapView.userTrackingMode = .follow
mapView.showsUserLocation = true
view.addSubview(mapView)
self.view.bringSubview(toFront: self.upButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func mapView(mapView: AnyObject!, didUpdateUserLocation userLocation: AnyObject!) {
print("didUpdateUserLocation")
}
}
最佳答案
当 showsUserLocation
属性设置为 YES
时,调用此方法
每本地图 View 收到新的位置更新时。这个方法也是
如果 map View 的用户跟踪模式设置为
MGLUserTrackingModeFollowWithHeading
和航向改变,或者如果它被设置
到 MGLUserTrackingModeFollowWithCourse
并且类(class)更改。
如果应用程序当前在后台运行,则不会调用此方法。如果您想在运行时接收位置更新 后台,必须使用Core Location框架。
关于swift - MapBox didUpdateUserLocation 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800207/