2012-05-03 17 views
17

¿Cómo puedo obtener la distancia en metros entre dos CLLocation s? CLLocation no proporciona ningún método para hacerlo, seeems.¿Cómo se mide la distancia en metros entre dos CLLocations?

+5

¿Estás bromeando? [Referencia de CLLocation] (http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html) Cmd-F, "metros": "La altitud medida en metros" Cmd -G: "El radio de incertidumbre, medido en metros" Nuevamente: "La velocidad instantánea en metros por segundo" Nuevamente: "La precisión del valor de altitud en metros". Una vez más: "distanceFromLocation: Devuelve la distancia (en metros) desde la ubicación del receptor a la ubicación especificada." ¡Santa vaca! Esto es incluso más fácil en Xcode. –

+1

Gracias, mi mal. –

Respuesta

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

http://developer.apple.com/library/ios/DOCUMENTATION/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/doc/uid/TP40007126-CH3-SW18 – JeremyP

5
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]; 

Esperanza, esto le ayudará a ...

+0

No puedo agregar el bloque de código ... para agregar un bloque de código .. – Nit

2
+ (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; 
} 
2
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]; 
Cuestiones relacionadas