didUpdateLocations instead of didUpdateToLocation
I asume you used the following delegate to get the last position?
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
The delegate above is deprecated in iOS 6. Now the following should be used:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
In order to get the last position, simply get the last object of the array:
[locations lastObject]
In other words, [locations lastObject]
(new delegate) equals newLocation
(old delegate) .
CLLocationManager didUpdateLocations or didUpdateToLocation
I am also faced the same issue in my old project, So I tried the following method, then working fine for me
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
The delegate above is deprecated in iOS 6. Now the following should be used:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
In order to get the last position, simply get the last object of the array:
[locations lastObject]
In other words, [locations lastObject]
(new delegate) equals newLocation
(old delegate)
example
iOS6 and above
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count >= 2) {
oldLocation = [locations objectAtIndex:locations.count-1];
} else {
oldLocation = nil;
}
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}
if you use in iOS6 and below add the following method also
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self locationManager:locationManager didUpdateLocations:[[NSArray alloc] initWithObjects:newLocation, nil]];
}
for additional reference follow this tutorial
didUpdateLocations gets called, but sends wrong location
I figured it out, thanks to the comments below other answers..
I was waiting too short before calling the [locationManager stopUpdatingLocation];
method. The GPS just needed some more time..
So i just waited before doing the stopUpdatingLocation with an NSTimer. That did the job. Thanks to gWiz!
Get location about didUpdateLocations and didUpdateToLocation never call on ios 5.1 and ios6
You do not have to import the CLLocationManagerDelegate
in your .h and AFAIK I think CoreLocation
is not using Google Maps as well, so unless you are using the Google Maps API or something there should be no need to import the GoogleMaps as well.
Try using this instead in your method:
- (void)viewDidLoad
{
[super viewDidLoad];
if( _locationManager == nil )
{
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
}
}
edit:
I realised that you are trying to get the difference in the distance. So maybe just change your
_locationManager = [CLLocationManager new];
to
_locationManager = [[CLLocationManager alloc] init];
and that should solve the problem.
Following your comment, I have checked a little more, and realised that you are using the startUpdatingHeading
method call, therefore you should be using the didUpdateHeading
delegate methods. This is from the Apple CLLocationManager
reference:
Heading events are delivered to the locationManager:didUpdateHeading: method of your delegate. If there is an error, the location manager calls the locationManager:didFailWithError: method of your delegate instead.
Hope this helps! :)
didUpdateLocations never called
where do you start the location update ? for example:
//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
_locationManager.allowsBackgroundLocationUpdates = true // allow in background
return _locationManager
}()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation() // start location manager
}
here is a working conroller code:
also important to to set up Custom iOS Target Properties.
Add these two lines to the Info.plist:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
//
// ViewController.swift
// LocationTest2
import UIKit
import CoreLocation
class ViewController: UIViewController {
//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
// _locationManager.allowsBackgroundLocationUpdates = true // allow in background
return _locationManager
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//allow location use
locationManager.requestAlwaysAuthorization()
print("did load")
print(locationManager)
//get current user location for startup
// if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
// }
}
}
// MARK: - CLLocationManagerDelegate
extension ViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("**********************")
print("Long \(location.coordinate.longitude)")
print("Lati \(location.coordinate.latitude)")
print("Alt \(location.altitude)")
print("Sped \(location.speed)")
print("Accu \(location.horizontalAccuracy)")
print("**********************")
}
}
}
didUpdateLocations delegate method not call in iOS 5.1 and earlier
didUpdateLocations:
did not exist in ios 5 or earlier. If you want to support those ios version then just use the deprecated didUpdateToLocation:fromLocation
. Even though it is deprecated it still works in ios 6 and 7. That's the simplest solution.
You could have delegate methods for both, but that gets more complicated to manage. If you don't need to build track logs in the background then you don't really need didUpdateLocations:
it was created to save battery power while collecting multiple points when running in the background.
Related Topics
Didupdatelocations Instead of Didupdatetolocation
Convert Coordinates to City Name
Cocoapods Could Not Find Compatible Versions for Pod "Firebase/Core" | Cloud_Firestore, Flutter
How to Change the Height of a Uinavigationbar in Storyboard Without Using a Uinavigationcontroller
How to Generate a Barcode from a String in Swift
The App Delegate Must Implement the Window Property If It Wants to Use a Main Storyboard File Swift
How to Put the Uipagecontrol Element on Top of the Sliding Pages Within a Uipageviewcontroller
Google Maps iOS Sdk, Getting Directions Between 2 Locations
Xcode - Symbol(S) Not Found for Architecture X86_64 (iOS Lib)
Grouped Uitableview Remove Outer Separator Line
Disable Magnification Gesture in Wkwebview
How to Make Uiview Animation Sequence Repeat and Autoreverse
Push Notifications Not Being Received on iOS 10, But Working on iOS 9 and Before
How to Listen for All Notifications Sent to the iOS Nsnotificationcenter's Defaultcenter
Opengl Es 2.0 to Video on iPad/Iphone
Destinationviewcontroller Segue and Uinavigationcontroller Swift