ios - 无法显示用户当前位置 Mapbox iOS API

标签 ios swift mapbox

我想在用户打开应用程序后立即显示用户的当前位置。我使用 Mapbox iOS SDK。但这根本不起作用。我不知道出了什么问题。打开后只能看到不相关的 map 。

import UIKit
import Mapbox
import CoreLocation

class ViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate 
{

    @IBOutlet weak var mapView: MGLMapView!

    let locationManager = CLLocationManager()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()  
     }


    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: Location Delegate Methods

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    {
        let location = locations.last
        let mapView = MGLMapView(frame: view.bounds)
        mapView.userTrackingMode = .Follow
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        mapView.setCenterCoordinate(center, zoomLevel: 15, animated: true)
        view.addSubview(mapView)
        self.locationManager.stopUpdatingLocation()
    }

    func  locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print ("Errors:" + error.localizedDescription)
    }     
}

最佳答案

我认为问题可能出在 iOS 模拟器上。当您在模拟器上运行应用程序时,它不会模拟您的位置,除非您告诉它。 如果您查看 Xcode,您会在底部看到一个导航按钮。

enter image description here

尝试将位置更改为某个位置,它应该会显示:)

关于ios - 无法显示用户当前位置 Mapbox iOS API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37757761/

相关文章:

android - 我应该在 iOS Android 应用程序上使用什么加密方法 : AES128 or 3DES

javascript - 在标记之间动态绘制线条

ios - 什么是 CGFloat.random(min : CGFloat, max : CGFloat) of swift 2 in swift 3?

ios - 如何将计时器移至后台线程

ios - 使用 Swift 登录 Facebook 后不会触发 Segue

ios - UIPageViewController 未在单击按钮时完成转换

routes - 如何初始化传单路由线

android - Mapbox 4 到 Mapbox 5 错误

ios - 删除用户位置注释上的选择图像

ios - 使用 NSDate 获取日期