I tried writing a coordinate using Core Location, and it's running nicely, only thing is, it's a vegetable when it comes to coordinates.
This is the code that gets the coordinates.
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (startingLocation == nil)
self.startingLocation = newLocation;
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.latitude];
latitudeLabel.text = latitudeString;
NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.longitude];
longitudeLabel.text = longitudeString;
NSString *horizontalAccuracyString = [[NSString alloc] initWithFormat:@"%g",newLocation.horizontalAccuracy];
horizontalAccuraryLabel.text = horizontalAccuracyString;
NSString *altitudeString = [[NSString alloc] initWithFormat:@"%g",newLocation.altitude];
altitudeLabel.text = altitudeString;
NSString *verticalAccuracyString = [[NSString alloc] initWithFormat:@"%g",newLocation.verticalAccuracy];
verticalAccuracyLabel.text = verticalAccuracyString;
CLLocationDistance distance = [newLocation getDistanceFrom:startingLocation];
NSString *distanceString = [[NSString alloc] initWithFormat:@"%g",distance];
distanceTraveledLabel.text = distanceString;
}
It's giving me Lat: 37.7858 Long: -122.406 which is somewhere in USA, while I'm running the code from Europe, Romania...
Could it be because it's running from a simulator?
Also I know the locationManager:didUpdateToLocation:fromLocation is deprecated, but I tried doing it with:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
but I don't know how to access the elements of [location lastObject].
That lat & long combination is San Francisco, which is the home base for many commuting (via a posh luxury commuter bus) Apple employees.
You can set the location that your simulator is working with pretty easily:
Latitude & Longitude for Bucharest is "44.4325" & "26.103889", as far as I know.
As for accessing the elements of "[location lastObject]
", the object(s) in that array will be a CLLocation object which has a "coordinate
" property. If you get a "didUpdateLocations:
" delegate method being called, that array will always have at least one location (which is what you access via "lastObject
").