首页 文章

在iOS中,使用Swift,我想显示用户的位置,但蓝点不会出现在MapView上

提问于
浏览
0

蓝点不会出现在 Map 上 . 看来CLLocation管理器和self.mapView.userLocation.coordinate值之间存在一些脱节 .

CLLocation管理器正确返回Apple Campus坐标,但mapView.userLocation.coordinate值为纬度和经度返回0,0 .

我已经调试了几个小时 .

More Information Below:

在viewDidAppear和viewDidLoad中,我将用户的当前位置打印到控制台,如下所示:

print(self.mapView.userLocation.coordinate)

这是在控制台中呈现的输出:

CLLocationCoordinate2D(纬度:0.0,经度:0.0)

This is what my MapViewController looks like:

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实例化如下:

let storyboard = UIStoryboard(名称:“Main”,bundle:Bundle.main)让mapVC = storyboard.instantiateViewController(withIdentifier:“MapVC”)为? MapViewController self.present(mapVC!,animated:true,completion:nil)

2 回答

  • 1

    你实现了viewForAnnotation函数吗?你在检查你没有为MKUserLocation绘制(或没有绘制)不同类型的引脚吗?

    例如

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

    还可以检查设置>隐私>位置服务>您的应用

    enter image description here

  • 0

    我的控制流程已关闭 . 在应用有权检索用户位置之前, Map 将呈现 . 它现在正在运作 .

相关问题