Reputation: 43
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..
Upvotes: 1
Views: 2355
Reputation: 551
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];
}
Upvotes: 0
Reputation: 966
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...
Upvotes: 1
Reputation: 42977
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
Upvotes: 1
Reputation: 2741
- (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);
}
} ];
}
Upvotes: 1