Reputation:
Location is not being printed to console. I have had this exact code working before but since updating to Xcode 10 it is not print the users current location into the console.
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)")
}
Upvotes: 1
Views: 4325
Reputation: 119
Step:1 - import
import MapKit
import CoreLocation
Step:2 - use : MKMapViewDelegate,CLLocationManagerDelegate
Step:3 -
var locationManager = CLLocationManager()
var latitude: Double = 0.0
var longitude: Double = 0.0
Step:4 - In ViewDidLoad
self.determineMyCurrentLocation()
Step:5 - define 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}
Upvotes: 1
Reputation: 534885
The problem is your Info.plist. You need all three keys:
Fix that and the app will spring to life. I pasted your code into a project, set up the Info.plist as shown, and it worked fine.
You can actually omit "Always" starting in iOS 11, but you must have both "When In Use" and "Always and When In Use", even if you are planning to ask only for "Always".
The runtime was in fact telling you this all along in the Xcode console, but you apparently weren't looking at it. It says:
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.
Upvotes: 2
Reputation: 527
Have you imported CoreLocation
?
Try to change your locationManager
form
var locationManager: CLLocationManager!
to
let locationManager = CLLocationManager()
and then you don't need it in determineMyCurrentLocation()
I would also let userLocation = CLLocation()
in the top of your class and then update your location.
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)")
}
Upvotes: 1
Reputation: 44
Confirm NSLocationWhenInUseUsageDescription
and NSLocationAlwaysUseUsageDescription
are in your plist file.
Upvotes: 0