Created
March 18, 2014 05:25
-
-
Save ArchieGoodwin/9614019 to your computer and use it in GitHub Desktop.
Correct realisation of didUpdateToLocations and get locality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations | |
{ | |
CLLocation *newLocation = locations.lastObject; | |
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; | |
if (locationAge > 5.0) return; | |
if (newLocation.horizontalAccuracy < 0) return; | |
// Needed to filter cached and too old locations | |
//NSLog(@"Location updated to = %@", newLocation); | |
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude]; | |
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]; | |
double distance = [loc1 distanceFromLocation:loc2]; | |
_currentLocation = newLocation; | |
if(distance > 20) | |
{ | |
NSLog(@"SIGNIFICANTSHIFT"); | |
//[self getLocality]; | |
} | |
[[NSNotificationCenter defaultCenter] postNotificationName:DKALocationUpdated object:self userInfo:nil]; | |
} | |
-(void)getLocality | |
{ | |
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; | |
[geocoder reverseGeocodeLocation:[[CLLocation alloc] initWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude] completionHandler:^(NSArray *placemarks, NSError *error) { | |
if (error == nil) { | |
NSLog(@"%@", [NSString stringWithFormat:@"%@,%@", [[placemarks objectAtIndex:0] locality], [[placemarks objectAtIndex:0] country]]); | |
_placemark = [placemarks objectAtIndex:0]; | |
NSLog(@"%@, %@, %@, %@, %@", [_placemark.addressDictionary objectForKey:@"CountryCode"], [_placemark.addressDictionary objectForKey:@"Country"], [_placemark.addressDictionary objectForKey:@"City"],_placemark.addressDictionary, _placemark); | |
//self.location.address = ABCreateStringWithAddressDictionary([[placemarks objectAtIndex:0] addressDictionary], NO); | |
} | |
[[NSNotificationCenter defaultCenter] postNotificationName:DKALocationMuchUpdated object:self userInfo:nil]; | |
//[MBProgressHUD hideAllHUDsForView:self.view animated:YES]; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment