2013-12-19 114 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]; 
} 
相關問題