How to calculate the distance between two GPS coordinates without using Google Maps API?

前端 未结 7 1109
小鲜肉
小鲜肉 2020-12-08 02:56

I\'m wondering if there\'s a way to calculate the distance of two GPS coordinates without relying on Google Maps API.

My app may receive the coordinates in float or

7条回答
  •  無奈伤痛
    2020-12-08 03:26

    You can use the geokit ruby gem. It does these calculations internally, but also supports resolving addresses via google and other services if you need it to.

    require 'geokit'
    
    current_location = Geokit::LatLng.new(37.79363,-122.396116)
    destination = "37.786217,-122.41619"
    current_location.distance_to(destination) 
    
    # Returns distance in miles: 1.211200074136264
    

    You can also find out the bearing_to (direction expressed as a float in degrees between 0-360) and midpoint_to (returns an object you can run .latitude and .longitude methods on).

提交回复
热议问题