繁体   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