2011-09-30 106 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); 
    } 
}