[英]In iOS, using Swift, I want to show the user's location, but the blue dot does not appear on the MapView
藍點未出現在地圖上。 似乎CLLocation管理器和self.mapView.userLocation.coordinate值之間存在一些斷開連接。
CLLocation管理器正確返回Apple Campus坐標,但mapView.userLocation.coordinate值的緯度和經度均返回0,0。
我已經調試了好幾個小時。
以下是更多信息:
在viewDidAppear和viewDidLoad中,我將用戶的當前位置打印到控制台,如下所示:
print(self.mapView.userLocation.coordinate)
這是在控制台中呈現的輸出:
CLLocationCoordinate2D(緯度:0.0,經度:0.0)
這是我的MapViewController的樣子:
import UIKit
import CoreLocation
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var manager: CLLocationManager? = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// map stuff
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest
self.mapView.delegate = self
// print user's current location to console
print("user location in view did load:")
print(self.mapView.userLocation.coordinate)
}
override func viewDidAppear(_ animated: Bool) {
manager?.requestAlwaysAuthorization()
manager?.startUpdatingLocation()
self.mapView.showsUserLocation = true
self.mapView.userTrackingMode = .follow
// print user's current location to console
print("user location in view did appear:")
print(self.mapView.userLocation.coordinate)
animateMapToUserLocation()
}
}
筆記:
我已將相關的隱私消息添加到Info.plist文件中。
情節提要中的MapView用實心圓連接到正確的ViewController。
MapViewController的實例化如下:
讓Storyboard = UIStoryboard(name:“ Main”,bundle:Bundle.main)讓mapVC = storyboard.instantiateViewController(withIdentifier:“ MapVC”)為? MapViewController self.present(mapVC !,動畫:true,完成:nil)
我的控制流程關閉了。 該地圖將在應用有權檢索用戶位置之前呈現。 現在正在工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.