【问题标题】:Capturing current location and calculating distance捕获当前位置并计算距离
【发布时间】:2010-08-31 18:14:15
【问题描述】:

我已经看了一段时间了。我找到了很多帖子并查看了文档,但我无法让它工作。我已经实现了核心位置,并且它的工作没有问题,即

NSLog(@"Description: %@\n", [newLocation description]);

描述细节显示在日志中。

我想在用户点击按钮时捕获当前位置,并且当他们开始移动时间歇性地重新捕获他们的位置。使用此信息,我想计算起点和当前点之间的距离。

我知道我可以使用以下内容:

CLLocationDistance distance = [myLocation distanceFromLocation:restaurantLocation]

要计算距离,但我不确定如何捕获当前位置。

如果有人可以发布一些示例代码,那就太好了。我即将完成这项工作,只需要最后一次完成。

问候, 斯蒂芬

第 2 部分:从最初的帖子开始,我取得了一些进步。详情如下:

#import "MyCLController.h"


@implementation MyCLController

@synthesize locationManager;
@synthesize delegate;

- (id) init {
    self = [super init];
    if (self != nil) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self; // send loc updates to myself
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [self.delegate locationUpdate:newLocation];

    CLLocation *item1 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
    CLLocation *item2 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];

    int meters = [item1 getDistanceFrom:item2]; 
    NSLog(@"Distance-d: %d", meters);

    [item1 release];
    [item2 release];


}


- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    [self.delegate locationError:error];
}

- (void)dealloc {
    [self.locationManager release];
    [super dealloc];
}

@end

我在这里计算距离是否正确,还是应该在 NewWorkoutViewController.m 中的 locationUpdate 方法中计算?

【问题讨论】:

    标签: iphone


    【解决方案1】:

    您在位置管理器上设置了一个代理,当位置更改时将调用该代理。您可以通过指定距离过滤器来控制(间接)调用它的频率,这样只有在位置移动了至少该数量时您才会收到回调。

    【讨论】:

    • 我已经取得了一些进展,并实现了您上面所说的一些代码,但还有一些问题。我已经更新了我原来的帖子(见第 2 部分)。
    猜你喜欢
    • 2016-06-15
    • 2012-10-05
    • 2019-10-07
    • 2011-04-03
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多