I am trying to find User's current location using Location Manager. Below is my code in viewdidload
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if(IS_OS_8_OR_LATER)
{
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
and in didUpdateLocations
self.userLocation = locations.lastObject;
if (userLocation != nil)
{
NSLog(#"sdsd %f %f", userLocation.coordinate.longitude, userLocation.coordinate.latitude);
[locationManager stopUpdatingLocation];
}
Am getting confirmation dialog and users location as set in simulation but not real time location even in device. I configured all settings in both device as well as xcode but still getting default location only.
Please let me know what should I do to get my realtime location and not simulation location?
The simulator does not receive your current location from the operating system. To debug you can select Debug->Location->City Bicycle Ride in the simulator. This will simulate a bike ride around the block.
On the device it can take a while to get your current location if you are indoors. Turning on bluetooth and WIFI help iOS get a location faster.
Open your xCode go to Product-> Scheme-> Edit Scheme and then here inside Run select Core location and check the Allow location simulation. May be it will work.
Related
i'm developing an iOS application using CLLocationManager to get current user location.
When i'm out of the building it's work well but i'm come to in the building delgate of CLLocationManager always return 0 to me.
Can i get exactly the location when i'm in the building?
I have checked on google maps and apple maps it's aways work well
Plz help me.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLLocationAccuracyBest;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
if(IS_OS_8_OR_LATER) {
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
If you are inside a building then you are unlikely to be able to receive a GPS signal. Unless your building is a significant public place (airport, shopping mall) that has been added to Apple's indoor mapping then you will not get a location while indoors.
I've used location services in many apps, but this new app has a problem on iOS 8.0 and up. I am not getting the notification on the app's first load prompting to allow location services. However, on my iOS 7.1 device I get the prompt.
Here is what I have in my appDelegate in didFinishLaunchingWithOptions:
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self initializeRegionMonitoring];
And the initializeRegionMonitoring method is:
-(void) initializeRegionMonitoring {
NSLog(#"initialize region monitoring");
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// radius of region being monitored
CLLocationDistance radius = 25; // 20 metre sensitivity
CLLocationCoordinate2D coordinate;
coordinate.latitude = 25.886099;
coordinate.longitude = -80.165124;
self.someRegion = [[CLCircularRegion alloc] initWithCenter:coordinate radius:radius identifier:#"Qualex"];
self.someRegion.notifyOnEntry = YES;
self.someRegion.notifyOnExit = YES;
[self.locationManager startMonitoringForRegion:self.someRegion];
// notify changes when the device has moved x meters
self.locationManager.distanceFilter = 20; // or set to 20 meters
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
I also have set the NSLocationAlwaysUsageDescription in my info.plist, so there must be something I've forgotten, right? Thanks for the help!
Edit:
I'm also registering for remote notifications at the same time, that's never been a problem before but I thought it might be some useful extra information.
Also, when terminated the app then re-run the notification to allow location services pops up, but goes away instantly. Just flashes on the screen. I have no idea why it would dismiss without clicking one of the options on the alert.
Before iOS 8, you could request location permission simply by instantiating a CLLocationManager object and attempting to start location tracking. On iOS 8 and above, this does not present a permission prompt; you must request authorization manually with either the requestAlwaysAuthorization or requestWhenInUseAuthorization methods.
That being said, on iOS 8, if you call either of those methods, then the CLLocationManager instance on which you called it is released, the location permission prompt will dismiss itself. What's happening here is that you are creating a location manager, requesting permission (iOS starts to try to present the permission dialog), calling initializeRegionMonitoring, and setting the self.locationManager property to a new location manager instance. This causes the first one you created to get released by ARC, so the permission prompt is dismissed before it even gets a chance to appear.
Removing the line self.locationManager = [[CLLocationManager alloc] init]; in your initializeRegionMonitoring method should fix the issue.
Ive been struggling with a way to retrieve information periodically from a BT device. My bluetooth device is located in a vehicle typically, so my question is if its possible to use say... (if user traveling > 10km/h) to run a task. Or on major location change.
Is is possible to get a really course location that I would be able to use to get a general idea of wether the user is moving? I only need it to trigger once every couple days(while user is driving). The user never interacts with my app after initial setup.
Thanks.
Implementation of cmyr's suggestion:
CLLocationManager *locationManager;
int badge_count = 0;
- (void)startSignificantChangeUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.pausesLocationUpdatesAutomatically = YES;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[locationManager requestAlwaysAuthorization];
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 500; // meters
[locationManager allowDeferredLocationUpdatesUntilTraveled:501 timeout:-1];
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
badge_count++;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge_count];
NSLog(#"Location Event WOOT!");
}
Unfortunately I cannot get the event to trigger. I have added Location updated to the apps plist.
The above code is contained inside my app delegate.m file
Core Location has a set of APIs for specifically this use-case, which Apple refers to as Significant Location Change Monitoring.
From the documentation:
The significant-change location service delivers updates only when there has been a significant change in the device’s location, such as 500 meters or more.
This API only updates your location when and if you've traveled the specified distance. It does not provide constant updates. If you need constant updates, you will have to use the standard location service.
I want that at fist 30 minutes my application will using my GPS in the background so i do the following:
CLLocationManager *locationManager = nil;
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLLocationAccuracyBest;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;
// Do any additional setup after loading the view from its nib.
[locationManager requestAlwaysAuthorization];
After 30 minutes i want that my application will run only in foreground...
How can i remove the thread which using my gps in background?
As i understood i need to add [locationManager stopUpdatingLocation] and [locationManager requestWhenInUseAuthorization];
it works but in the app settings under privacy -> location services- > then there is 3 options: 1)never 2) While using the app 3)Always and even the app not running in the background then the third option is selected still 3)Always I want that the setting will change to "While using the app"
The authorisation process is separate to actually requesting location updates be delivered to your CLLocationManagerDelegate.
Once you have been granted permission to use location (either always or when in use) you can then use [locationManager startUpdatingLocation] and [locationManager stopUpdatingLocation] to control whether location updates are delivered to your delegate.
So, the short answer is call [locationManager stopUpdatingLocation] after 30 minutes.
when I'm trying to show the user location on my map it shows the location is somewhere in the ocean (lat = 0.000 , lon = 0.000) , I have set the simulated location to be in london and still nothing.
when i try to compile the app on my iphone its doesn't ask for permission to get user current location , just when i enable the app for using location services in the iphone settings , the app shows the user current location.
any idea why it does it?
this is my viewDidLoad :
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager = [[CLLocationManager alloc] init];
if(IS_OS_8_OR_LATER) {
[_locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
}
_locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
There is no need to adding both they both do different things
[_locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
requestWhenInUseAuthorization
use [_locationManager requestWhenInUseAuthorization]; when you want to get location updates when app is Active of foreground mode and not when in background .
requestAlwaysAuthorization
use [_locationManager requestAlwaysAuthorization]; when you want to get location updates Even when your app is in background mode. So OS would ask for permissions accordingly
Adding Keys to info.Plist
It is now mandetory to add either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription Key to your info.Plist depending upon which permission you are asking for .
The value to this key should be a NSString explaining user why the app wants to use location services something like "We need your location for XYZ reason"
If iOS does not find above key in your Plist file , it will ignore location request.
Hope this helps. By the way I reccomand you to watch WWDC 2014 video "Whats new in Core Location", all iOS8 changes have been nicely explained.
Other Changes in Code
Also one more thing i see in your code , You are creating new instance of _locationManager in 3rd line , Means you are creating new object after assigning delegate to locationManager, should not do that too.