gpt4 book ai didi

ios - 在 iOS 中,使用 Swift,我想显示用户的位置,但是蓝点没有出现在 MapView 上

转载 作者:行者123 更新时间:2023-11-28 14:09:48 25 4
gpt4 key购买 nike

蓝点没有出现在 map 上。看起来 CLLocation 管理器和 self.mapView.userLocation.coordinate 值之间存在一些断开。

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

我已经调试了好几个小时了。

更多信息如下:

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

print(self.mapView.userLocation.coordinate)

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

CLLocationCoordinate2D(latitude: 0.0, longitude: 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. Storyboard中的 MapView 通过实心圆连接到正确的 ViewController。

  3. MapViewController 实例化如下:

    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) 让 mapVC = storyboard.instantiateViewController(withIdentifier: "MapVC") 作为? map View Controller self.present(mapVC!, animated: true, completion: nil)

最佳答案

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

例如

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

另请检查“设置”>“隐私”>“定位服务”>“您的应用”

enter image description here

关于ios - 在 iOS 中,使用 Swift,我想显示用户的位置,但是蓝点没有出现在 MapView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com