-1

I tried to calculate the distance between two locations but something is wrong because the distance is 0 every time.

        float oldLatitude = [gInfo.latitude floatValue];
        float oldLongitude = [gInfo.longitude floatValue];
        CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:oldLatitude longitude:oldLongitude];
        CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:newLatitude longitude:newLongitude];
        if (newLatitude == 0 && newLongitude == 0) {
            [self showAlarm2];
            return;
        }
        float distance = [newLocation distanceFromLocation:oldLocation];
        // distance 0 ????????
        int result = (int)roundf(distance );
        if (result < 0) {
            result = -result;
        }
       // long result = (long)distance;

        NSLog(gInfo.name );
        NSLog(@"Distance i meters: %i", result);
        //**************** filter group by check distance *************

       if (result <= 10) {
            [viewArray addObject:gInfo];
        }
João Cunha
  • 3,776
  • 3
  • 22
  • 33
  • use this link it will fine for u http://stackoverflow.com/questions/23243554/calculate-distance-of-users-current-location-and-a-specific-city/23243628#23243628 – Anbu.Karthik May 30 '14 at 10:51
  • Distance is never < 0. – iphonic May 30 '14 at 10:57
  • 2
    Have you tried using the debugger to step through the code and check that 1) your logic works and 2) the values of both the latitudes and longitudes? Just telling us it doesn't work is not helpful - more information is needed – Robotic Cat May 30 '14 at 10:57
  • Provide the values of `newLocation` and `oldLocation`. – zaph May 30 '14 at 10:59

1 Answers1

0

Use this

 CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];

    CLLocation *location = [locationManager location];

    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];
    latitude = coordinate.latitude;
    longitude = coordinate.longitude;

    MKCoordinateRegion myRegion;
    MKCoordinateSpan mySpan;
    mySpan.latitudeDelta = 1.0f;
    mySpan.longitudeDelta = 1.0f;
    myRegion.center = coordinate;
    myRegion.span = mySpan;




  //-----------------------------------------------
        NSDictionary *dict = (NSDictionary*)[arrayJsonData objectAtIndex:indexPath.row];
        CLLocation *locA = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
        CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[dict1 valueForKey:@"Latitude"] doubleValue] longitude:[[dict1 valueForKey:@"Longitude"] doubleValue]];

        CLLocationDistance distance = [locA distanceFromLocation:locB];

        double distanceInMiles = (distance/CONVERSION_TO_MILES); 
// CONVERSION_TO_MILES 1609.344
Vibha Singh
  • 623
  • 4
  • 9