i using leaflet-rails gem (0.7.4) , need calculate track distance in case path , fro. in our application, upload gpx file , display points using leaflet. calculate track distance first , last point in case of , fro path track distance comes out zero. here points
array of coordinates (latitude , longitude).
def deg2rad deg return deg * math::pi / 180; end def fetch_track_distance distance = 0 start_point = self.points.first end_point = self.points.last if start_point.present? && end_point.present? dlat = self.deg2rad(end_point.latitude - start_point.latitude) dlon = self.deg2rad(end_point.longitude - start_point.longitude) r = math::sin(dlat/2) * math::sin(dlat/2) + math::cos(self.deg2rad(start_point.latitude)) * math::cos(self.deg2rad(end_point.latitude)) * math::sin(dlon/2) * math::sin(dlon/2); c = 2 * math::atan2(math.sqrt(r), math::sqrt(1-r)); mdist = radian * c; distance = (mdist/1000) if mdist.present? end return distance.round(3) end
thanks in advance.
Comments
Post a Comment