2011-05-16 23 views
12

我的目的是在加載視圖時獲取用戶的經度/緯度,並將值存儲在兩個變量中供以後計算。我不需要跟蹤用戶位置並更新它,我只需要獲得他/她的座標一次。我的代碼如下所示:獲取用戶的經度/緯度當viewDidLoad

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // locationManager update as location 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
    CLLocation *location = [locationManager location]; 
    // Configure the new event with information from the location 
    CLLocationCoordinate2D coordinate = [location coordinate]; 

    float longitude=coordinate.longitude; 
    float latitude=coordinate.latitude; 

    NSLog(@"dLongitude : %f",longitude); 
    NSLog(@"dLatitude : %f", latitude); 
} 

在控制檯中我不斷收到這樣的:

dLongitude : 0.000000 
dLatitude : 0.000000 

能否請你幫我嗎?

+2

您是在設備上還是在模擬器上測試? – Eiko 2011-05-16 11:31:08

+0

模擬器:) – Luca 2011-05-16 12:45:53

+0

你可以通過模擬器模擬你的位置,它並不重要。 – 2015-05-22 14:14:25

回答

7

要獲取用戶的位置,即使您需要讓locationManager開始更新位置(您已這樣做)並實施經理在調用位置時調用的委派方法 - 您無法立即從位置管理器獲取位置。

如果您不想跟蹤用戶位置 - 只需在第一次獲取位置(並在未來需要時存儲該位置)後停止更新該委託方法中的位置。

+0

嗨,在我將位置/緯度值分配給變量後的'locationManager'中,我怎麼停止更新位置,我做了這個'[locationManager stopUpdatingLocation]',這足夠嗎? – Luca 2011-05-16 13:58:13

+0

@Malek,是的,應該足以停止跟蹤位置 – Vladimir 2011-05-16 14:01:37

+0

好吧,所以我不必實施'stopUpdatingLocation'委託方法? – Luca 2011-05-16 14:23:00

7

只要您致電[locationManager startUpdatingLocation];您的viewDidLoad方法已完成。您需要實現

-(void) locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *) newLocation 
      fromLocation: (CLLocation *) oldLocation 

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 

將由您的LocationManager被調用時,它得到的位置,或者當它失敗。在這兩種方法中,您可以撥打[locationManager stopUpdatingLocation]

在didUpdateToLocation方法中,您應該將所有代碼從viewDidLoad中移出,從CLLocation *location = [locationManager location];開始,這就是您將得到正確值的位置。

+0

這正是我所做的,但是,我得到的經度和緯度值是這個值:'0.000000'這就是爲什麼我仍然有些困惑從這裏是因爲,是由於模擬器,如果是這樣,我不必擔心,如果我測試我的應用程序在設備:) – Luca 2011-05-16 17:10:02

6

這爲我工作

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // locationManager update as location 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
    [locationManager stopUpdatingLocation]; 
    CLLocation *location = [locationManager location]; 
    // Configure the new event with information from the location 

    float longitude=location.coordinate.longitude; 
    float latitude=location.coordinate.latitude; 

    NSLog(@"dLongitude : %f", longitude); 
    NSLog(@"dLatitude : %f", latitude); 
} 
+1

更好地使用委託回調,而不是使用[locationManager stopUpdatingLocation];剛開始後。停止可以在卸載方法中完成。 – Dilshan 2013-01-20 06:59:16

3

試試這個代碼

-(IBAction)Button:(id)sender 

{ 

locationManager = [[CLLocationManager alloc] init]; 

locationManager.delegate = self; 

locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

locationManager.distanceFilter = kCLDistanceFilterNone; 

[locationManager startUpdatingLocation]; 

CLLocation *location = [locationManager location]; 

//配置與信息從該位置的新事件

CLLocationCoordinate2D coordinate = [location coordinate]; 

NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 

NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude]; 

NSLog(@"dLatitude : %@", latitude); 

NSLog(@"dLongitude : %@",longitude); 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 40, 250, 50)]; 

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 200, 50)]; 

UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 200, 100)]; 

myLabel.textColor = [UIColor blackColor]; 

myLabel1.textColor = [UIColor blackColor]; 

label.backgroundColor = [UIColor clearColor]; 

myLabel.backgroundColor = [UIColor clearColor]; 

myLabel1.backgroundColor = [UIColor clearColor]; 

[myLabel setText:latitude]; 

[myLabel1 setText:longitude]; 

label.text = @"Current Latitude And Longitude"; 

[self.view addSubview:label]; 

[self.view addSubview:myLabel]; 

[self.view addSubview:myLabel1]; 

} 
1

如果你是得到Lat lan g兩者(0.00,00)

這可能是您的解決方案,

第1步: NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

Info.plist

步驟2中添加這兩個關鍵: 而不是[locationManager startUpdatingLocation];啓動更新

第3步: ,你需要實現你的ViewController接口 2 mehthods

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { 

} 

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



CLLocation * currentLocation = [locations lastObject]; 

    NSLog(@"Updated Lat %f",currentLocation.coordinate.longitude); 
    NSLog(@"Updated Lang %f",currentLocation.coordinate.latitude); 



} 
CLLocationManagerDelegate

**step 4:** 

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
geocoder = [[CLGeocoder alloc] init]; 
    manager.delegate = self; 
    manager.desiredAccuracy = kCLLocationAccuracyBest; 
    [manager requestWhenInUseAuthorization]; 
    [manager requestAlwaysAuthorization]; 
    [manager startUpdatingLocation]; 


}