2016-04-04 13 views
0

私が持っているものは、現在のユーザーの場所を見つけてその現在の場所にピンを配置する単純なマップレイアウトです。私はコードが正しいと思うが、自分の位置を動かすとピンが見えない。注釈を地図に表示できないのはなぜですか?

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

まず、あなたViewControllerMKMapViewDelegateを追加する必要があります。 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 ! 
    } 
} 
関連する問題