简体   繁体   English

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

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

What is the propery way to obtain CLLocationCoordinate2D? 获取CLLocationCoordinate2D的正确方法是什么?

In .h: 在.h中:

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

In .m 在.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];
}

Is it this: 是这个吗?

_coordinate = lastLocation.coordinate;

or this: 或这个:

_coordinate = manager.location.coordinate;

First read the documentation as already stated in the comments, because this is a must know for location programming. 首先,请阅读注释中已经说明的文档,因为这对于位置编程是必不可少的。
Second pay attention that is not the first data you get from delegate, because it could be a cached data (you need to check the timestamp) or an invalid data (with horizontal accuracy less that zero). 第二要注意的不是您从委托那里获得的第一个数据,因为它可能是缓存的数据(您需要检查时间戳记)或无效的数据(水平精度小于零)。
You could use that code: 您可以使用该代码:

- (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
}

Hope this helps, Andrea 希望这会有所帮助,安德里亚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM