簡體   English   中英

ios:如何使用獲取CLLocationCoordinate2D <CLLocationManagerDelegate>

[英]ios: How to obtain CLLocationCoordinate2D with <CLLocationManagerDelegate>

獲取CLLocationCoordinate2D的正確方法是什么?

在.h中:

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

在.m中

//Called when the location can be obtained
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //Get the latitude and longitude location
    CLLocation *lastLocation = [locations lastObject];
    _latitude = [[NSString alloc] initWithFormat:@"%f", lastLocation.coordinate.latitude];
    _longitude = [[NSString alloc] initWithFormat:@"%f", lastLocation.coordinate.longitude];
}

是這個嗎?

_coordinate = lastLocation.coordinate;

或這個:

_coordinate = manager.location.coordinate;

首先,請閱讀注釋中已經說明的文檔,因為這對於位置編程是必不可少的。
第二要注意的不是您從委托那里獲得的第一個數據,因為它可能是緩存的數據(您需要檢查時間戳記)或無效的數據(水平精度小於零)。
您可以使用該代碼:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation * newLocation = [locations lastObject];
    if (newLocation.horizontalAccuracy < 0) {
        return;
    }
    NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
    if (abs(interval)>5) {
        return;
    }
   //YOUR CODE HERE
}

希望這會有所幫助,安德里亞

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM