I need to get the position and the heading. Once I run my code, I only get the heading updates, location does not update as it doesn't appear in logs.
If I disable heading updates,I get my location coordinates.
If heading updates is on I don't get location.
Has anyone had the same sort of problem?
Here's my set up routine and my methods:
locationManager = [[CLLocationManager alloc]init];
locationManager.headingFilter = 1;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation// fromLocation:(CLLocation *)oldLocation
{NSString *currentLatitude = [[NSString alloc] initWithFormat:#"%g", newLocation.coordinate.latitude];
NSLog(#"currentLatitude = %#", currentLatitude);
NSString *currentLongitude = [[NSString alloc] initWithFormat:#"%g", newLocation.coordinate.longitude];
NSLog(#"currentLongitude = %#", currentLongitude);
NSString *currentHorizontalAccuracy = [[NSString alloc] initWithFormat:#"%g", newLocation.horizontalAccuracy];
NSLog(#"currentHorizontalAccuracy = %#", currentHorizontalAccuracy);
NSString *currentAltitude = [[NSString alloc] initWithFormat:#"%g", newLocation.altitude];
NSLog(#"currentAltitude = %#", currentAltitude);
NSString *currentVerticalAccuracy = [[NSString alloc] initWithFormat:#"%g", newLocation.verticalAccuracy];
NSLog(#"currentVerticalAccuracy = %#", currentVerticalAccuracy);
}- (BOOL)locationManagerShouldDisplayHeadingCalibration: (CLLocationManager *)manager {
return NO;}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
CLLocationDirection trueHeading = newHeading.trueHeading;}
The location has not been updating because the method has been updated in ios 6.0
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
This is how the didUpdateLocations should look like, now it takes an array.
[locations lastObject] this is the last know location in the array. From it you can get the coordinates and else.
Related
I am checking on device not on simulator and I want to fetch user's current location but it always shows location which I have set in edit scheme field from target of Xcode. I am using below code:
- (void)CurrentLocationIdentifier
{
//---- For getting current gps location
// locationManager = [CLLocationManager new];
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if(kCLAuthorizationStatusDenied)
{
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//currentLocation = [locations objectAtIndex:0];
currentLocation=[locations lastObject];
NSLog(#"location:%#",currentLocation);
[locationManager stopUpdatingLocation];
//[locationManager startUpdatingLocation];
geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
if (!(error))
{
placemark = [placemarks objectAtIndex:0];
NSLog(#"\nCurrent Location Detected\n");
NSString *Country = [[NSString alloc]initWithString:placemark.ISOcountryCode];
NSString *state =[[NSString alloc]initWithString:placemark.administrativeArea];
//NSString *state =placemark.administrativeArea;
NSLog(#"%#",Country);
NSLog(#"administrativeArea>>%#",state);
self.stateLabel.text=state;
//[locationManager stopUpdatingLocation];
[[NSUserDefaults standardUserDefaults]setValue:Country forKey:#"countryISOCode"];
}
else
{
}
}];
[locationManager stopUpdatingLocation];
}
How to fetch exact country and state information of user?
I have been trying to fake my location in the iOS simulator but the methods I have been reading about are not working. I have used the debugger to set my fake location, and have made a custom location in Debug->Location->Custom Location, yet I still log 0.000 0.000 for lat and long.
I'm using this code to find current location.
- (IBAction)currentLocationButtonPressed:(UIButton *)sender
{
//Search for pubs and bars in current location.
//Push to tableViewController
NSLog(#"current location Pressed.");
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // Best accuracy
[locationManager startUpdatingLocation];
NSLog(#"%#",[self deviceLocation]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(#"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
}
- (NSString *)deviceLocation {
return [NSString stringWithFormat:#"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}
The delegate is set and I am requesting authorization.
- (void)viewDidLoad {
[super viewDidLoad];
searchLocationTextField.delegate = self;
locationManager.delegate = self;
locationManager = [[CLLocationManager alloc]init];
[locationManager requestWhenInUseAuthorization]; // For foreground access
[locationManager requestAlwaysAuthorization]; // For background access
}
Set locationManager delegate to after allocating it.
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager:didUpdateToLocation:fromLocation: delegate method is deprecated, use locationManager:didUpdateLocations: instead.
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(#"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
I am using CLLocationManger to get device current location. All is going well on simulation. I check the "Allow Location Simulation" and set Sydney, Australia as default location. I got the lat and long of Sydney on simulator that is fine
My problem is that when i try on device I still get the Sydney location however my device current location is India.
My device get the location what ever I set in Xcode.
Here is my code:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 1;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
#pragma mark delegate method which tell the current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
latitudeCurrentLocation = [NSString stringWithFormat:#"%f",newLocation.coordinate.latitude];
longitudeCurrentLocation= [NSString stringWithFormat:#"%f",newLocation.coordinate.longitude];
[locationManager stopUpdatingLocation];
locationManager=nil;
NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
Please suggest what should I should do.
Use this delegate method and check your location address
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
Here is sample code :
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:_locationManager.location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"reverseGeocodeLocation:completionHandler: Completion Handler called!");
if (error){
NSLog(#"Geocode failed with error: %#", error);
return;
}
NSLog(#"placemarks=%#",placemarks);
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(#"placemark.ISOcountryCode =%#",placemark.ISOcountryCode);
NSLog(#"placemark.country =%#",placemark.country);
NSLog(#"placemark.postalCode =%#",placemark.postalCode);
NSLog(#"placemark.administrativeArea =%#",placemark.administrativeArea);
NSLog(#"placemark.locality =%#",placemark.locality);
NSLog(#"placemark.subLocality =%#",placemark.subLocality);
NSLog(#"placemark.subThoroughfare =%#",placemark.subThoroughfare);
NSLog(#"stringlocation:%#",_cityString);
}];
_userLocation = [locations lastObject]; // This will give you co-ordinates
[_locationManager stopUpdatingLocation];
}
When you run this in your device select don't simulate location
I have a weird problem.
I have 2 classes:
BusinessVC
FavoritesVC
In both of them i show the user the current distance from him to some specific point.
In both of the classes i use the EXACT same code for that.
FavoritesVC is presented as modal view controller.
For some reason didUpdateLocations / didUpdateToLocation methods are getting called ONLY in BusinessVC and NOT in FavoritesVC.
I'm implementing CLLocationManagerDelegate delegate in both classes.
The code I'm using:
-(void)viewDidLoad {
[super viewDidLoad];
[self locatedMe];
}
-(void) locatedMe{
NSLog(#"locateMe");
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDistanceFilter:kCLDistanceFilterNone]; // whenever we move
[_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters]; // 100 m
[_locationManager startUpdatingLocation];
}
// For iOS6
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation * newLocation = [locations lastObject];
NSNumber *lat = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude];
NSNumber *lon = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude];
self.userLocation = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]];
}
// For earlier then iOS6
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSNumber *lat = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude];
NSNumber *lon = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude];
self.userLocation = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]];
NSLog(#"%#",self.userLocation);
}
OK! Fixed that by running the code on main thread using this code:
-(void) locateMe {
NSLog(#"locateMe");
dispatch_async(dispatch_get_main_queue(), ^{
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // 100 m
[_locationManager startUpdatingLocation];
[_locationManager startUpdatingHeading];
});
}
I'm receiving an array of placemarks from :
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
..
}
when printing to console using NSLog, the placemark gives me data in the form of:
..HIDDEN.. St., Brighton, MA 02135, United States #
<+42.HIDDEN,-71.HIDDEN> +/- 100.00m
But when using the following code, I get Boston as the city
[placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey]);
Brighton is a part of Boston, but how can the console log be showing me a more specific location than the City key..?
It looks like the best you can do with placemarks is
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
This post may be of some use iOS: get Accurate Reverse Geocoding using CLLocationManager?
It's also worth noting that using Boston in place of Brighton is still a valid address for the same location.
http://support.apple.com/kb/HT1975
This worked for me:
- (void) resetLocationData{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
NSLog(#"LAT: %f", [locationManager location].coordinate.latitude);
NSString *latitude = [NSString stringWithFormat:#"%f", [locationManager location].coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", [locationManager location].coordinate.longitude];
NSString *latlong = [NSString stringWithFormat:#"%#,%#", latitude, longitude];
NSLog(#"LONG: %f", [locationManager location].coordinate.longitude);
NSLog(#"Combined Location: %#", latlong);
[[locationManager location]timestamp];
}
#pragma mark - CLLOCATION STUFF
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.currentLocation = newLocation;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:self.currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(#"Placemark is %#", placemarks);
CLPlacemark *place = [placemarks objectAtIndex:0];
NSLog(#"placemark 0: %#", place);
// locationLabel.text = placemarks;
}];
if(newLocation.horizontalAccuracy <= 100.0f) {
[locationManager stopUpdatingLocation];
}
}
LOG OUTPUT:
2013-01-13 23:23:05.508 CLLocationTest[47617:907] Placemark is (
"XX XXXXXX Rd, XX XXXXXX Rd, Brighton, MA 02135, United States # <+42.XXXXXX,-71.XXXXXXX> +/- 100.00m"