Created
July 17, 2017 18:46
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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