swift - MapBox didUpdateUserLocation 从未被调用

标签 swift mapbox

我正在尝试使用 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/

相关文章:

javascript - mapbox-gl js 对 "text-offset"使用步进斜坡

java - 如何从 map 上删除符号

arrays - 如何在单个表达式中追加到新初始化的 Array()?

profiling - 如何在 Swift 中检测和调试强引用循环?

swift - 在for循环中附加图像url数组,然后发布到数据库

ios - 如何使 UIView 匹配 UILabel 的大小

ios - 无法在 iOS Swift 中导入 UserNotifications

java - Mapbox,MapSnapshotter.callback 并不总是被调用

javascript - Redux mapStateToProps 在我的 Mapbox map 中不起作用

ios - 使用 NSPredicate 过滤 Mapbox MGLFillExtrusionStyleLayer 中的 MGLFeature $id