2013-12-19 115 views
1

我想在应用程序启动时获取用户当前位置(拉特,长)..我不想使用委托方法来频繁位置更新..我不使用地图只是需要启动当前的位置..我应该怎么做?如何让用户在应用程序启动时的当前位置

我使用下面的代码,但它不工作。

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

请帮助..

+0

嘿你必须去槽代表方法来获得当前位置的用户 –

+1

你必须使用委托medhods一旦位置被提取您可以停止位置管理器的位置更新 –

+0

[locationManager stopUpdatingLocation]; –

回答

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

,你用它做...

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

是否可以在不使用此委托方法的情况下获取用户位置? –

+0

不,它不能根据这种方式委托方法将调用一次,所以它不会给应用程序额外的负载 –

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]; 
} 
相关问题