0

蓝点不会出现在地图上。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()
}

}

笔记:

  1. 我已将相关的隐私消息添加到 Info.plist 文件中。

  2. 故事板中的 MapView 以实心圆圈连接到正确的 ViewController。

  3. MapViewController 实例化如下:

    让 storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) 让 mapVC = storyboard.instantiateViewController(withIdentifier: "MapVC") as? MapViewController self.present(mapVC!,动画:真,完成:无)

4

2 回答 2

1

你实现了 viewForAnnotation 功能了吗?您是否正在检查您没有为 MKUserLocation 绘制(或未能绘制)不同类型的引脚?

例如

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
    ...
}

还要检查设置>隐私>定位服务>您的应用

在此处输入图像描述

于 2018-10-06T13:30:15.700 回答
0

我的控制流关闭了。地图将在应用程序有权检索用户位置之前呈现。它现在正在工作。

于 2018-10-06T19:21:43.783 回答