Created
May 6, 2015 14:23
-
-
Save jeffdeville/9c88cc374139eae41802 to your computer and use it in GitHub Desktop.
This file contains 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 | |
begin | |
gem 'geocoder' | |
rescue | |
p "you must install the geocoder gem: `gem install geocoder`, or `sudo gem install geocoder`" | |
raise | |
end | |
require 'pry' | |
require 'geocoder' | |
if ARGF.argv.length != 1 | |
p "Usage: mapproperties [FILE_PATH]" | |
raise | |
end | |
# Configure | |
Geocoder.configure(lookup: :google) | |
# 0. Load file | |
addresses = File.readlines(ARGF.argv[0]).map{|line| line.chomp} | |
pp addresses | |
# 1. geocode each address | |
results = addresses.map do |address| | |
r = Geocoder.search(address).first | |
{ "address" => address }.merge(r.geometry['location']) | |
end | |
# 2. 1st address = the center | |
def create_markers(property, *comps) | |
property_marker = create_marker(property["lat"], property["lng"], "0", "green") | |
comp_markers = comps.map.with_index do |comp, ind| | |
create_marker(comp["lat"], comp["lng"], ind + 1) | |
end | |
(Array(property_marker) + comp_markers).join("&") | |
end | |
def create_marker(lat, lng, label, color="red") | |
"markers=color:#{color}%7Clabel:#{label}%7C#{lat},#{lng}" | |
end | |
markers = create_markers *results | |
# 3. create points in order of appearance | |
p "https://maps.googleapis.com/maps/api/staticmap?center=#{results[0]["lat"]},#{results[0]["lng"]}&zoom=13&size=600x300&maptype=roadmap&#{create_markers(*results)}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment