未获取 swift 中的用户当前位置 4
Not getting users current location in swift 4
位置未打印到控制台。我以前使用过这个确切的代码,但自从更新到 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:
确认 NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysUseUsageDescription
在您的 plist 文件中。
你导入了吗CoreLocation
?
尝试更改您的 locationManager
表格
var locationManager: CLLocationManager!
至
let locationManager = CLLocationManager()
然后在 determineMyCurrentLocation()
中不需要它
我也会 let userLocation = CLLocation()
在您的 class 顶部,然后更新您的位置。
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。您需要 所有三个键:
修复该问题,应用程序将 spring 恢复正常。我将您的代码粘贴到一个项目中,如图所示设置 Info.plist,它工作正常。
从 iOS 11 开始,您实际上可以省略 "Always",但您必须同时拥有 "When In Use" 和 "Always and When In Use",即使您打算只要求 "Always".
运行时实际上一直在 Xcode 控制台中告诉您这一点,但您显然没有在看它。它说:
The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data.
步骤:1 -导入
import MapKit
import CoreLocation
步骤:2 - 使用:MKMapViewDelegate,CLLocationManagerDelegate
步骤:3 -
var locationManager = CLLocationManager()
var latitude: Double = 0.0
var longitude: Double = 0.0
步骤:4 - 在 ViewDidLoad
self.determineMyCurrentLocation()
步骤:5 - 定义 Func
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
}
Insert in info.plist {Privacy - Location Usage Description , Privacy - Location Always and When In Use Usage Description ,Privacy - Location When In Use Usage Description , Privacy - Location Always Usage Description}
位置未打印到控制台。我以前使用过这个确切的代码,但自从更新到 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:
确认 NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysUseUsageDescription
在您的 plist 文件中。
你导入了吗CoreLocation
?
尝试更改您的 locationManager
表格
var locationManager: CLLocationManager!
至
let locationManager = CLLocationManager()
然后在 determineMyCurrentLocation()
我也会 let userLocation = CLLocation()
在您的 class 顶部,然后更新您的位置。
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。您需要 所有三个键:
修复该问题,应用程序将 spring 恢复正常。我将您的代码粘贴到一个项目中,如图所示设置 Info.plist,它工作正常。
从 iOS 11 开始,您实际上可以省略 "Always",但您必须同时拥有 "When In Use" 和 "Always and When In Use",即使您打算只要求 "Always".
运行时实际上一直在 Xcode 控制台中告诉您这一点,但您显然没有在看它。它说:
The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data.
步骤:1 -导入
import MapKit
import CoreLocation
步骤:2 - 使用:MKMapViewDelegate,CLLocationManagerDelegate
步骤:3 -
var locationManager = CLLocationManager()
var latitude: Double = 0.0
var longitude: Double = 0.0
步骤:4 - 在 ViewDidLoad
self.determineMyCurrentLocation()
步骤:5 - 定义 Func
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
}
Insert in info.plist {Privacy - Location Usage Description , Privacy - Location Always and When In Use Usage Description ,Privacy - Location When In Use Usage Description , Privacy - Location Always Usage Description}