· 1 min read
Screencast: Geocoder
Downloads in verschiedenen Formaten:
Resourcen:
bash
[bash] rails g nifty:scaffold location address:string latitude:float longitude:float rake db:migrate bundle [/bash]
Gemfile
[ruby] gem ‘geocoder’ [/ruby]
models/location.rb
[ruby] geocoded_by :address after_validation :geocode, :if => :address_changed? [/ruby]
locations_controller.rb
[ruby] def index if params[:search].present? @locations = Location.near(params[:search], 50, :order => :distance) else @locations = Location.all end end [/ruby]
locations/show.html.erb
[html] <%= image_tag “http://maps.google.com/maps/api/staticmap?size=450x300&sensor=false&zoom=16&markers=#{@location.latitude}%2C#{@location.longitude}” %>
Nearby locations
- <% for location in @location.nearbys(10) %>
- <%= link\_to location.address, location %> (<%= location.distance.round(2) %> miles) <% end %>
locations/index.html.erb
[html] <%= form_tag locations_path, :method => :get do %>
<%= text_field_tag :search, params[:search] %> <%= submit_tag “Search Near”, :name => nil %>
<% end %> [/html]