Calculate distance between 2 GPS coordinates

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

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

29条回答
  •  旧时难觅i
    2020-11-21 23:59

    Scala version

      def deg2rad(deg: Double) = deg * Math.PI / 180.0
    
      def rad2deg(rad: Double) = rad / Math.PI * 180.0
    
      def getDistanceMeters(lat1: Double, lon1: Double, lat2: Double, lon2: Double) = {
        val theta = lon1 - lon2
        val dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) *
          Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta))
        Math.abs(
          Math.round(
            rad2deg(Math.acos(dist)) * 60 * 1.1515 * 1.609344 * 1000)
        )
      }
    

提交回复
热议问题