Calculate distance between 2 GPS coordinates

前端 未结 29 3570
青春惊慌失措
青春惊慌失措 2020-11-21 23:34

How do I calculate distance between two GPS coordinates (using latitude and longitude)?

29条回答
  •  走了就别回头了
    2020-11-21 23:49

    I think a version of the algorithm in R is still missing:

    gpsdistance<-function(lat1,lon1,lat2,lon2){
    
    # internal function to change deg to rad
    
    degreesToRadians<- function (degrees) {
    return (degrees * pi / 180)
    }
    
    R<-6371e3  #radius of Earth in meters
    
    phi1<-degreesToRadians(lat1) # latitude 1
    phi2<-degreesToRadians(lat2) # latitude 2
    lambda1<-degreesToRadians(lon1) # longitude 1
    lambda2<-degreesToRadians(lon2) # longitude 2
    
    delta_phi<-phi1-phi2 # latitude-distance
    delta_lambda<-lambda1-lambda2 # longitude-distance
    
    a<-sin(delta_phi/2)*sin(delta_phi/2)+
    cos(phi1)*cos(phi2)*sin(delta_lambda/2)*
    sin(delta_lambda/2)
    
    cc<-2*atan2(sqrt(a),sqrt(1-a))
    
    distance<- R * cc
    
    return(distance)  # in meters
    }
    

提交回复
热议问题