I am trying to make use of the options within iOS simulator : debug->freeway drive/ city run in order to simulate the location updates.
In my code I am using CLLocationManager for getting location updates with following code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDistanceFilter:20];
}
-(void)viewWillAppear:(BOOL)animated {
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)lm didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
NSLog(#"Location returned: %f, %f Accuracy: %f", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
}
I am never getting a callback on the delegate for location updates, while my app is in background and i am selecting the option in simulator.
I have provided my app the background mode for location updates. Please let me know how exactly to use these features or if i am missing anything here.
I finally sorted out the problem. The simulator's options are working perfectly fine but it was the implementation of CLLocation which was the problem.
On iOS 8 the location update code will not work unless :
You add NSLocationWhenInUseUsageDescription & NSLocationAlwaysUsageDescription to the plist with some string values that will be prompted to user.
You need to add ask user's permission for getting the location codes to work:
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
Taken from this post.
Related
I have followed many tutorials but none have worked. The coordinate always outputs 0.000000, 0.000000.
Here is my code for the location:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
currentLocation = (CLLocation *)[locations lastObject];
}
-(void)getLocation {
longitude = floorf(currentLocation.coordinate.longitude * 10000)/10000;
}
I have this in a button:
locationManager stopUpdatingLocation];
[self getLocation];
And I have this in the viewDidLoad:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
I am using a fake location in the ios simulator in xcode so it should not output 0.000000.
You need to request authorization to use location services.
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[self.locationManager requestAlwaysAuthorization];
}
You may need to add this line in viewDidLoad:
[locationManager requestAlwaysAuthorization];
If that doesn't work, I'm inclined to say it's because you're running in the simulator. Even the fake location was screwing me up at one point. Can you run on a device?
Edit: try adding this to info.plist:
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>
follow this tutorial you found the lat & long and by using both get the address also...:)
https://www.appcoda.com/how-to-get-current-location-iphone-user/
I am using the below code for displaying the user current location in iOS 8.4.
- (void)viewDidLoad
{
[super viewDidLoad];
[locationManager requestAlwaysAuthorization];
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
locationManager = [[CLLocationManager alloc] init];
locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
But it is not working properly. It displays the following error:
Trying to start MapKit location updates without prompting for location
authorization. Must call -[CLLocationManager
requestWhenInUseAuthorization] or -[CLLocationManager
requestAlwaysAuthorization] first.
For get user current location you need add one key as string in your project info.plist file.
Key is :
NSLocationAlwaysUsageDescription
I am using Xcode 6.I have some problems when I try to get User current Location using CLLocationManager. Even I added the NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to Info.plist file. And also used [CLLocationManager requestWhenInUseAuthorization]. Then also I am getting console output as
2014-10-15 11:45:15.004 MapIOS8[1916:57908] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
Have you tried writting the code in this order?
CLLocationManager *YourLocationManager = [[CLLocationManager alloc] init];
YourLocationManager.delegate = self;
[YourLocationManager requestWhenInUseAuthorization];
[YourLocationManager startUpdatingLocation];
yourMapView.delegate = self;
yourMapView.showsUserLocation = YES;
Also, to get the user location coordinate, you may have to implement this mapkit delegate method
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"%f, %f", userLocation.coordinate.latitude, userLocation.coordinate.longitude);
[mapView selectAnnotation:userLocation animated:YES];
}
If you are using iOS Simulator try to reset location settings from Settings-> General-> Privacy-> Reset location & privacy. Then call:
[LocationManager requestAlwaysAuthorization];
At the first execution must appear a popup to allow the use of the current user's position. After allow the use of current location start your LocationManager with:
[LocationManager startUpdatingLocation];
I am using simulator and changing the location to simulate the movement of device.
The problem that I have figured out is that:
In the following code, when I try to print the latitude and longitude values, the value are rounded off to 6 decimal digits(43.825885,-75.839785) while the original values that I enter in the simulator(Debug->Location) is 43.82588498,-75.83978498. (For these initial values, the didUpdateLocations delegate gets called)
Now the next location(Just next to the previous location) that I enter is 43.82499145,-75.84050195 which I must get 43.824991,-75.840502. But this time the didUpdateLocations delegate doesn't get called.
But when I now give 43.82110323,-75.85291386 (rounded to 43.821103,-75.852914), this time the didUpdateLocation delegate is called.
(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocationCoordinate2D coordinate = [[locations lastObject] coordinate];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"here Latitude : %#", latitude);
NSLog(#"here Longitude : %#",longitude);
}
The following are my locationManager properties:
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[_locationManager setDistanceFilter:300];
[_locationManager startUpdatingLocation];
[_locationManager startUpdatingHeading];
This is my mapView object's setup:
self.mainMapView.delegate=self;
[self.mainMapView setShowsUserLocation:YES];
Is this behavior because the OS is rounding off the Lat/Lon values? Or is it because I am using the simulator(which I don't think should be the problem because when I read similar questions, people have mentioned that if you simulate location then there shouldn't be any problem)?
I don't have an iOS device. Is it still possible that didUpdateLocation may work correctly on an actual device?
I am not able to figure out the bug here. I want the didUpdateLocations delegate to be called every time a slightest location update is made. Kindly guide me.
Try removing your setDistanceFilter and letting locationManger use the default (kCLDistanceFilterNone). The distance filter tells your location manager to call didUpdateLocations only when the device has moved that amount or beyond. So, in your case, it will update the location every 300 meters.
The first thing you need to do is to add one or both of the following keys to your Info.plist file:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
Next you need to request authorization for the corresponding location method, WhenInUse or Background. Use one of these calls:
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
// Check for iOS 8
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
I'm trying to build an app that gives GPS coordinates on regular time intervals for display on a label and for sending to a web server, but it's proving difficult. I've managed to get it to give me accurate live GPS coordinates, but I have to press a button to get the label to refresh.
I figure the didUpdateLocation method is an ok place for this for now, but it seems to never run. I'm testing this by including an NSLog post - when I do this with Apple's LocateMe example app it runs like it should.
Below is my method:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
theLocation = [NSString stringWithFormat:#"latitude: %f
longitude: %f", locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude];
NSLog(#"location manager did something!!!");
}
Though I'm guessing that the issue lies with something outside of the above method. I suppose I should include my viewDidLoad method as that's where I startUpdatingLocation:
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[outletLabelData setText:(#"Loaded")];
}
I've also tried having the startUpdatingLocation in my button action method, and even having it work like a switch where you start and stop the updates (which should trigger the update event), but still no success.
Other details:
Xcode 4.4
iOS 5.1
Testing app on my iPhone while plugged into my mac
Can anyone help with this?
You need to tell your CCLocationManager who is the delegate. In your case it is your current controller.
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
and obviously make sure that your controller conforms to CLLocationManagerDelegate protocol.