2016-04-04 9 views
0

Was ich habe, ist ein einfaches Kartenlayout Wo es den aktuellen Benutzer Standort findet und dann einen Pin an diesem aktuellen Ort platziert. Ich denke, ich habe den richtigen Code, aber ich sehe den Pin nicht, wenn ich meinen Standort leite.Warum kann ich meine Anmerkungen nicht auf der Karte anzeigen lassen?

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. 
} 

}

Antwort

0

Zu allererst Sie müssen die MKMapViewDelegate in Ihrem ViewController hinzuzufügen. Weil Sie MapView Delegate Methoden verwenden müssen, um Ihre Anmerkungen anzuzeigen. Sieh dir meine Codes an und füge sie deinem Projekt hinzu. Es sollte funktionieren, wenn Sie Benutzer currentLocation Koordinaten ohne jedes Problem erhalten. Wenn es nicht funktioniert, lass es mich wissen.

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 ! 
    } 
} 
Verwandte Themen