distanceFromLocation - Calculate distance between two points

后端 未结 7 978
礼貌的吻别
礼貌的吻别 2020-11-29 21:27

Just a quick question on Core Location, I\'m trying to calculate the distance between two points, code is below:

    -(void)locationChange:(CLLocation *)newL         


        
相关标签:
7条回答
  • 2020-11-29 21:54

    Taken from the excellent libary CoreLocation utitlities :

    - (CLLocationDistance) distanceFromCoordinate:(CLLocationCoordinate2D) fromCoord;
    {
        double earthRadius = 6371.01; // Earth's radius in Kilometers
    
        // Get the difference between our two points then convert the difference into radians
        double nDLat = (fromCoord.latitude - self.coordinate.latitude) * kDegreesToRadians;  
        double nDLon = (fromCoord.longitude - self.coordinate.longitude) * kDegreesToRadians; 
    
        double fromLat =  self.coordinate.latitude * kDegreesToRadians;
        double toLat =  fromCoord.latitude * kDegreesToRadians;
    
        double nA = pow ( sin(nDLat/2), 2 ) + cos(fromLat) * cos(toLat) * pow ( sin(nDLon/2), 2 );
    
        double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
        double nD = earthRadius * nC;
    
        return nD * 1000; // Return our calculated distance in meters
    }
    
    0 讨论(0)
提交回复
热议问题