Skip to content

Instantly share code, notes, and snippets.

@ytspar
Created July 17, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytspar/da4d201394c6db5b135a94fae7d7d7e3 to your computer and use it in GitHub Desktop.
Save ytspar/da4d201394c6db5b135a94fae7d7d7e3 to your computer and use it in GitHub Desktop.
Get latitude and longitude from a string (e.g., extract an exact location from Facebook directions)
#!/usr/bin/env ruby
# Get lat, long from a string
# Example: https://wego.here.com/directions/mix//One-Ounce-for-Onion,-1912-Ekkamai12,-Sukhumvit-63-Rd.,-Klongton-Nua,-Wattana,-10110-Bangkok:e-eyJuYW1lIjoiT25lIE91bmNlIGZvciBPbmlvbiIsImFkZHJlc3MiOiIxOVwvMTIgRWtrYW1haTEyLCBTdWtodW12aXQgNjMgUmQuLCBLbG9uZ3RvbiBOdWEsIFdhdHRhbmEsIEJhbmdrb2ssIFRoYWlsYW5kIDEwMTEwIiwibGF0aXR1ZGUiOjEzLjczMDg2NjY4NDk2OSwibG9uZ2l0dWRlIjoxMDAuNTg5NzMzNDA0MTYsInByb3ZpZGVyTmFtZSI6ImZhY2Vib29rIiwicHJvdmlkZXJJZCI6MTg4MjcxMTU4MDE2MTIwfQ==?map=13.73087,100.58973,15,normal&fb_locale=en_US
# Outputs 13.73087,100.58973 and drops it into the clipboard on Mac
puts 'Enter a URL'
s = gets.chomp
reg = '(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)'
matches = []
s.scan(Regexp.new(reg)) do
matches << Regexp.last_match
end
separator = '-' * matches.join.length
puts separator
puts (matches.empty?)? "nothing found" : matches
puts separator
def pbcopy(input)
str = input.to_s
IO.popen('pbcopy', 'w') { |f| f << str }
str
end
pbcopy matches.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment