首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ios:如何通过<CLLocationManagerDelegate>获取CLLocationCoordinate2D

ios:如何通过<CLLocationManagerDelegate>获取CLLocationCoordinate2D
EN

Stack Overflow用户
提问于 2013-04-15 09:14:25
回答 1查看 301关注 0票数 0

获取CLLocationCoordinate2D的正确方法是什么?

在.h中:

代码语言:javascript
复制
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

在.m中

代码语言:javascript
复制
//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];
}

是这样的吗:

代码语言:javascript
复制
_coordinate = lastLocation.coordinate;

或者这样:

代码语言:javascript
复制
_coordinate = manager.location.coordinate;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-15 14:11:47

首先阅读已经在注释中说明的文档,因为这是位置编程的必备知识。

第二,注意这不是你从委托中得到的第一个数据,因为它可能是一个缓存的数据(你需要检查时间戳)或者是一个无效的数据(水平精度小于零)。

您可以使用该代码:

代码语言:javascript
复制
- (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
}

希望这对你有帮助,安德里亚

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16006254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档