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

时间:2016-06-10 22:06:44

标签: ios swift mapbox

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

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)
    }     
}

1 个答案:

答案 0 :(得分:2)

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

enter image description here

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

相关问题