How to calculate the distance of two coordinates in the lens c?

how is the name how? I tried the code from Google land, but it looks like the result is different from the result of calculating the Google map. below is the code i made

-(double)GetDistance:(double)lat1 long1:(double)lng1 la2:(double)lat2 long2:(double)lng2 { //NSLog(@"latitude 1:%.7f,longitude1:%.7f,latitude2:%.7f,longtitude2:%.7f",lat1,lng1,lat2,lng2); double radLat1 = [self rad:lat1]; double radLat2 = [self rad:lat2]; double a = radLat1 - radLat2; double b = [self rad:lng1] -[self rad:lng2]; double s = 2 * asin(sqrt(pow(sin(a/2),2) + cos(radLat1)*cos(radLat2)*pow(sin(b/2),2))); s = s * EARTH_RADIUS; s = round(s * 10000) / 10000; return s; } -(double)rad:(double)d { return d *3.14159265 / 180.0; } 

EARTH_RADIUS value is 6378.138

using this function, providing two coordinates, the result will be 4.5kM but when I use google map to get the direction between two identical coordinates, it will show me a distance of about 8 km

can anyone help point out the problem of my code?

+4
source share
5 answers

Since it is labeled iPhone, why not use the built-in distance function, rather than collapse by yourself? location1 and location2 are CLLocation objects.

 CLLocationDistance distance = [location1 getDistanceFrom:location2]; 
+50
source

Here is a simple code (suppose you just have the latitude and longitude of two points)

 CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:startLatitude longitude:startLongitude]; CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:endLatitude longitude:endLongitude]; CLLocationDistance distance = [startLocation distanceFromLocation:endLocation]; // aka double 

Remember to add the MapKit Framework to your project and import MapKit into your file:

 #import <MapKit/MapKit.h> 
+13
source

Google maps are more likely to give you mileage, while the big circle equation you specified will be the distance in a straight line. If there was a straight road straight from point A to point B, Google Maps would probably give you the same distance as the equation you have there.

+5
source

You should be able to use the google API to calculate either a long distance or a distance, depending on the needs of your application.

See GLatLong::distanceFrom and GDirections::getDistance .

+1
source

Because

 getDistanceFrom: 

isDeprecated Try using

[newLocation distanceFromLocation:oldLocation

+1
source

All Articles