不能迅速获得用户的当前位置4

时间:2018-10-24 00:41:56

标签: swift core-location xcode10

位置未打印到控制台。我之前已经使用过此确切的代码,但是自从更新到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: enter image description here

4 个答案:

答案 0 :(得分:2)

问题出在您的 Info.plist 。您需要所有三个键

enter image description here

修复此问题,该应用程序将栩栩如生。我将您的代码粘贴到一个项目中,如图所示设置了 Info.plist ,它运行良好。

实际上,您可以从iOS 11开始省略“始终”,但是即使您打算只要求“始终”,您也必须同时具有“使用中”和“始终使用中”。

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

  

应用程序的Info.plist必须同时包含NSLocationAlwaysAndWhenInInUseUsageDescription和NSLocationWhenInUseUsageDescription键,并带有向用户说明该应用程序如何使用此数据的字符串值。

答案 1 :(得分:1)

您导入了CoreLocation吗?

尝试更改您的locationManager表单

var locationManager: CLLocationManager!

let locationManager = CLLocationManager()

然后您不需要determineMyCurrentLocation()

我也let 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)")
}

答案 2 :(得分:1)

步骤:1-导入

import MapKit
import CoreLocation

步骤:2-使用:MKMapViewDelegate,CLLocationManagerDelegate

步骤:3-

var locationManager = CLLocationManager()

步骤:4-在ViewDidLoad中

 self.determineMyCurrentLocation()

步骤:5-定义功能

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

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

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")
}
  

在info.plist中插入{隐私-位置使用情况描述,隐私-始终使用时位置使用情况描述,隐私-使用时位置使用情况描述,隐私-位置始终使用情况描述}

答案 3 :(得分:0)

确认 proc sql; create table as select freq, class, subclass, count(subclass) as count from DATASET1 where freq le 9 group by Subclass union all select freq, class, subclass, count(class) as count from DATASET1 where freq ge 10 group by class; NSLocationWhenInUseUsageDescription在您的plist文件中。