未获取用户在swift 4中的当前位置

未获取用户在swift 4中的当前位置,swift,core-location,xcode10,Swift,Core Location,Xcode10,位置未打印到控制台。我以前也有过这样的代码,但自从更新到Xcode 10后,就没有将用户当前位置打印到控制台中 var locationManager: CLLocationManager! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) determineMyCurrentLocation() } func determineMyCurrentLocation(

位置未打印到控制台。我以前也有过这样的代码,但自从更新到Xcode 10后,就没有将用户当前位置打印到控制台中

var locationManager: CLLocationManager!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    determineMyCurrentLocation()
}


func determineMyCurrentLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0] as CLLocation

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")

}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Error \(error)")
}
Info.plist:

确认NSLOCATION当您的plist文件中有使用说明和NSLOCATIONALWAYSEUSAGEDESCRIPTION时。

确认NSLOCATION当您的plist文件中有使用说明和NSLOCATIONALWAYSEUSAGEDESCRIPTION时。

是否导入了CoreLocation

尝试更改您的locationManager表单

然后你就不需要它在当前位置上了

我还将让userLocation=CLLocation位于类的顶部,然后更新您的位置

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
    let newLocation = locations[0]
    let distance = userLocation.distance(from: newLocation)
    // using this to update my location every 100m
    if distance > 100 {
        userLocation = newLocation
    }

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")
}
您导入了CoreLocation吗

尝试更改您的locationManager表单

然后你就不需要它在当前位置上了

我还将让userLocation=CLLocation位于类的顶部,然后更新您的位置

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
    let newLocation = locations[0]
    let distance = userLocation.distance(from: newLocation)
    // using this to update my location every 100m
    if distance > 100 {
        userLocation = newLocation
    }

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")
}
问题是你的Info.plist。您需要全部三把钥匙:

解决了这一问题,应用程序将起死回生。我将您的代码粘贴到一个项目中,如图所示设置Info.plist,它运行良好

在iOS 11中,您实际上可以省略“始终开始”,但在使用时和“始终”以及“使用时”必须同时具有这两个选项,即使您计划只要求“始终”

实际上,运行时一直在Xcode控制台中告诉您这一点,但您显然没有看到它。它说:

应用程序的Info.plist必须同时包含NSLocationAlways和WhenInUsageDescription以及NSLocationWhenUsageDescription键,并带有向用户解释应用程序如何使用此数据的字符串值

问题是你的Info.plist。您需要全部三把钥匙:

解决了这一问题,应用程序将起死回生。我将您的代码粘贴到一个项目中,如图所示设置Info.plist,它运行良好

在iOS 11中,您实际上可以省略“始终开始”,但在使用时和“始终”以及“使用时”必须同时具有这两个选项,即使您计划只要求“始终”

实际上,运行时一直在Xcode控制台中告诉您这一点,但您显然没有看到它。它说:

应用程序的Info.plist必须同时包含NSLocationAlways和WhenInUsageDescription以及NSLocationWhenUsageDescription键,并带有向用户解释应用程序如何使用此数据的字符串值

步骤:1-导入

步骤:2-使用:MKMapViewDelegate、CLLocationManagerDelegate

步骤:3-

步骤:4-在ViewDidLoad中

 self.determineMyCurrentLocation()
步骤:5-定义函数

func determineMyCurrentLocation() {
    if CLLocationManager.locationServicesEnabled()
    {
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.distanceFilter = kCLDistanceFilterNone
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

        let authorizationStatus = CLLocationManager.authorizationStatus()
        if (authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways)
        {
            self.locationManager.startUpdatingLocation()
        }
        else if (locationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)))
        {
             self.locationManager.requestAlwaysAuthorization()
        }
        else
        {
            self.locationManager.startUpdatingLocation()
        }
    }
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error while requesting new coordinates")
    }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    self.latitude = locations[0].coordinate.latitude
    self.longitude = locations[0].coordinate.longitude
}
在info.plist中插入{隐私-位置使用说明,隐私-位置始终和使用时使用说明,隐私-位置始终和使用时使用说明,隐私-位置始终使用说明}

步骤:1-导入

步骤:2-使用:MKMapViewDelegate、CLLocationManagerDelegate

步骤:3-

步骤:4-在ViewDidLoad中

 self.determineMyCurrentLocation()
步骤:5-定义函数

func determineMyCurrentLocation() {
    if CLLocationManager.locationServicesEnabled()
    {
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.distanceFilter = kCLDistanceFilterNone
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

        let authorizationStatus = CLLocationManager.authorizationStatus()
        if (authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways)
        {
            self.locationManager.startUpdatingLocation()
        }
        else if (locationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)))
        {
             self.locationManager.requestAlwaysAuthorization()
        }
        else
        {
            self.locationManager.startUpdatingLocation()
        }
    }
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error while requesting new coordinates")
    }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    self.latitude = locations[0].coordinate.latitude
    self.longitude = locations[0].coordinate.longitude
}
在info.plist中插入{隐私-位置使用说明,隐私-位置始终和使用时使用说明,隐私-位置始终和使用时使用说明,隐私-位置始终使用说明}


在上面的代码中?欢迎使用Stack Overflow,这个问题似乎已经得到了回答我已经尝试过了,位置仍然没有被更新nothing@Gary在真实设备中测试位置服务比在模拟器中测试位置服务更好。如果使用模拟器,您可以通过以下方式模拟位置:调试>位置>自定义位置..在上面的代码中?欢迎使用堆栈溢出,这个问题似乎已经得到回答我已经尝试过了,该位置仍然没有被更新nothing@Gary在真实设备中测试位置服务比在模拟器中测试位置服务更好。如果使用模拟器,您可以通过以下方式模拟位置:调试>位置>自定义位置..仅用于请求AlwaysAuthorization为什么他需要全部3个@亲爱的,别看我!我试过了,但总是在使用时出错。然后我在添加Always后尝试了一下,结果出现了一个错误。然后我在使用中添加了,正如我所说的,整个应用程序开始活跃起来。这个问题只发生在Xcode 10上,对吗?尽管如此,我想把它们全部加起来是不会费事的:@亲爱的,在iOS 11中你实际上可以省略总是开始,但你必须在使用时和总是以及使用时都有。它与Xcode版本无关;这和iOS版本有关。只是为了请求授权,为什么他需要全部3个@亲爱的,别看我!我试过了,但总是在使用时出错。然后我在添加Always后尝试了一下,结果出现了一个错误。然后我在使用中添加了,正如我所说的,整个应用程序开始活跃起来。这个问题只发生在Xcode 10上,对吗?尽管如此,我想把它们全部加起来是不会费事的:@亲爱的,在iOS 11中你实际上可以省略总是开始,但你必须在使用时和总是以及使用时都有。这与我无关 Xcode版本;这与iOS版本有关。
func determineMyCurrentLocation() {
    if CLLocationManager.locationServicesEnabled()
    {
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.distanceFilter = kCLDistanceFilterNone
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

        let authorizationStatus = CLLocationManager.authorizationStatus()
        if (authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways)
        {
            self.locationManager.startUpdatingLocation()
        }
        else if (locationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)))
        {
             self.locationManager.requestAlwaysAuthorization()
        }
        else
        {
            self.locationManager.startUpdatingLocation()
        }
    }
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error while requesting new coordinates")
    }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    self.latitude = locations[0].coordinate.latitude
    self.longitude = locations[0].coordinate.longitude
}