lib/plus_codes/open_location_code.rb
def recover_nearest(short_code, reference_latitude, reference_longitude)
      return short_code if full?(short_code)
      raise ArgumentError,
      "Open Location Code(Plus+Codes) is not valid: #{short_code}" unless short?(short_code)

      ref_lat = clip_latitude(reference_latitude)
      ref_lng = normalize_longitude(reference_longitude)

      prefix_len = SEPARATOR_POSITION - short_code.index(SEPARATOR)
      code = prefix_by_reference(ref_lat, ref_lng, prefix_len) << short_code
      code_area = decode(code)

      area_range = precision_by_length(prefix_len)
      area_edge = area_range / 2

      latitude = code_area.latitude_center
      latitude_diff = latitude - ref_lat
      if (latitude_diff > area_edge)
        latitude -= area_range
      elsif (latitude_diff < -area_edge)
        latitude += area_range
      end

      longitude = code_area.longitude_center
      longitude_diff = longitude - ref_lng
      if (longitude_diff > area_edge)
        longitude -= area_range
      elsif (longitude_diff < -area_edge)
        longitude += area_range
      end

      encode(latitude, longitude, code.length - SEPARATOR.length)
    end
# Recovers a full Open Location Code(Plus+Codes) from a short code and a reference location.
#
# @param short_code [String] a plus+codes
# @param reference_latitude [Numeric] a reference latitude in degrees
# @param reference_longitude [Numeric] a reference longitude in degrees
# @return [String] a plus+codes
Recovers a full Open Location Code(Plus+Codes) from a short code and a reference location.
Recovers a full Open Location Code ( Plus + Codes ) from a short code and a reference location .
def recover_nearest(short_code, reference_latitude, reference_longitude)
      return short_code if full?(short_code)
      raise ArgumentError,
      "Open Location Code(Plus+Codes) is not valid: #{short_code}" unless short?(short_code)
      ref_lat = clip_latitude(reference_latitude)
      ref_lng = normalize_longitude(reference_longitude)
      prefix_len = SEPARATOR_POSITION - short_code.index(SEPARATOR)
      code = prefix_by_reference(ref_lat, ref_lng, prefix_len) << short_code
      code_area = decode(code)
      area_range = precision_by_length(prefix_len)
      area_edge = area_range / 2
      latitude = code_area.latitude_center
      latitude_diff = latitude - ref_lat
      if (latitude_diff > area_edge)
        latitude -= area_range
      elsif (latitude_diff < -area_edge)
        latitude += area_range
      end
      longitude = code_area.longitude_center
      longitude_diff = longitude - ref_lng
      if (longitude_diff > area_edge)
        longitude -= area_range
      elsif (longitude_diff < -area_edge)
        longitude += area_range
      end
      encode(latitude, longitude, code.length - SEPARATOR.length)
    end
def recover_nearest ( short_code , reference_latitude , reference_longitude ) return short_code if full? ( short_code ) raise ArgumentError , "Open Location Code(Plus+Codes) is not valid: #{short_code}" unless short? ( short_code ) ref_lat = clip_latitude ( reference_latitude ) ref_lng = normalize_longitude ( reference_longitude ) prefix_len = SEPARATOR_POSITION - short_code . index ( SEPARATOR ) code = prefix_by_reference ( ref_lat , ref_lng , prefix_len ) << short_code code_area = decode ( code ) area_range = precision_by_length ( prefix_len ) area_edge = area_range / 2 latitude = code_area . latitude_center latitude_diff = latitude - ref_lat if ( latitude_diff > area_edge ) latitude -= area_range elsif ( latitude_diff < - area_edge ) latitude += area_range end longitude = code_area . longitude_center longitude_diff = longitude - ref_lng if ( longitude_diff > area_edge ) longitude -= area_range elsif ( longitude_diff < - area_edge ) longitude += area_range end encode ( latitude , longitude , code . length - SEPARATOR . length ) end
Recovers a full Open Location Code(Plus+Codes) from a short code and a reference location.
Recovers a full Open Location Code ( Plus + Codes ) from a short code and a reference location .
None
String
short_code
None
reference_latitude
a reference latitude in degrees
a reference latitude in degrees
reference_longitude
a reference longitude in degrees
a reference longitude in degrees