![](/img/trans.png)
[英]how to use my location(latitude,longitude) of CLLocation at viewdidload()
[英]iOS CLLocation - getting the location on ViewDidLoad
這可能是我想要的簡單事情,但我已經設置了位置服務(為了清晰起見縮短了):
- (void)viewDidLoad
{
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@",newLocation.coordinate.latitude);
NSLog(@"%@",newLocation.coordinate.longitude);
}
工作正常,並給我一個位置數據流到日志。
但我想要的是能夠立即在ViewDidLoad中獲取當前位置,因為我只需要它一次,而不是一次不斷的更新 - 它只是為了找到“最近”的便利設施,所以我可以向用戶報告。 我試過添加:
self.locationLat = [self.locationManager location].coordinate.latitude;
self.locationLng = [self.locationManager location].coordinate.longitude;
在startUpdatingLocation之后立即到ViewDidLoad,但它們總是以null形式出現。 還有什么我需要調用才能在運行后獲取數據嗎?
謝謝
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
/*report to user*/
[self.locationManager stopUpdatingLocation];
}
因此,您將獲得一次位置,然后停止更新它。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingLocation];
NSLog(@"%@",newLocation.coordinate.latitude);
NSLog(@"%@",newLocation.coordinate.longitude);
}
您只能獲得值
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
沒有其他功能可用於獲取位置值..因此,您將獲得最快的值是在首次調用此函數時...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.