关于C#:如何测量两个CLLocation之间的距离(以米为单位)?

How to measure the distance in meters between two CLLocations?

如何获得两个CLLocation之间的距离(以米为单位)? 看来CLLocation没有提供任何方法来执行此操作。


1
2
CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB];
// distance is a double representing the distance in meters


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];  //      distance is expressed in meters

CLLocationDistance kilometers = distance / 1000.0;
// or you can also use this..
CLLocationDistance meters = distance;

NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", kilometers];

flot totaldistancecovered = [distanceString floatValue];

//Now,you can use this float value for addition...
// distanceMoved  should be float type variable which is declare in .h file...

 distanceMoved = distanceMoved + totaldistancecovered ;
 theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];

希望这个能对您有所帮助...


1
2
3
4
5
CLLocation *loc1 = [[CLLocation alloc]  initWithLatitude:dblLatitude longitude:dblLongitude];  
CLLocation *loc2 = [[CLLocation alloc]  initWithLatitude:dblCurrentLatitude longitude:dblCurrentLongitude];
double dMeters = [loc1 distanceFromLocation:loc2];
[loc1 release];
[loc2 release];


1
2
3
4
5
6
7
8
9
+ (CLLocationDistance)distanceBetweenCoordinate:(CLLocationCoordinate2D)originCoordinate andCoordinate:(CLLocationCoordinate2D)destinationCoordinate {    
    CLLocation *originLocation = [[CLLocation alloc] initWithLatitude:originCoordinate.latitude longitude:originCoordinate.longitude];
    CLLocation *destinationLocation = [[CLLocation alloc] initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
    CLLocationDistance distance = [originLocation distanceFromLocation:destinationLocation];
    [originLocation release];
    [destinationLocation release];

    return distance;
}

斯威夫特3.0

1
2
3
4
let location1 = CLLocation(latitude: <CLLocationDegrees>, longitude: <CLLocationDegrees>)
let location2 = CLLocation(latitude: <CLLocationDegrees>, longitude: <CLLocationDegrees>)

let distance = location1.distance(from: location2)

distance以米为单位Double

https://developer.apple.com/reference/corelocation/cllocation/1423689-distance