2016-04-09 23 views
0

Ich versuche herauszufinden, wie man den aktuellen Standort mit SWIFT für eine iOS-App in XCODE abrufen kann.Wie bekomme ich den aktuellen Standort in Swift iOS Programmierung?

Ich folgte diesem Tutorial here, aber aus irgendeinem Grund funktioniert mein Code nicht. Ich denke, das liegt daran, dass ich eine aktualisierte Xcode-Versionierung verwende. Aus irgendeinem Grund hat mein Delegierter verschiedene Parameter und ich bin mir nicht sicher, warum, aber wenn ich mein Programm erstelle, fragt es mich nicht einmal, ob ich überhaupt Standortdienste erlauben möchte.

Hier ist mein Code

override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     self.locationManager.delegate = self; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     self.locationManager.requestWhenInUseAuthorization(); 
     self.locationManager.startUpdatingLocation(); 
    } 

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

    // GPS STUFF 
    // UPDATE LOCATION 
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, ErrorType) -> Void in 
      if(ErrorType != nil) 
      { 
       print("Error: " + ErrorType!.localizedDescription); 
       return; 
      } 

      if(placemarks?.count > 0) 
      { 
       let pm = placemarks![0] ; 
       self.displayLocationInfo(pm); 
      } 
     } 
    } 

    // STOP UPDATING LOCATION 
    func displayLocationInfo(placemark: CLPlacemark) 
    { 
     self.locationManager.stopUpdatingLocation(); 
     print(placemark.locality); 
     print(placemark.postalCode); 
     print(placemark.administrativeArea); 
     print(placemark.country); 
    } 

Auch in info.plist Ich habe auch NSLocationWhenInUseUsageDescription.

würde Ratschläge

Antwort

0

In Ihrem Info.plist, fügen Sie diese groß sein:

enter image description here

oder Sie können in Ihrem Info.plist wie folgt hinzu:

<key>NSLocationAlwaysUsageDescription</key> 
    <string>To get current location</string> 

Dies wird Sie fragen, Lokalisierungsdienste zulassen

Ich hoffe, dies hilft Ihnen.

+0

Nein, das hat auch nicht funktioniert. –

+0

Sieht alles so aus, als könnte es in meiner locationManager Funktion falsch liegen? –

+0

Sie haben vergessen, mapView.showsUserLocation = true in Ihrer viewDidLoad zu füllen, lassen Sie es uns versuchen. – Khuong

Verwandte Themen