lib/plus_codes/open_location_code.rb
# Removes four, six or eight digits from the front of an Open Location Code(Plus+Codes) given a reference location.
#
# @param code [String] a plus+codes
# @param latitude [Numeric] a latitude in degrees
# @param longitude [Numeric] a longitude in degrees
# @return [String] a short plus+codes
Removes four, six or eight digits from the front of an Open Location Code(Plus+Codes) given a reference location.
Removes four six or eight digits from the front of an Open Location Code ( Plus + Codes ) given a reference location .
def shorten(code, latitude, longitude)
      raise ArgumentError,
      "Open Location Code(Plus+Codes) is a valid full code: #{code}" unless full?(code)
      raise ArgumentError,
      "Cannot shorten padded codes: #{code}" unless code.index(PADDING).nil?
      code_area = decode(code)
      lat_diff = (latitude - code_area.latitude_center).abs
      lng_diff = (longitude - code_area.longitude_center).abs
      max_diff = [lat_diff, lng_diff].max
      [8, 6, 4].each do |removal_len|
        area_edge = precision_by_length(removal_len) * 0.3
        return code[removal_len..-1] if max_diff < area_edge
      end
      code.upcase
    end
def shorten ( code , latitude , longitude ) raise ArgumentError , "Open Location Code(Plus+Codes) is a valid full code: #{code}" unless full? ( code ) raise ArgumentError , "Cannot shorten padded codes: #{code}" unless code . index ( PADDING ) . nil? code_area = decode ( code ) lat_diff = ( latitude - code_area . latitude_center ) . abs lng_diff = ( longitude - code_area . longitude_center ) . abs max_diff = [ lat_diff , lng_diff ] . max [ 8 , 6 , 4 ] . each do | removal_len | area_edge = precision_by_length ( removal_len ) * 0.3 return code [ removal_len .. - 1 ] if max_diff < area_edge end code . upcase end
Removes four, six or eight digits from the front of an Open Location Code(Plus+Codes) given a reference location.
Removes four six or eight digits from the front of an Open Location Code ( Plus + Codes ) given a reference location .
a short plus+codes
a short plus + codes
String
code
None
latitude
a latitude in degrees
a latitude in degrees
longitude
a longitude in degrees
a longitude in degrees