I'm developing a location-based mobile application. My app updates the server with user's current location even when the app is killed/terminated. My app was working fine before integrating google place picker API. After installing google place picker API for ios, my app updates the location for about 5 to 10 minutes only and then it stops updating location.
//My code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize Location Manager
sLocationManager;
//the system automatically relaunches the application into the background if a new event arrives.
// The options dictionary passed to the application:didFinishLaunchingWithOptions: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event
if (launchOptions[UIApplicationLaunchOptionsLocationKey]) {
NSLog(#"---UIApplicationLaunchOptionsLocationKey");
sLocationManager.locationUpdatedInBackground = ^(NSArray<CLLocation *> *locations) {
NSLog(#"---setLocationUpdatedInBackground");
[sLocationModule saveNewLocations];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:300];
notification.alertBody = #"Your location was changed. Please run application for tracking your geoposition with best accuracy.";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[sLocationManager startMonitoringSignificantLocationChanges];
};
}else {
sLocationManager.locationUpdatedInForeground = ^(NSArray<CLLocation *> *locations) {
NSLog(#"---setLocationUpdatedInForeground");
[sLocationModule saveNewLocations];
};
sLocationManager.locationUpdatedInBackground = ^(NSArray<CLLocation *> *locations) {
NSLog(#"---setLocationUpdatedInBackground");
[sLocationModule saveNewLocations];
[sLocationManager startDeferredLocationUpdates];
};
// Notifications
[application cancelAllLocalNotifications];
}
return YES;
//location manager
#pragma mark - CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)aLocations {
NSString *infoMessage = [NSString stringWithFormat:#"%#", aLocations];
NSLog(#"%#", infoMessage);
locations = [#[] mutableCopy];
NSLog(#"locations=%#",locations);
[aLocations enumerateObjectsUsingBlock:^(CLLocation * _Nonnull location, NSUInteger idx, BOOL * _Nonnull stop) {
// Check for location errors (speed -1.00 mps / course -1.00)
// if (location.speed >= 0 &&
// location.course >= 0) {
if (locations.count > 0) {
if ([location.timestamp timeIntervalSinceDate:[locations lastObject].timestamp] > dLocationManagerDistanceFilter ||
[location distanceFromLocation:[locations lastObject]] > dLocationUpdatesUntilTimeout) {
[locations addObject:location];
NSLog(#"location.timestamp>dLocationManagerDistanceFilter**** locations=%#",locations);
}
} else
[locations addObject:location];
NSLog(#"else***locations=%#",locations);
//save location in userdefaults
NSString *latitudeValue = [NSString stringWithFormat:#"%f", location.coordinate.latitude];
NSString *longitudeValue = [NSString stringWithFormat:#"%f",location.coordinate.longitude];
NSLog(#"new location is recieved Lat : %# Long : %#",latitudeValue,longitudeValue);
[User setUserLat:latitudeValue];
[User setUserLon:longitudeValue];
// }
}];
if ([self isInBackground]) {
NSLog(#"app is in background***");
if (self.locationUpdatedInBackground) {
backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
}];
self.locationUpdatedInBackground(locations);
[delegate locationManagerDidUpdateLocationInBackground];
[[NSNotificationCenter defaultCenter] postNotificationName:nLocationManagerDidUpdateLocationInBackgroundNotification
object:nil
userInfo:nil];
[self endBackgroundTask];
}
} else {
if (self.locationUpdatedInForeground) {
if([[NSUserDefaults standardUserDefaults]objectForKey:#"userIdKey"]){
self.locationUpdatedInForeground(locations);
[delegate locationManagerDidUpdateLocationInForeground];
[[NSNotificationCenter defaultCenter] postNotificationName:nLocationManagerDidUpdateLocationInForegroundNotification
object:nil
userInfo:nil];
}
}
}
}
code for adding place picker
#pragma mark - Place picker delegate methods
- (IBAction)pickPlace:(id)sender {
//dismiss the keyboard
[tfLocation resignFirstResponder];
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001);
GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
coordinate:southWest];
GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:viewport];
_placePicker = [[GMSPlacePicker alloc] initWithConfig:config];
[_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
if (error != nil) {
NSLog(#"Pick Place error %#", [error localizedDescription]);
return;
}
if (place != nil) {
NSLog(#"Place name %#", place.name);
NSLog(#"Place address %#", place.formattedAddress);
NSLog(#"Place attributions %#", place.attributions.string);
// NSString *latitudeValue = [NSString stringWithFormat:#"%f", locationManager.location.coordinate.latitude];
// NSString *longitudeValue = [NSString stringWithFormat:#"%f",locationManager.location.coordinate.longitude];
tfLocation.text=place.formattedAddress;
locationPoint=[NSString stringWithFormat:#"%f,%f", place.coordinate.latitude,place.coordinate.longitude];
} else {
NSLog(#"No place selected");
}
}];
}
// Add a UIButton in Interface Builder, and connect the action to this function.
- (IBAction)getCurrentPlace:(UIButton *)sender {
[_placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *placeLikelihoodList, NSError *error){
if (error != nil) {
NSLog(#"Pick Place error %#", [error localizedDescription]);
return;
}
self.nameLabel.text = #"No current place";
self.addressLabel.text = #"";
if (placeLikelihoodList != nil) {
GMSPlace *place = [[[placeLikelihoodList likelihoods] firstObject] place];
if (place != nil) {
self.nameLabel.text = place.name;
self.addressLabel.text = [[place.formattedAddress componentsSeparatedByString:#", "]
componentsJoinedByString:#"\n"];
}
}
}];
}
// Present the autocomplete view controller when the button is pressed.
- (IBAction)onLaunchClicked:(id)sender {
GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
acController.delegate = self;
[self presentViewController:acController animated:YES completion:nil];
}
// Handle the user's selection.
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
[self dismissViewControllerAnimated:YES completion:nil];
// Do something with the selected place.
NSLog(#"Place name %#", place.name);
NSLog(#"Place address %#", place.formattedAddress);
NSLog(#"Place attributions %#", place.attributions.string);
}
- (void)viewController:(GMSAutocompleteViewController *)viewController
didFailAutocompleteWithError:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
// TODO: handle the error.
NSLog(#"Error: %#", [error description]);
}
// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
// Turn the network activity indicator on and off again.
- (void)didRequestAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)didUpdateAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Can anyone tell, what am I doing wrong here? What is the best to use place picker without affecting background location updates or is there any other way to pick place/location just like google place picker in ios?
Please help me out or tell me the other solution.
thank you!
Related
I've created a helper class for my location needs so i don't violate the DRY principle. The class looks like this:
Location.h
#interface Location : NSObject <CLLocationManagerDelegate>{
CLLocationManager *manager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
-(float)latitude;
-(float)longitude;
-(NSString *)postalcode;
Location.m
#implementation Location{
float latitude;
float longitude;
NSString *postalcode;
}
-(id)init{
NSLog(#"Hallo");
[self setupLocationManager];
return self;
}
-(float)latitude{
return latitude;
}
-(float)longitude{
return longitude;
}
-(NSString *)postalcode{
return postalcode;
}
-(void)setupLocationManager{
manager = [[CLLocationManager alloc] init];
[manager requestWhenInUseAuthorization];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
manager.distanceFilter = 100;
[manager startUpdatingLocation];
geocoder = [[CLGeocoder alloc] init];
}
#pragma mark - CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error
{
NSLog(#"Error: %#", error);
NSLog(#"Failed to get location! :(");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Location: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
latitude = currentLocation.coordinate.latitude;
longitude = currentLocation.coordinate.longitude;
}
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
postalcode = [NSString stringWithFormat:#"%#",placemark.postalCode];
/*
self.address.text = [NSString stringWithFormat:#"%# %#\n%# %#\n%#\n%#",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
*/
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
}
#end
When i in my ViewController tries to create an instance of Location and set latitude and longitude labels, in the viewDidLoad method, the labels are sat to 0.00000.
Apparently it takes around half a second for Location to get the coordinates.
I've tried using
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self setCoordinateLabels];
});
But that just seems very hacky and can't possibly be the best practise? So is there any way i could do this a better way?
Well - that is very hacky. Why don't you forward your delegate methods call?
locationManager:didUpdateToLocation:
(Btw. this is a legacy function)
Tells you when the first location is set. You can just have an array of delegates on your Location class and call every delegate when it's time.
Here is an example with blocks:
static NSMapTable *listenerBlocks;
+ (void)addListener:(NSObject *)listener listenerBlock:(void (^)())listenerBlock
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
listenerBlocks =
[NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory
valueOptions:NSMapTableStrongMemory];
});
if (listenerBlock && listener) {
[listenerBlocks setObject:listenerBlock forKey:listener];
}
}
+ (void)removeListener:(NSObject *)listener
{
if (listener) {
[listenerBlocks removeObjectForKey:listener];
}
}
In your locationManager:didUpdateToLocation: you then just call
NSArray *allBlocks = [[listenerBlocks objectEnumerator] allObjects];
for(void (^listenerBlock)(NSString *) in allBlocks)
{
listenerBlock();
}
at the end
In the class that needs updates for the labels (e.g. myLabel):
[Location addListener:self listenerBlock:^{
dispatch_async(dispatch_get_main_queue(),^{
//myLabel.text = //... update your label here
[self setCoordinateLabels]; // as from your code above..
});
}];
Hi i have below code which works fine on iPod touch 5, but same code is not working on iPhone 6, i done research on same issue but i have not found anything useful. both devices have latest iOS.
Both devices have iOS 8
// MapViewController.m
// SidebarDemo
//
// Created by Simon on 30/6/13.
// Copyright (c) 2013 Appcoda. All rights reserved.
//
#import "PetFinderViewController.h"
#import "SWRevealViewController.h"
#interface PetFinderViewController ()
#end
#implementation PetFinderViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1] ;
self.title = #"Pet Finder";
// Change button color
//_sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = #selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
// Check if beacon monitoring is available for this device
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Monitoring not available" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil]; [alert show]; return;
}
else
{
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Create a NSUUID
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"ebefd083-70a2-47c8-9837-e7b5634df524"];
// Setup a new region AND start monitoring
str_beaconIdentifier = #"in.appstute.marketing";
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:str_beaconIdentifier];
self.myBeaconRegion.notifyEntryStateOnDisplay = YES;
self.myBeaconRegion.notifyOnEntry = YES;
self.myBeaconRegion.notifyOnExit = YES;
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
self.lbl_rangeStatus.text = #"Finding Your Pet";
self.lbl_regionStatus.text = #"";
self.lbl_distance.text = #"";
}
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (![CLLocationManager locationServicesEnabled]) {
NSLog(#"Couldn't turn on ranging: Location services are not enabled.");
}
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
NSLog(#"Couldn't turn on monitoring: Location services not authorised.");
[self.locationManager requestAlwaysAuthorization];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Core Location Delegate methods
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
UILocalNotification *notify = [[UILocalNotification alloc] init];
notify.alertBody = #"You are near your Pet's region.";
notify.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notify];
// We entered a region, now start looking for our target beacons!
//self.statusLabel.text = #"Finding beacons.";
self.lbl_rangeStatus.text = #"Pet Found";
self.lbl_regionStatus.text = #"Status : Entered Region";
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
//Opening camera
/*if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
//[self presentModalViewController:imagePicker animated:YES];
[self presentViewController:imagePicker animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Camera Unavailable"
message:#"Unable to find a camera on your device."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}*/
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
UILocalNotification *notify = [[UILocalNotification alloc] init];
notify.alertBody = #"You are far away from your Pet's region.";
notify.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notify];
// Exited the region
//self.statusLabel.text = #"None found.";
self.lbl_rangeStatus.text = #"Pet Not Found";
self.lbl_regionStatus.text = #"Status : Exited Region";
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
CLBeacon *foundBeacon = [beacons firstObject];
// Retrieve the beacon data from its properties
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:#"%#", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:#"%#", foundBeacon.minor];
NSLog(#"uuid=%#, major=%#, minor=%#",uuid, major, minor);
self.lbl_regionStatus.text = #"Status : Entered Region";
if(foundBeacon.proximity==CLProximityImmediate)
{
NSLog(#"Immediate");
//self.Lb_proxomity.text = #"Immediate";
}
else if (foundBeacon.proximity==CLProximityNear)
{
NSLog(#"Near");
//self.Lb_proxomity.text = #"Near";
}
else if(foundBeacon.proximity==CLProximityFar)
{
NSLog(#"Far");
//self.Lb_proxomity.text = #"Far";
}
else if(foundBeacon.proximity==CLProximityUnknown)
{
NSLog(#"Unknown");
//self.Lb_proxomity.text = #"Unknown";
}
float actualDistance = foundBeacon.accuracy/10;
NSLog(#"Distance = %f",actualDistance);
if(actualDistance >= 0.0)
{
self.lbl_distance.text = [NSString stringWithFormat:#"Distance : %.2f m",actualDistance];
}
//self.Lb_meter.text = [NSString stringWithFormat:#"%.2f",foundBeacon.accuracy];
//self.Lb_centimeter.text = [NSString stringWithFormat:#"%.2f",(foundBeacon.accuracy*100)];
//[self presentExhibitInfoWithMajorValue:foundBeacon.major.integerValue];
//Calling this method to display strength for distance between user and the pet
[self fn_showStrengthForDistanceBetweenUserAndPet:actualDistance];
}
#pragma mark - Check Background App Refresh status
-(BOOL)CanDeviceSupportAppBackgroundRefresh
{
// Override point for customization after application launch.
if ([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusAvailable) {
NSLog(#"Background updates are available for the app.");
return YES;
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied)
{
NSLog(#"The user explicitly disabled background behavior for this app or for the whole system.");
return NO;
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted)
{
NSLog(#"Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.");
return NO;
}
return NO;
}
#pragma mark - Check if monitoring region failed
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(#"monitoringDidFailForRegion - error: %#", [error localizedDescription]);
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLBeaconRegion *)region{
if (state == CLRegionStateInside) {
//Start Ranging
[manager startRangingBeaconsInRegion:region];
}
else{
//Stop Ranging
[manager stopRangingBeaconsInRegion:region];
}
}
#end
I suspect you are having authorization issues on your iPhone. Set a breakpoint or add NSLog statements to make sure this line is getting called:
[self.locationManager requestAlwaysAuthorization];
Do you get prompted? If not, uninstall and reinstall.
Also, check in setting that Bluetooth and Location services are enabled on the phone, and check settings on your app to see that location services are actually enabled for it.
You need to set one of
NSLocationAlwaysUsageDescription
or
NSLocationWhenInUseUsageDescription when requesting location updates (even with iBeacons).
If you don't, in iOS 8, this will fail silently.
I have this code which send user notifications every time the distance between an event that occur in a local JSON file and his current location is < 100 meter asking him whether he is at that event or not , when he presses on yes then that event will be marked as attended. the thing is I tried to do that by using some code i found online but I'm not sure if it is the right way to do it, anyway i tested it on my iPhone and what happened is when i arrived to an event location it kept sending unstoppable notifications and when i try to press yes or no nothing actually happen it keeps sending these notifications. Can anyone plz explain for me what is going wrong, I'm not very familiar with Xcode and objective-C language. The code i used is shown below.
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// load Core Data
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
NSLog(#"No NSManagedObjectContext generated");
}
NSLog(#"DelegateApp Managed Object Context = %#", context);
[[DataManager sharedInstance] setManagedObjectContext:context];
[[DataManager sharedInstance] initDataBase];
return YES;
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
[self showAlarm:notification.alertBody];
NSLog(#"AppDelegate didFinishLaunchingWithOptions");
application.applicationIconBadgeNumber = 0;
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[self showAlarm:notification.alertBody];
application.applicationIconBadgeNumber = 0;
NSLog(#"AppDelegate didReceiveLocalNotification %#", notification.userInfo);
}
- (void)showAlarm:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"SPOT IT"
message:text delegate:self
cancelButtonTitle:#"YES"
otherButtonTitles:#"NO",nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"NO"])
{
NSLog(#"Button 2 was selected.");
}
else if([title isEqualToString:#"YES"])
{
NSLog(#"Button 1 was selected.");
// attended
[_eachEvent setHasATTENDED:[NSNumber numberWithBool:TRUE]];
// save
NSError *error = nil;
if (![_managedObjectContext save:&error])
{
NSLog(#"Error in saving");
}
}
}
in my DataManager class:
- (void) locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
//NSLog(#"MV_EventsDataManager new location: latitude %+.6f, longitude %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
for (Event *musicevent in [self loadTodaysEvents]) {
// distance
CLLocationDegrees lat = [musicevent.lat doubleValue];
CLLocationDegrees lon = [musicevent.longi doubleValue];
CLLocation *evLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
double distance = [evLocation distanceFromLocation:newLocation];
//NSLog(#"\t Calculated KM %# to %#", [NSString stringWithFormat:#"%.1f",(distance/1000.0)], musicevent.title);
// CLOSE !
if (distance <= 100) {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Are u there!";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1; // increment
// NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Object 1", #"Key 1", #"Object 2", #"Key 2", nil];
// localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
}
Depending on how you setup the location manager, the delegate method locationManager:didUpdateToLocation:fromLocation: will typically be called once per second with location updates. So your code is posting local notifications over and over. You need to keep track of when you've posted a notification so you can avoid posting duplicates.
Am starting to do reverse geocoding, and the _geocoder instance variable gets initialized OK, but no data gets passed to the _placemark object. Here is the error log:
2013-12-16 14:00:12.040 MyLocations[10555:70b] *** Going to geocode
2013-12-16 14:00:12.041 MyLocations[10555:70b] *** Found placemarks: (null), error: Error Domain=kCLErrorDomain Code=8 "The operation couldn’t be completed. (kCLErrorDomain error 8.)"
Am assuming that the locationManager:didFailWithError: method is getting called, but don't understand the error code.
Here is the code for CurrentLocationViewController.m
#import "CurrentLocationViewController.h"
#interface CurrentLocationViewController ()
#end
#implementation CurrentLocationViewController {
CLLocationManager *_locationManager;
CLLocation *_location;
BOOL _updatingLocation;
NSError *_lastLocationError;
CLGeocoder *_geocoder;
CLPlacemark *_placemark;
BOOL _performingReverseGeocoding;
NSError *_lastGeocodingError;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if((self = [super initWithCoder:aDecoder])) {
_locationManager = [[CLLocationManager alloc] init];
_geocoder = [[CLGeocoder alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self updateLabels];
[self configureGetButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)getLocation:(id)sender
{
if(_updatingLocation) {
[self stopLocationManager];
} else {
_location = nil;
_lastLocationError = nil;
[self startLocationManager];
}
[self updateLabels];
[self configureGetButton];
}
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError %#", error);
if (error.code == kCLErrorLocationUnknown) {
return;
}
[self stopLocationManager];
_lastLocationError = error;
[self updateLabels];
[self configureGetButton];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
if ([newLocation.timestamp timeIntervalSinceNow] <- 5.0) {
return;
}
if (newLocation.horizontalAccuracy < 0) {
return;
}
if (!_performingReverseGeocoding) {
NSLog(#"*** Going to geocode");
_performingReverseGeocoding = YES;
[_geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"*** Found placemarks: %#, error: %#", placemarks, error);
_lastGeocodingError = error;
if (error == nil && [placemarks count] > 0) {
_placemark = [placemarks lastObject];
} else {
_placemark = nil;
}
_performingReverseGeocoding = NO;
[self updateLabels];
}];
}
}
-(void)updateLabels
{
if (_location != nil) {
self.latitudeLabel.text = [NSString stringWithFormat:#"%.8f", _location.coordinate.latitude];
self.longitudeLabel.text = [NSString stringWithFormat:#"%.8f", _location.coordinate.longitude];
self.tagButton.hidden = NO;
self.messageLabel.text = #"";
} else {
self.latitudeLabel.text = #"";
self.longitudeLabel.text = #"";
self.addressLabel.text = #"";
self.tagButton.hidden = YES;
NSString *statusMessage;
if (_lastLocationError == nil) {
if ([_lastLocationError.domain isEqualToString:kCLErrorDomain] && _lastLocationError.code == kCLErrorDenied) {
statusMessage = #"Location Services Disabled";
} else {
statusMessage = #"Error Getting Location";
}
} else if (![CLLocationManager locationServicesEnabled]) {
statusMessage = #"Location Services Disabled";
} else if (_updatingLocation) {
statusMessage = #"Searching...";
} else {
statusMessage = #"Press the Button to Start";
}
self.messageLabel.text = statusMessage;
}
}
-(void)configureGetButton
{
if (_updatingLocation) {
[self.getButton setTitle:#"Stop"
forState:UIControlStateNormal];
} else {
[self.getButton setTitle:#"Get My Location"
forState:UIControlStateNormal];
}
}
-(void)startLocationManager
{
if ([CLLocationManager locationServicesEnabled]) {
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[_locationManager startUpdatingLocation];
_updatingLocation = YES;
}
}
-(void)stopLocationManager
{
if (_updatingLocation) {
[_locationManager stopUpdatingLocation];
_locationManager.delegate = nil;
_updatingLocation = NO;
}
}
#end
kCLErrorDomain error 8 means Apple geoloc servers know nothing about the provided location. When doing reverse geocoding: lat/long has no address match, when forward geocoding: the address is not known to link to lat/long
I am working on an application which aims to display on a map the position of the iPhone during an activity (adding annotations and drawing line between them).
To do this, I use a Location Manager which get new positions and notify the MapDisplayViewController. When using with the simulator I got good results as you can see here http://www.youtube.com/watch?v=ESLIYSU_Mqw but with an iPhone I got weird result such 200 meters wrong.
I also use a MKMapView to display the device position. with these settings (next code block) I got that http://grab.by/mgUU.
If I show the user location I got different results, it is normal ?
- (void)setupMap {
self.mapView.delegate = self;
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
[self.mapView setUserInteractionEnabled:NO];
[self.mapView setShowsUserLocation:NO];
[self.mapView setZoomEnabled:NO];
[self.waitingView start];
}
Here screenshots with a real device:
background: http://grab.by/mgN2
no background: http://grab.by/mgNc
edit: the difference come froms I used for these 2 cases, 2 different ways of getting current location of the device are used:
the handle from the mapView when the app is active
the handle from the Location Manager when the app is in background
Does it a good idea to mix both locations system ?
I also test this solution CLLocationManager and accuracy issues - any experiences? but I got the same result.
Thanks ;)
Setup of Location Manager:
- (void)setup {
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.isUpdatingLocation = NO;
}
Notification: when adding location in background:
- (void)doUpdateWithLocation:(CLLocation *)location {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundUpdateTask];
NSLog(#"%s do update in background %#", __PRETTY_FUNCTION__, location);
if (self.userLocationFound == NO && self.isUpdatingLocation == YES) {
self.startCoordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
self.userLocationFound = YES;
}
else if (self.isUpdatingLocation) {
static int distance;
ABGPSPosition *position = [[ABGPSPosition alloc] init];
position.startPoint = MKMapPointForCoordinate(self.startCoordinate);
position.endPoint = MKMapPointForCoordinate(location.coordinate);
//INFO: Get the distance between this new point and the previous point.
CLLocationDistance metersApart = MKMetersBetweenMapPoints(position.startPoint, position.endPoint);
if (metersApart > MINIMUM_DELTA_METERS) {
MKMapPoint points[2] = {position.startPoint, position.endPoint};
distance += metersApart;
NSLog(#"%s - %d", __PRETTY_FUNCTION__, distance);
NSInteger count = [self.measurementArray count];
[self willChange:NSKeyValueChangeInsertion
valuesAtIndexes:[NSIndexSet indexSetWithIndex:count] forKey: #"measurementArray"];
[self.measurementArray insertObject:location atIndex:count];
[self didChange:NSKeyValueChangeInsertion
valuesAtIndexes:[NSIndexSet indexSetWithIndex:count] forKey: #"measurementArray"];
self.startCoordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
}
}
[self endBackgroundUpdateTask];
});
}
MapDisplayViewController get the new location:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:keyPathMeasurementArray]) {
if ([change[NSKeyValueChangeKindKey] intValue] == NSKeyValueChangeInsertion) {
NSIndexSet *insertedIndexSet = change[NSKeyValueChangeIndexesKey];
[insertedIndexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
CLLocation *location = self.locationManager.measurementArray[idx];
MKUserLocation *userLocation = [[MKUserLocation alloc] init];
[userLocation setCoordinate:location.coordinate];
NSLog(#"%s - didUpdateUserLocation", __PRETTY_FUNCTION__);
[self.locationBackgroundList addObject:userLocation];
NSLog(#"%s | %#", __PRETTY_FUNCTION__, self.locationBackgroundList);
}];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
MapDisplayViewController adding Annotation the new map view:
- (void)handleAddLocations {
NSLog(#"%s | %#", __PRETTY_FUNCTION__, self.locationBackgroundList);
if ([self.locationBackgroundList count] > 0) {
for (MKUserLocation *backgroundUserLocation in self.locationBackgroundList) {
{
LocAnnotation* annotation = [[LocAnnotation alloc] initWithCoordinate:backgroundUserLocation.coordinate];
NSLog(#"%s %#", __PRETTY_FUNCTION__, [backgroundUserLocation description]);
[self.mapView addAnnotation:annotation];
}
[self mapView:self.mapView didUpdateUserLocation:backgroundUserLocation];
}
NSString *message = [NSString stringWithFormat:#"%s | Annotation number: %d", __PRETTY_FUNCTION__, [self.locationBackgroundList count]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Debug" message:message delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[self.locationBackgroundList removeAllObjects];
self.locationBackgroundList = [[NSMutableArray alloc] init];
}
}
Fixed you have to use only one way to locate your device with the CLLocationManager or the MapView. ;)