Xcode 如何获得我当前职位和新职位之间的距离?

Xcode 如何获得我当前职位和新职位之间的距离?,xcode,locationmanager,cllocationdistance,Xcode,Locationmanager,Cllocationdistance,我想在几分钟后得到我当前位置和新(当前)位置之间的距离。我想要一个计时器,每秒钟用新的距离更新我的标签 我站在一条有我目前位置的街道上,再往前跑300米。我得到了一个新的当前位置和标签应该给300米 我对面向对象的c语言编程不是很有经验。 到目前为止,我得到的是: #import <CoreLocation/CoreLocation.h> @interface NormalSoloViewController () <CLLocationManagerDelegate>

我想在几分钟后得到我当前位置和新(当前)位置之间的距离。我想要一个计时器,每秒钟用新的距离更新我的标签

我站在一条有我目前位置的街道上,再往前跑300米。我得到了一个新的当前位置和标签应该给300米

我对面向对象的c语言编程不是很有经验。 到目前为止,我得到的是:

#import <CoreLocation/CoreLocation.h>

@interface NormalSoloViewController () <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lbDistance;

@end

@implementation NormalSoloViewController
{
    CLLocationManager *locationManager;
    CLLocation *firstLocation;
}

- (void)viewDidLoad
{

    [super viewDidLoad];

    locationManager =[[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLHeadingFilterNone;
    [locationManager startUpdatingLocation];

    firstLocation = locationManager.location;
}

- (void)startTimer
{
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateDistance:) userInfo:nil repeats:YES];
}

- (void)updateDistance:(NSTimer *)timer
{
    double distance = 0;
    distance = distance + [self calculateDistance];
    self.lbDistance.text = [NSString stringWithFormat:@"%f", distance];

}

- (double) calculateDistance {

    CLLocation *newFirstLocation = firstLocation;
    CLLocation *secondLocation = locationManager.location;
    firstLocation = secondLocation;

    return [newFirstLocation distanceFromLocation:secondLocation];
}
#导入
@接口规范SoloviewController()
@性质(弱,非原子)IBUILabel*lbDistance;
@结束
@SoloviewController的实现
{
CLLocationManager*locationManager;
CLLocation*第一个位置;
}
-(无效)viewDidLoad
{
[超级视图下载];
locationManager=[[CLLocationManager alloc]init];
locationManager.distanceFilter=KClHeadingFilterOne;
[locationManager startUpdatingLocation];
firstLocation=locationManager.location;
}
-(无效)startTimer
{
[NSTimer scheduledTimerWithTimeInterval:1目标:自选择器:@selector(UpdateInstance:)用户信息:无重复:是];
}
-(void)updateInstance:(NSTimer*)计时器
{
双倍距离=0;
距离=距离+[自计算距离];
self.lbDistance.text=[NSString stringWithFormat:@“%f”,距离];
}
-(双)计算距离{
CLLocation*newFirstLocation=firstLocation;
CLLocation*secondLocation=locationManager.location;
第一位置=第二位置;
返回[newFirstLocation distanceFromLocation:secondLocation];
}

代码不起作用,因为
locationManager.location
不能用那种方式调用。 我已将速度添加到我的应用程序中。它可以工作,但只有经理才能发送新信息

    @implementation ViewController
    {
        CLLocationManager *locationManager;
        double distance;
        double speed;
        double extraDistance;
        NSMutableArray *locations;
        CLLocation *newLocation2;
        CLLocation *oldLocation2;
        CLLocation *newLocationCheck;
    }

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        distance =0;
        speed =0;
        locationManager = [[CLLocationManager alloc] init];
        locations = [[NSMutableArray alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self startTimer];
        [locationManager startUpdatingLocation];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (void)startTimer
    {
        // Timer that repeatedly does something every 2 seconds
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(updateSpeedDistance:) userInfo:nil repeats:YES];
    }

   - (void)updateSpeedDistance:(NSTimer *)timer
{
    // fetches the old and new location out of the array list
    oldLocation2 = locations[0];
    newLocation2 = locations[1];

    // checks if theres a new location
    if (newLocation2 != newLocationCheck) {

        // if there is only one following location
        if (oldLocation2 != newLocation2){
            distance = distance + [newLocationCheck distanceFromLocation:newLocation2];
            speed = [oldLocation2 distanceFromLocation: newLocation2] / 2;
            newLocationCheck = newLocation2;
        // if there multiple locations avaible
        }
        else {
            distance = distance + [oldLocation2 distanceFromLocation:newLocation2];
            speed = [oldLocation2 distanceFromLocation: newLocation2] / 2;
            newLocationCheck = newLocation2;
        }
    }
    // if there's no new location the speed wil drop
    else {
        speed = speed /1.2;
    }
    self.lbDistance.text = [NSString stringWithFormat:@"Distance: %1.2f m", distance];
    self.lbSpeed.text = [NSString stringWithFormat:@"Speed: %1.2f m/s", speed];
}

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"didUpdateToLocation: %@", newLocation);

        if (newLocation != nil && oldLocation != nil) {
            [locations removeAllObjects];
            [locations addObject:oldLocation];
            [locations addObject:newLocation];
        }
        else {
            [locationManager startUpdatingLocation];
        }
    }

    @end
你只要打电话就行了

  // Call for the First Location Latitude & longitude
    Place* home = [[Place alloc] init];
    home.name = @"Home";
    home.latitude = [[appDel.dictionaRY1styLoc valueForKey:@"Dict1lat"] doubleValue];
    home.longitude = [[appDel.dictionaRY1styLoc valueForKey:@"Dict1Long"] doubleValue];
    CLLocationCoordinate2D coordinate21 = (CLLocationCoordinate2D){ home.latitude, home.longitude};
    MKCircle *circle1 = [MKCircle circleWithCenterCoordinate:coordinate21 radius:5000];
    [mapViewRoute addOverlay:circle1];
    CLLocation * locA = [[CLLocation alloc] initWithLatitude:home.latitude longitude:home.longitude];



    // Call for the Second Location Latitude & longitude
    Place* work = [[Place alloc] init];
    work.name = @"work";
    work.latitude = [[appDel.dictionary2ndyLoc valueForKey:@"Dict2lat"] doubleValue];
    work.longitude = [[appDel.dictionary2ndyLoc valueForKey:@"Dict2Long"] doubleValue];
    CLLocationCoordinate2D coordinate22 = (CLLocationCoordinate2D){ work.latitude, work.longitude};
    MKCircle *circle2 = [MKCircle circleWithCenterCoordinate:coordinate22 radius:5000];
    [mapViewRoute addOverlay:circle2];
    CLLocation * locB = [[CLLocation alloc] initWithLatitude:work.latitude longitude:work.longitude];
    NSLog(@"%f==%f && %f== %f",home.latitude,home.longitude,work.latitude,work.longitude);

    CLLocationDistance distanceE = [locB distanceFromLocation:locA];
    NSLog(@"distanceE: %2f", distanceE/1000);
    lblDistanceKm.text = [NSString stringWithFormat:@"%.2f KM",(distanceE/1000)];

这里
CLLocationDistance
是通过我们可以找到的距离的类名

您的代码有什么问题吗?看起来不错。但它将始终显示最后两个位置之间的距离,时差为1秒。你没有保留你的第一个位置。我不确定你想要这个。