Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created April 22, 2011 15:39
Show Gist options
  • Save judofyr/936901 to your computer and use it in GitHub Desktop.
Save judofyr/936901 to your computer and use it in GitHub Desktop.
module MLS
extend self
GRID = ("A".."Z").to_a
SQUARE = ("0".."9").to_a
SUB = ("a".."z").to_a
lat = 180.0
PARTS = [[18, GRID], [10, SQUARE], [24, SUB], [10, SQUARE]]
PARTS.map! { |div, part| [lat = lat / div, part] }
def from_latlng(lat, lng)
lat += 90
lng += 180
PARTS.inject("") do |res, (div, part)|
part_lat, lat = lat.divmod(div)
part_lng, lng = lng.divmod(div * 2)
res << part[part_lng] << part[part_lat]
end
end
def to_latlng(mls)
lat = -90
lng = -180
mls.split('').each_slice(2).zip(PARTS) do |(lng_part, lat_part), (div, part)|
lat += part.index(lat_part) * div
lng += part.index(lng_part) * div * 2
end
return lat, lng
end
end
if $0 == __FILE__
res = MLS.from_latlng(59.723649, 10.824333)
p MLS.to_latlng(res)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment