What I have is a simple map layout Where it finds the current users location and then it places a pin at that current location. I think I have the code right but I don't see the pin when I run my location.
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.
}
}
答案 0 :(得分:0)
首先,必须在MKMapViewDelegate
中添加ViewController
。因为您需要使用 MapView委派方法来显示注释。查看我的代码并将它们添加到您的项目中。如果您获得用户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 !
}
}