gpt4 book ai didi

ios - 无法在 MKMapview Swift 上显示注释图钉

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

我正在尝试显示当前位置的注释图钉。为此,我编写了以下代码

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mapView.layer.cornerRadius = 8.0
imageView.layer.cornerRadius = 8.0

//mapview settings
mapView.mapType = MKMapType.standard
mapView.isZoomEnabled = true
mapView.isZoomEnabled = true

// Or, if needed, we can position map in the center of the view
mapView.center = view.center
mapView.delegate = self

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

determineCurrentLocation()
}

func determineCurrentLocation()
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

if CLLocationManager.locationServicesEnabled() {
//locationManager.startUpdatingHeading()
locationManager.startUpdatingLocation()
}
}
//Mapview Delegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation

// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
//manager.stopUpdatingLocation()

let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

mapView.setRegion(region, animated: true)

// Drop a pin at user's Current Location
let myAnnotation: MKPointAnnotation = MKPointAnnotation()
myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
myAnnotation.title = "Current location"
mapView.addAnnotation(myAnnotation)
}

func locationManager(manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}

但是,它在 map View 中没有任何显示。有什么建议吗?

enter image description here

最佳答案

您需要在 Info.plist 中添加 NSLocationAlwaysUsageDescription 键,并在提示中显示一条消息。

enter image description here

关于ios - 无法在 MKMapview Swift 上显示注释图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50343384/

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