捕获当前位置和计算距离
问题描述:
我一直在寻找这一段时间。我发现了很多帖子,并查看了文档,但我无法正常工作。我已实施了核心位置,并且其工作没有问题,即捕获当前位置和计算距离
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方法?
答
您在位置管理器上设置了一个代理,该代理将在位置更改时调用。您可以通过指定一个距离过滤器来间接控制它被调用的频率,以便只有当位置移动了至少该量时才会得到回调。
我已经取得了一些进展,并按照上面所述实施了一些代码,但还有一些问题。我已更新了我的原始帖子(请参阅第2部分)。 – Stephen 2010-09-01 10:55:49