gpt4 book ai didi

swift - 为什么我无法在 map 上显示我的注释?

转载 作者:行者123 更新时间:2023-11-30 13:32:29 25 4
gpt4 key购买 nike

我拥有的是一个简单的 map 布局,它可以找到当前用户的位置,然后在该当前位置放置一个图钉。我认为我的代码是正确的,但当我运行我的位置时,我没有看到 pin。

import MapKit
import UIKit

class ViewController: UIViewController, CLLocationManagerDelegate {

var coreLocationManager = CLLocationManager()

var locationManager:LocationManager!

//allowing permission to use your location

@IBOutlet weak var Map: MKMapView!

@IBOutlet weak var myLocation: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

coreLocationManager.delegate = self

locationManager = LocationManager.sharedInstance

let authorizationCode = CLLocationManager.authorizationStatus()

if authorizationCode == CLAuthorizationStatus.NotDetermined && coreLocationManager.respondsToSelector("requestAlwaysAuthorization") || coreLocationManager.respondsToSelector("requestWhenInUseAuthorization"){

if NSBundle.mainBundle().objectForInfoDictionaryKey("NSLocationAlwaysUsageDescription") != nil {
coreLocationManager.requestAlwaysAuthorization()
}else{
print("No description provided")
}

}else{
getLocation()

}

}
//getting your location

func getLocation(){

locationManager.startUpdatingLocationWithCompletionHandler { (latitude, longitude, status, verboseMessage, error) -> () in
self.displayLocation(CLLocation(latitude: latitude, longitude: longitude))
}


}

func displayLocation(Location:CLLocation){

Map.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(Location.coordinate.latitude, Location.coordinate.longitude), span: MKCoordinateSpanMake(0.05, 0.05)), animated: true)

let locationPinCoord = CLLocationCoordinate2D(latitude: Location.coordinate.latitude, longitude: Location.coordinate.longitude)
let annotation = MKPointAnnotation()
annotation.coordinate = locationPinCoord










//SHOW POINTS
Map.addAnnotation(annotation)
Map.showAnnotations([annotation], animated: true)



}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status != CLAuthorizationStatus.NotDetermined || status != CLAuthorizationStatus.Denied || status != CLAuthorizationStatus.Restricted{

}
}


@IBAction func updateLocation(sender: AnyObject) {
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

最佳答案

首先,您必须在您的ViewController中添加MKMapViewDelegate。因为您需要使用 MapView 委托(delegate)方法来显示您的注释。查看我的代码并将它们添加到您的项目中。如果您毫无问题地获取用户 currentLocation 坐标,那么它应该可以工作。如果不起作用请告诉我。

import UIKit
import MapKit

class YourViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
guard let pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("YourReusableIdentifier") as! MKAnnotationView else { return nil }
pinView.annotation = annotation
pinView.canShowCallout = true
return pinView
}

// It directs to CalloutView position to Above of Pin Position
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
// Do something when user didSelect the Annotation View.
}

override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
getLocation() // Starting Location Manager Update this is your method !
}
}

关于swift - 为什么我无法在 map 上显示我的注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36409518/

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