Calculating the distance between two coordinates using CLLocation

I use CLLocationDistanceto get the distance between two points, but I get an error message when transmitting my current location.

CLLocation *current = [[CLLocation alloc] initWithLatitude:startLocation.coordinate.latitude longitude:startLocation.coordinate.longitude];
CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lat"] doubleValue] longitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lon"] doubleValue]];

//Here the current location gives me an error "Initializing cllocation with an expression incompatible format"
CLLocationDistance *itemDist = [itemLoc distanceFromLocation:current];
NSLog(@"Distance: %@", itemDist);
+4
source share
4 answers

The error you get is actually:

Initialization of 'CLLocationDistance *' (aka 'double *') with an expression of incompatible type 'CLLocationDistance' (aka 'double')

As you say, you initialize itemDist(which you declared as CLLocationDistance *) to what returns CLLocationDistance(do not notice an asterisk).

CLLocationDistance .
( double - . ).


itemDist CLLocationDistance CLLocationDistance ( ):

CLLocationDistance itemDist = [itemLoc distanceFromLocation:current];

NSLog, , :

NSLog(@"Distance: %f", itemDist);
+4

2.0 :

let userLocation:CLLocation = CLLocation(latitude: 11.11, longitude: 22.22)
let priceLocation:CLLocation = CLLocation(latitude: 33.33, longitude: 44.44)
let meters:CLLocationDistance = userLocation.distanceFromLocation(priceLocation)
+5

in view load

//************************** GEtting USer latitude/ Longitude ********************




  self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        [self.locationManager startUpdatingLocation];
//************************** GEtting USer latitude/ Longitude ********************

- CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {        
        CLLocation *locA = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.longitude longitude:currentLocation.coordinate.latitude];
        CLLocation *locB = [[CLLocation alloc] initWithLatitude:11.111111111111111 longitude:11.1111111111111111]; 
        User_Vanue_Distance = [locA distanceFromLocation:locB];
        NSLog(@"THIS IS THE DISTANCE BT TWO POINTS +++++++++++++++++++++%f",User_Vanue_Distance);  
    }
}

You need to put the keys below in info.plist, in ios 8 to access the location we need to put the keys below in info.plist 1. NSLocationWhenInUseUsageDescription : Location required 2.NSLocationAlwaysUsageDescription Needed : Location Needed

This will work for you, but you need to add one thing to add a check on ios8. otherwise it will work on ios 7

// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.


  if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

Hope this works for you. :)

0
source

Find the distance in KM from two different lat and long

let userLocation:CLLocation = CLLocation(latitude: 23.0320, longitude: 72.5250)
let priceLocation:CLLocation = CLLocation(latitude: 23.0283, longitude: 72.5067)
let distance = String(format: "%.2f km", userLocation.distanceFromLocation(priceLocation)/1000)
print("Distance is KM is:: \(distance)")
0
source

Source: https://habr.com/ru/post/1534515/


All Articles