2011-09-30 188 views
1

我想獲取當前位置,我看到了這個代碼,但是如何指定這個緯度和經度來標記點擊按鈕?獲取當前的經緯度

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

    NSLog(@"latitudeAAAAA %f",newLocation.coordinate.latitude); 
    NSLog(@"longitudeAAAA %f",newLocation.coordinate.longitude); 
    [corelocation stopUpdatingLocation]; 
} 

回答

1

您需要設置自己的類作爲您CLLocationManager的委託,然後調用startUpdatingLocationdocs),使其叫你提到的方法。回調將在您要求開始更新位置之後的某個時刻到來,並且一直到來,直到您要求它停止。如果應用程序應該自行啓動,則必須找出用例,但只有當用戶單擊某個按鈕時,或者應該開始更新時才保存該位置(或者您想要用它做什麼)當用戶點擊時(我不太清楚你的問題是什麼意思)。

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/doc/uid/TP40007125-CH3-SW2

2
label.text = [NSString stringWithFormat:@"%lf,%lf", newLocation.coordinate.latitude, newLocation.coordinate.longitude]; 
0

在接口生成器,設置touchUpInside事件調用- (IBAction)buttonClicked:(id)sender(或一組動作編程)然後設定按鈕委託自我。在viewDidLoad設置您的locationManager

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
    [locationManager setDelegate:self]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager startUpdatingLocation]; 
} 

按鈕動作:

- (IBAction)buttonClicked:(id)sender { 
    myLabel.text = [NSString stringWithFormat:@"%+.6f,%+.6f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; 
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Button clicked" message:myLabel.text delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; 
    [alert show]; 

} 

位置管理的委託方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSDate* eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (abs(howRecent) < 5.0) { 
     NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude); 
    } 
}