简体   繁体   English

迅速的位置归零

[英]Swift location comes back nil

This code was functioning earlier, and now it keeps crashing because the location is coming back as nil. 这段代码较早起作用,现在由于位置返回nil而一直崩溃。 I'm not sure where this error is coming from or how to fix it. 我不确定此错误来自何处或如何解决。 From what I can tell, my code is correct. 据我所知,我的代码是正确的。 Any help? 有什么帮助吗?

import UIKit
import CoreLocation
import MapKit

class LocationController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    var locationManager = CLLocationManager()

    @IBOutlet var Map: MKMapView!

    var manager:CLLocationManager!
    var myLocations: [CLLocation] = []

        override func viewDidLoad()
        {
            super.viewDidLoad()

            self.Map.mapType = MKMapType.Standard
            self.Map.showsUserLocation = true

            //self.Map.removeAnnotations(self.theMap.annotations)

            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()


            let location = self.locationManager.location

            var latitude: Double = location.coordinate.latitude
            var longitude: Double = location.coordinate.longitude

            println("current latitude :: \(latitude)")
            println("current longitude :: \(longitude)")
        }

The location property of CLLocationManager is declared as: CLLocationManagerlocation属性声明为:

@NSCopying var location: CLLocation! { get }

and thus your let location could be filled with a nil value. 因此,您的let location可能会填充nil值。 Perhaps this declaration changed recently as Apple scrubs their interfaces related to optionals. 也许最近苹果更改了与可选控件相关的接口时,该声明发生了变化。

You'll need to bind your latitude and longitude while respecting a possible nil . 您需要约束latitudelongitude同时注意可能的nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM