0

アプリでは、2 点間の距離を計算する必要があります。私はこのようにしています。

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{
    NSLog(@"updating");
     //Getting my concerts
    arrConcerten = [matches mutableCopy];

       for (Event *concert in arrConcerten) {
            CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];

            CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
            NSLog(@"meters are %f",meters);
            if(meters > 5000){
                [locationManager2 stopUpdatingLocation];
                NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
                NSLog(@"%@",message2);
            }else{

                NSLog(@"too far away");
            }

        }
    }

これは私のログが言うことです。

2013-10-17 15:57:23.518 Domino[7468:907] meters are 7973.753294
2013-10-17 15:57:23.520 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 51.038139 and long = 5.557330
2013-10-17 15:57:23.896 Domino[7468:907] meters are 27850.874245
2013-10-17 15:57:23.899 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 50.860332 and long = 5.493722

ご覧のとおり、場所は互いに近くにあります。しかし、それでも私たちは遠すぎると言っています。誰かが私を助けることができますか?

4

3 に答える 3

1

これを使用する理由:

距離が 5000 メートルを超えている場合: 何とかしてください。ロジックがわかりません。遠すぎると5000メートル以上。だから私は思う:

  - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation{
        NSLog(@"updating");
         //Getting my concerts
        arrConcerten = [matches mutableCopy];

       for (Event *concert in arrConcerten) {
            CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];

            CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
            NSLog(@"meters are %f",meters);
            if(meters <= 5000){
                [locationManager2 stopUpdatingLocation];
                NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
                NSLog(@"%@",message2);
            }else{

                NSLog(@"too far away");
            }

        }
    }

またはもちろん:

if(meters > 5000){

                    NSLog(@"it's far enough");
 }else{

                    NSLog(@"too close");
 }

そして、本当に重要なことがあります:

この方法では、地球の曲率に沿って 2 つの地点を結ぶ線をたどることで、2 つの地点間の距離を測定します。結果として得られる円弧は滑らかな曲線であり、2 つの場所間の特定の高度の変化は考慮されていません。アップルのドキュメントから

distanceFromLocation は、ターンごとの測定ではなく、「空中」の距離を測定します。

于 2013-10-17T14:16:58.500 に答える
0

これが解決策です-

-(NSString *)getDistance:(NSDictionary *)dicVal{
    CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[[dicVal valueForKey:@"latitude"] doubleValue] longitude:[[dicVal valueForKey:@"longitude"] doubleValue]];

    CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:appDelegate.userLocation.latitude longitude:appDelegate.userLocation.longitude];

    CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
    NSString *dist=[NSString stringWithFormat:@"%4.0f %@", distance/1000,NSLocalizedString(@"KM", nil)];
    return dist;
}
于 2013-10-17T14:21:01.667 に答える