I'm trying to develop an Altimeter on Xcode and Simulator but it always return 0 for the height above the sea.
I don't understand why, I've tried with a lot of places on Simulator.
My code is this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_mapView.delegate = self;
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.pausesLocationUpdatesAutomatically = YES;
_locationManager.delegate = self;
firstLocation = YES;
checkUserLocation = NO;
}
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
lastLocation = location.coordinate;
_latitudeLabel.text = [NSString stringWithFormat:#"Current latitude: %f", location.coordinate.latitude];
_longitudeLabel.text = [NSString stringWithFormat:#"Current longitude: %f", location.coordinate.longitude];
_altitudeLabel.text = [NSString stringWithFormat:#"Current altitude: %f m", location.altitude];
}
Where is my error?
There's nothing wrong with your code, its because you are using the simulator.
Determining altitude requires a device with GPS capabilities, and you also need to be using GPS on that device in order to get it (wifi only location would not report altitude correctly even on GPS-enabled devices). The iOS simulator does not have those capabilities so altitude will not be accurate there. You will need to use a real device with GPS to get altitude measurements.
If you want to simulate a CLLocation with altitude you can create your CLLocation object and pass it an altitude yourself:
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
altitude:(CLLocationDistance)altitude
horizontalAccuracy:(CLLocationAccuracy)hAccuracy
verticalAccuracy:(CLLocationAccuracy)vAccuracy
timestamp:(NSDate *)timestamp
Altitude is a readonly property so you will need to create a new CLLocation object yourself instead of changing it manually when you receive a CLLocation object in your delegate callback.
Related
I m using location delegate methods to get the current location of the user.Everything is working fine when i debug it using xcode 7.3 but when I debug it using latest Xcode 8 and turn off location service from settings,in that case the pop up of location service disable and pop up to ask permission to get the location continuously appears on the screen and it freezes the app screen until the phone is restarted.
This is the code in AppDelegate class
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(#"locationManager didUpdateLocations: %#",locations);
for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
self.myLocation = theLocation;
self.myLocationAccuracy = theAccuracy;
}
if (self.shareModel.afterResume==TRUE) {
appStatus=#"Killed";
self.locationTracker = [[LocationTracker alloc]init];
self.locationTracker.str_latitude=[NSString stringWithFormat:#"%f",self.myLocation.latitude];
self.locationTracker.str_longitude=[NSString stringWithFormat:#"%f",self.myLocation.longitude];
self.locationTracker.pushStatus=FALSE;
[self.locationTracker GetDevice_LocationSettings];
}
}
These two pop up comes one after another and doesn't allow to do any other action.
I was asked to code a small app for iOS in Objective C that's supposed to choose the closest object of a user, in a set of coordinates
The plan was to load the coordinates of the user only once, when the application loads for the first time.
Here's my current code :
(void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.headingFilter = 1;
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
self.userLat = self.locationManager.location.coordinate.latitude;
self.userLon = self.locationManager.location.coordinate.longitude;
NSLog(#" ----------------------------- ");
NSLog(#"User lat : %lf", self.userLat);
NSLog(#"User lon : %lf", self.userLon);
NSLog(#" ----------------------------- ");
}
I don't have a Apple iOS developper program, so I'm stuck with the iOS simulator from xCode.
I try to set some custom coordinates in the iOS simulator through debug, like this :
When i execute the app (obviously after setting coordinates), the log has userLat and userLon to 0.00..
Apr 6 03:16:20 Locals-Mac.local app[5683]: -----------------------------
Apr 6 03:16:20 Locals-Mac.local app[5683]: User lat : 0.000000
Apr 6 03:16:20 Locals-Mac.local app[5683]: User lon : 0.000000
Apr 6 03:16:20 Locals-Mac.local app[5683]: -----------------------------
I remember hearing somewhere that the iOS simulator don't work with the CoreLocation library (It might be right or wrong, i wouldn't know).
Is it the reason the lat/lon values are 0?
If the reason is indeed the iOS simulator, does this code give user current location?
Thanks in advance!
Edit :
With the answer and comments, i'm now using the didUpdateLocations callback function :
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
self.userLat = location.coordinate.latitude;
self.userLon = location.coordinate.longitude;
NSLog(#" ----------------------------- ");
NSLog(#"User lat : %lf", self.userLat);
NSLog(#"User lon : %lf", self.userLon);
NSLog(#" ----------------------------- ");
}
I ask xCode to simulate my position to London with the Scheme option that follows :
My new load function :
-(void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.headingFilter = 1;
self.locationManager.delegate = self;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestWhenInUseAuthorization];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
}
This code still doesn't print the NSLog that was put in the location callback function. Did i miss something?
The location manager doesn't instantly have the user's location when you start updating it. If you check the location property right after calling startUpdatingLocation it is usually going to be nil unless some other app was recently updating it. You need to implement the locationManager:didUpdateLocations: delegate method which will be called once it has a location.
You also need to check and possibly request for location permissions.
So it works when I'm on WiFi. But on 4G, it only works if I had been on Wifi and it already has the location. A lot of times without WiFi, the phone will say it's using my location but its not updating the label nor is it uploading the coordinates to the server. Here's the code:
- (void)viewDidLoad {
[super viewDidLoad];
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self->locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self->locationManager requestWhenInUseAuthorization];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *newLocation = [locations lastObject];
NSString *locationLat = [NSString stringWithFormat:#"%.8f", newLocation.coordinate.latitude];
NSString *locationLong = [NSString stringWithFormat:#"%.8f", newLocation.coordinate.longitude];
latLongLabel.text = [NSString stringWithFormat:#"Lat: %# - Long%#",locationLat,locationLong];
[self postLocation:locationLat secondArg:locationLong];
}
- (void)postLocation: (NSString *)latitudeString secondArg:(NSString *)longitudeString {
//POST COORDINATES TO MY SERVER
}
- (IBAction)startUpdating:(id)sender {
[locationManager startUpdatingLocation];
}
My suspicion is that when you are on 4G, the location updates still work fine (although wifi triangulation makes them a bit more precise but only Apple knows how, as the implementation is private), BUT there might be an issue sending those values to the server quickly or reliably enough via 4G connection. (for example in London it is slow as hell with so many people around)
You might narrow the debugging by simply logging the location update directly to some UIlabel on your view, and not going through server infrastructure.
AS a last resort I would make sure
that you set your CLActivityType property to CLActivityTypeFitness
and pausesLocationUpdatesAutomatically is set to NO.
I wrote a simple iOS application that retrieves location information and then uses the location to request Yahoo Weather.
The problem is that even when I call the Core Location in the viewDidLoad, it won't give me the result immediately.
So why can't I get the location information?
How can I get the location information in viewDidLoad?
The pseudocode currently is something like:
- (void)viewDidLoad
{
[super viewDidLoad];
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locManager.distanceFilter = 100;
[self.locManager startUpdatingLocation];
//won't get the current location right now, so the output will be null
NSLog(#"Current Location Longitude: %#", self.longitudeString);
NSLog(#"Current Location Latitude: %#", self.latitudeString);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
self.longitudeString = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
self.latitudeString = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
Location updates are not provided as instantly as you are expecting, you need to wait few seconds (2-3 or may be more) to get precise location update. If you want to have location data in viewDidLoad then you should init your location manager (and call startUpdatingLocation) before invoking the ViewController (since then it is not guaranteed that you will have location-data in viewDidLoad).
I want the current latitude and longitude of my device. I can print latitude and longitude when my program locate the device for the first time but I can't refresh it.
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
int lastindex = [locations count]-1;
CLLocation * currentLocation = [locations objectAtIndex:lastindex];
float latitude =currentLocation.coordinate.latitude;
float longitude =currentLocation.coordinate.longitude;
NSLog(#"LAT:%f LONG:%f",latitude,longitude);
[self latitudeLabel].text = [[NSString alloc]initWithFormat:#"latitude : %#",[[NSNumber numberWithFloat:latitude] stringValue]];
[self longitudeLabel].text = [[NSString alloc]initWithFormat:#"longitude : %#",[[NSNumber numberWithFloat:longitude] stringValue]];
}
This is what I tried but latitude/longitude are never updated. I tried it with the function didUpdateHeading but that didn't work either.
How can i do this. Any one can help me to solve this issue.
Thanks you
This method is triggered by startUpdatingLocation which I assume you're using since it did get called at least once. The method gets called each time the location data changes. Is your location actually changing enough to trigger it? Also, check the distanceFilter property on your CLLocationManager and make sure it is low enough.
Make sure you add the following to your header:
#interface YourLocationObject : SomeObject <CLLocationManagerDelegate>
.. and when you create your CLLocationManager object, you set yourself as the delegate:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//... other customization here ...//
[locationManager startUpdatingLocations];