How to get current altitude in ios7 - ios

Since I have updated my ios to 7.I dont know how get the current location and altitude with the new method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
Can Any one show me some sample code how to use this method to get current location and altitude?
Thanks a LOT.

The most recent location will be at the end of the locations array.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *mostRecentLocation = [locations lastObject];
bool haveValidAltitude = (mostRecentLocation.verticalAccuracy > 0);
if(haveValidAltitude)
{
NSLog(#"current altitude is: %g meters above sea level", mostRecentLocation.altitude);
}else{
NSLog(#"current altitude is unavailable");
}
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
NSLog(#"latitude %+.6f, longitude %+.6f\n", location.coordinate.latitude, location.coordinate.longitude);
}
locations is an array, last object of which is the newest available location.

Related

CLLocationManager Repeating Few Times

Having a bit of an issue. I have a GMSMapView in my application and it points to the users location upon loading. When the app loads I have the locationManager set to display the latitude in the log once it finds the users position. Right now it displays the latitude over 3 times (I plan to make this post to my server). When I launch my application I will have quite a few users and the extra 3 server requests per user would be a lot for a server to handle. I am wondering is there anything I have done wrong in the code or is there a way to get around this? Will attach the code below.
Thanks!
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:(id)self];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.locationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
longitude:newLocation.coordinate.longitude
zoom:17.0];
[self.mapView animateToCameraPosition:camera];
latitude = newLocation.coordinate.latitude;
longitude = newLocation.coordinate.longitude;
NSLog(#"latitude = %g", latitude);
}
didUpdateToLocation was deprecated way back in iOS6. Use the replacement didUpdateLocations, and then just pick off the last element of the array e.g.:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
location = [locations lastObject];
// ...
latitude = location.coordinate.latitude;
longitude = location.coordinate.longitude;

CoreLocation iOS9 storing Coordinates

I recently asked this question, not knowing of the changes made to CL in iOS 9. I am trying to store the user's current location and I have changed my code to reflect the new delegate methods in iOS 9, but didUpdateLocations is still never reached.
Here is the link to my original question: Latitude & Longitude Not Retrieved
And my updated code:
viewWillAppear
- (void)viewWillAppear:(BOOL)animated{
manager = [[CLLocationManager alloc] init];
manager.delegate = self;
if ([manager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[manager requestWhenInUseAuthorization];
}
[manager startUpdatingLocation];
}
didUpdateLocations
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(#"Location: %#", locations);
CLLocation *currentLocation = locations.lastObject;
if (currentLocation != nil) {
float latitude = currentLocation.coordinate.latitude;
float longitude = currentLocation.coordinate.longitude;
NSLog(#"dLongitude : %f", longitude);
NSLog(#"dLatitude : %f", latitude);
}
else{ NSLog(#"ERROR");
}
}
check for
authorizationStatus and startUpdatingLocation
and
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
you can also go to settings ->privacy -> location services (turn on) -> select ur app and select while using.

Get current latitude and longitude from didupdatelocation delegate method

I created a MAPVIEW to get latitude and longitude of user's current location.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
The didUpdateLocations delegate method gives an array which has current location's latitude and longitude.
How do i get latitude and longitude from that array?
The delegate method will return the array of CLLocation objects. This array contains most recent location at end of array.
So, you should receive it like this:
CLLocation *loc = [locations lastObject];
To access latitude, longitude write below code.
CLLocationDegrees latitude = loc.coordinate.latitude;
CLLocationDegrees longitude = loc.coordinate.longitude;
In the locations array, you will find CLLocation objects. From there, you can get the coordinates like this:
CLLocation *location = [locations objectAtIndex:someIndex]; //You can have more than one update so loop through it.
CLLocationDegrees latitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation * currentLocation = [locations lastObject];
NSString * latitude = [NSString stringWithFormat:#"%.6f", currentLocation.coordinate.latitude];
NSString * longitude = [NSString stringWithFormat:#"%.6f", currentLocation.coordinate.longitude];
}
You need to get the last object from the locations array and then grab the lattitude and longitude by the way.
CLLocation *location = [locations lastObject];
CLLocationDegrees lattitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;
You need to implement below method of delegate to get current location of user.
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
}

Iphone device getting the user location define in xcode

I am using CLLocationManger to get device current location. All is going well on simulation. I check the "Allow Location Simulation" and set Sydney, Australia as default location. I got the lat and long of Sydney on simulator that is fine
My problem is that when i try on device I still get the Sydney location however my device current location is India.
My device get the location what ever I set in Xcode.
Here is my code:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 1;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
#pragma mark delegate method which tell the current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
latitudeCurrentLocation = [NSString stringWithFormat:#"%f",newLocation.coordinate.latitude];
longitudeCurrentLocation= [NSString stringWithFormat:#"%f",newLocation.coordinate.longitude];
[locationManager stopUpdatingLocation];
locationManager=nil;
NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
Please suggest what should I should do.
Use this delegate method and check your location address
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
Here is sample code :
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:_locationManager.location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"reverseGeocodeLocation:completionHandler: Completion Handler called!");
if (error){
NSLog(#"Geocode failed with error: %#", error);
return;
}
NSLog(#"placemarks=%#",placemarks);
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(#"placemark.ISOcountryCode =%#",placemark.ISOcountryCode);
NSLog(#"placemark.country =%#",placemark.country);
NSLog(#"placemark.postalCode =%#",placemark.postalCode);
NSLog(#"placemark.administrativeArea =%#",placemark.administrativeArea);
NSLog(#"placemark.locality =%#",placemark.locality);
NSLog(#"placemark.subLocality =%#",placemark.subLocality);
NSLog(#"placemark.subThoroughfare =%#",placemark.subThoroughfare);
NSLog(#"stringlocation:%#",_cityString);
}];
_userLocation = [locations lastObject]; // This will give you co-ordinates
[_locationManager stopUpdatingLocation];
}
When you run this in your device select don't simulate location

How to get users current location on app launch

I want to get users current location(lat,long) on the app launch only..i dont want to use delegate method for frequent location updates..I m not using maps just need current location on launch..how should i do this??
I am using the code below but its not working.
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location;
location = [locationManager location];
CLLocationCoordinate2D coord;
coord.longitude = location.coordinate.longitude;
coord.latitude = location.coordinate.latitude;
NSLog(#"latitude %f longitude %f",coord.latitude,coord.longitude);
Output:-
latitude 0.000000 longitude 0.000000
please help..
better you use the delegate method to get user location and in didupdateLocation method stop it
I think this url may help you
iPhone SDK: Track users location using GPS
and in
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(#"%f %f ", here.latitude, here.longitude);
[locationManager stopUpdatingLocation];
}
and you done with it...
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
// Reverse Geocoding
// NSLog(#"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// NSLog(#"Found placemarks: %#, error: %#", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
[locationManager stopUpdatingLocation];
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
}
Use CLLocationManagerDelegate method
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[_locationManager stopUpdatingLocation];
NSLog(#"latitude %f longitude %f",newLocation.coordinate.latitude,,newLocation.coordinate.longitude);
}
Above method is deprecated in iOS6, use locationManager:didUpdateLocations: instead
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
import <CoreLocation/CoreLocation.h>
delegate: CLLocationManagerDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// System alert for Allowing current location of user.
// Location manager object creation.
locationManager = [[CLLocationManager alloc] init];`enter code here`
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
// iOS >= 6.0.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[locationManager stopUpdatingLocation];
}
// iOS < 6.0
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
}

Resources