#273 Geocoder
If you need to work with geographic data, Geocoder is an excellent gem for converting addresses and coordinates, finding nearby locations, determining distances, and more!
- Download:
- source codeProject Files in Zip (195 KB)
- mp4Full Size H.264 Video (18.6 MB)
- m4vSmaller H.264 Video (9.43 MB)
- webmFull Size VP8 Video (9.95 MB)
- ogvFull Size Theora Video (22.6 MB)
Keep in mind that doing geocode server side is not a best practice.
Google API limit is 2500 req/day (and decrease every year :))
Should be done client side with plugin like this: https://github.com/sgruhier/jquery-addresspicker
Sounds like this could save some requests esp. when finding an address.
Thank you.
What's the difference between Geocoder and GeoKit? I've used GeoKit before and found it very good.
I was wondering the same thing. Both look like they are done well but GeoKit seems like its been around a lot longer than Geocoder, but I could be wrong.
Geocoder is lighter, easier to use, and has features missing from GeoKit like built-in result caching. It's also designed to be compatible with more storage engines (like MongoDB) and environments (JRuby, Rack, SQLite, etc).
GeoKit has been around longer but Geocoder is also production quality.
Geokit wasn't working in Rails 3, when we tried to use it earlier this year.
I think GeoKit uses MySQL specific calculations to find nearest destinations.
When I tried Geocoder, I found the query results were not accurate and I didn't have time to look into them. I submitted a suggestion on this.
Here's another way to work with Google Maps: jquery-ui-map - a jQuery plugin.
http://code.google.com/p/jquery-ui-map/
Someone know how can I set intersections?
The crappy thing about using the Google Maps API is that there is a commercial restriction on the data. If your app makes money, then you need a license, which will run you $10K/year. Ouch!
What opensource provider would you suggest? I looked at mapquest but their api hasn't changed since august '10.
Otherwise openlayers looks really bad.
If I had a good alternative to google, I'd update my gem to implement it.
Maybe take a look at OpenStreetMap and their API.
Great Idea, I've just added openlayers, mapquest and bing as optionnal map providers in the gem. It's now direct to change from one to another (some functionalities may still lack but markers and infowindows are fine)
yes, but they don't even find my house ! GoogleMap does !
Geocoder currently supports Google, Yahoo, Bing, Yandex and Geocoder.ca APIs for street address geocoding. Geocoder.ca is free but only provides data for the US and Canada. Easy enough to configure:
The Geocoder README has a table comparing the features of the various APIs.
I spent a bunch of time researching providers because of the common theme in licensing terms, even with Geocoder.ca.
For the giants (Google, Yahoo, Bing) you are prohibited from doing anything for free other than displaying on their mapping API, including caching the results. Geocoder.ca requires you to be non-profit and provide attribution.
I eventually settled on GIS @ USC -- their terms are much less restrictive, I don't mind giving them attribution, and past the free 2500 transactions they're incredibly cheap in comparison to other commercial licenses (10k transactions = $20, 1M transactions = $1600.)
Handling the web service was a minimal amount of code using Net::HTTP::Post for transport, Nokogiri parses the XML, and I can cache results without worry. Maybe not the solution for everyone, but pretty well what I was looking for! Hope this helps someone...
What if the way that your app is making money isn't from the Google Maps data? For example:
If I had an ecommerce site as well as chain of retail stores and I wanted to use the Google Maps API for something like a Store Locator. Would I still still need to have said license?
I use my own helper to put the map code into the head via content for. The gem isn't worth the hassle IMO.
Can you share your helper in a gist?
Awesome cast, concise and clear.
been playing with the gem but cannot figure out how to change from miles to km or is this even an option?
You can use
Thanks,
I did look through the source and found that I can pass the unit I want as an options hash allowing me to provider user switching in the future.
I was under the impression that you should use a decimal data type when storing the latitude and longitude because a float isn't precise enough, what are your thoughts on this?
In general I think of single-precision floats as being precise to approximately a city block. Double-precision floats should be accurate to within inches.
Calculations with decimals are slower so I don't recommend them unless you need some really extreme precision (in which case you should probably be using a proper geospatial datastore).
Hey thanks for that info, I'll have to do some research on this geospatial datastore
Check out MySQL spatial extensions and PostGIS.
If only I had known about this gem before I finished my collegiate capstone project. =(
Oh well, still won the contest =P
Hello, Ryan.
Five days ago I installed gmaps4rails gem and, since then, I'm very happy with it. So I feel compelled to testify about its easiness of use, specially with maps handling.
Just out of the box, gmaps4rails offers the ability to show a collection of items in a map, which, by the way, is also easy to set with a helper method.
I don't know if this "is" Google API or gmaps4rails, but the default map groups the items when you zoom out.
I believe that, by the default, geocoding happens after validations. And you can set your callback method.
So, Ryan, could you please make a screencast about gmaps4rails?
Thanks!
I agree, gmaps4rails is a great gem. Try it first if you need to handle maps in your app.
Ryan, Thanks. Your screencasts have helped me such a great deal.
Does anyone have inputs on how to find businesses between two points/along the path ?
tried this under rails 3.1 and get
Any ideas?
Edit: fixed formatting - @sethvargo
Looks like @marqueymarc was able to resolve his issue by explicitly adding
json_pureandjsonto the Gemfile.reference: http://stackoverflow.com/questions/6636929/railscast-geocoding-missing-jsonparsererror-constant
Geocoder is a great gem and works perfectly fine for me until
I added will_paginate to paginate the results using .near()
It never gives me the correct number of items on the view which is really weird. Any help would be appreciated, thanks!
Man you are a life saver. Thanks for the episode.
Is it possible to make the method of geocoding conditional? i want to geocode by ip_address by default but if the user updates there current address, I want to then geocode by that.Anyone have ideas on how to do that? something like
whoops, had those geocoded_by calls reversed
How are people who are using this testing? I'm trying to find the best place to insert a hook to prevent actual geocoding during my tests.
Using rspec, I'm doing the following: (Geocoded Zipcode stores zipcode->lat/lon pairs)
I'm really not happy with this solution. Gotta be a better way.
You may try VCR to stub the HTTP response.
http://github.com/myronmarston/vcr
;)
For some reason my Nearby Locations aren't working. I haven't tried to go past that part. I've tried copy and pasting the code into the right files and it still doesn't work :(. Anyone have an idea as to what would be causing this?
How does the distance_from work? Is the distance considering the streets, the same as used on maps.google.com (by walk, by car, by bus and etc) or just straight point to point?
I think that it's straight point to point...
http://rdoc.info/github/alexreisner/geocoder/master/Geocoder/Calculations#distance_between-instance_method
http://en.wikipedia.org/wiki/Haversine_formula
It looks good, but it's not ready to use in nontrivial projects, you cannot use namescopes, something like this doesn't works
Product.published.near("Barcelona", 50, :order => :distance)
:(
i am getting the following error when trying to add form for search functionality
TypeError in Locations#index
Showing c:/rails/maps/app/views/locations/index.html.erb where line #4 raised:
can't convert Symbol into Integer
Extracted source (around line #4):
1: All Locations
2: <%= form_tag locations_path, :method => :get do %>
3:
4: <%= text_field_tag :search, :params[:search] %>
5: <%= submit_tag "search near", :name => nil %>
6:
7: <% end %>
Rails.root: c:/rails/maps
Shouldn't line 4 be:
<%= text_field_tag :search, params[:search] %>params doesn't take a : in front of it
FYI, anyone attempting to add search through an association:
http://stackoverflow.com/questions/8867844/model-association-issue-in-rails/8868269#8868269
Anyone know how to use this to convert a search into a zip code?
For me the application does not show the near by locations, even if I run the code as is..
Anyone had similar problems?
Would appreciate any help on how to fix this.
Thx
Hi.. I am working with geocoder.
In my model, i am having fields like location, city, state, country and Pin code/ Zip code..
With the help of Geocoder i could be able to locate the complete address including street, city, state and country.
But my requirement is i need to locate the place with the help of Zip code too...
Here is my model:
class Employee < ActiveRecord::Base
attr_accessible :name, :location, :city, :state, :country, :pincode, :latitude, :longitude
def address
[location, city, state, country].compact.join(', ')
end
geocoded_by :address
after_validation :geocode, :if => :location_changed?
end
Can anyone help me out????????????
Hi. . . another great cast. . .
Any pointers to tutorials for using ipaddress with geocoder.
I've tried a few things but have not gotten it to work.
Many thanks
Hi anyone know how to add advanced search, by name and address ...???
You sir, made my day!
First sign in through GitHub to post a comment.