#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_pure
andjson
to 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
;)
check out here, the geocode actually suggests a way to test it. it's probably something new in their documentation
https://github.com/alexreisner/geocoder#user-content-testing-apps-that-use-geocoder
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?
Old problem, but I was working on a similar problem. I solved it by using the "area" gem, and converting the zip code (passed as string) into a lat/long pair.
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
ya i also face same kind of problem i also try to search area near to zip code but it will result same zip code data
are you having same kind of problem
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!
any one have solution to find area near to the zip_code i try near method but it will give me all the area which has the same zip_code it can not find near zip_code area so any one have solution then reply me... thanx
i use ex:-> @something = SomeThing.near(params[:search])
in params[:search] i found zip_code of the city like (10001)
if any one have solution of this please reply me
Hello,
I am having issues with the near by locations not showing up in my application. Any ideas as to why or what i need to do to fix it? I followed the tutorial and no luck. Thanks!
I am trying to use geocoder gem to geocode address into latitude and longitude. However when I create a new record, it only records the address string but doesn't contain the latitude and longitude of the given address.
I am behind a proxy, but I have my proxy variable set.
What could be the problem here ?
Looks like there's an typo in the ascii casts in the listing for
/app/views/locations/show.html.erb
The first few lines look like it was duplicated and pasted in in the wrong place.
+1
I did everything as given in the tutorial, but somehow i couldn't seem to generate latitude and longitude automatically when i leave the values blank.
Throws an error infact in "show.html.erb" in this particular line
//
<% for location in @location.nearbys(10) %> -> error line
undefined method `each' for nil:NilClass -> error
//
Can any1 pls help. Thanks in advance.
PS: Im using rails 4 so i don't have to write the attr_accessible line. Just to mention.
How does Geocoder comply with section 10.1.3b in the Google Maps TOS?
How to fix this error "Geocoding API not responding fast enough (use Geocoder.configure(:timeout => ...) to set limit)"
How to fix this error "Geocoding API not responding fast enough (use Geocoder.configure(:timeout => ...) to set limit)"
I have configured timeout as 20 but still I get same error.
I'm actually interested in the nearby function. So what I want is that when a user(x) logs in his/her location (lat/long) is calculated and the updated in the database. If another user(y) logs in the nearby function should be able to check if user x is in a 50 mile radius or not. If yes then show it on the main page.... How do I do that please help.
Just kidding super awesome tutorial.... Great work
Just kidding super awesome tutorial.... Great work