如何在应用启动时获取用户当前位置

时间:2013-12-19 06:17:32

标签: ios mapkit core-location

我想在应用程序启动时获取用户当前位置(lat,long)..我不想使用委托方法进行频繁的位置更新。我不使用地图只需要在启动时需要当前位置。我这样做??

我正在使用下面的代码,但它无效。

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);

输出: - 纬度0.000000经度0.000000

请帮助..

4 个答案:

答案 0 :(得分:1)

使用CLLocationManagerDelegate方法

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [_locationManager stopUpdatingLocation];
    NSLog(@"latitude %f longitude %f",newLocation.coordinate.latitude,,newLocation.coordinate.longitude);

}

在iOS6中不推荐使用上述方法,而是使用locationManager:didUpdateLocations:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations

答案 1 :(得分:1)

- (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);
        }
    } ];
}

答案 2 :(得分:1)

最好使用委托方法获取用户位置,并在didupdateLocation方法中停止它  我认为这个网址可能会对你有所帮助        iPhone SDK: Track users location using GPS  并在

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D here =  newLocation.coordinate;
NSLog(@"%f  %f ", here.latitude, here.longitude);
[locationManager stopUpdatingLocation];
}

你做完了......

答案 3 :(得分:0)

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];
}