我想在用户打开应用程序后立即显示用户的当前位置。我使用 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,您会在底部看到一个导航按钮。
尝试将位置更改为某个位置,它应该会显示:)
关于ios - 无法显示用户当前位置 Mapbox iOS API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37757761/