2012-05-03 4 views
17

Comment puis-je obtenir la distance en mètres entre deux CLLocation s? ne fournit aucune méthode pour le faire, il voit.Comment mesurer la distance en mètres entre deux CLLocations?

+5

Vous plaisantez? [CLLocation Reference] (http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html) Cmd-F, "meters": "L'altitude mesurée en mètres" Cmd -G: "Le rayon d'incertitude, mesuré en mètres" Encore: "La vitesse instantanée en mètres par seconde" Encore: "La précision de la valeur de l'altitude en mètres." Une fois de plus: "distanceDomLocation: Renvoie la distance (en mètres) entre l'emplacement du destinataire et l'emplacement spécifié." Vache sacrée! C'est encore plus facile dans Xcode lui-même. –

+1

Merci, mon mauvais. –

Répondre

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

Hope, cela vous aidera ...

+0

Je ne peux pas ajouter de bloc de code. Veuillez ajouter un bloc de code. – 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];