Geocoding on the fly vs database lookup - ruby-on-rails

I'm starting a new rails project that integrates closely with Google Maps. When a user searches for a city, I'm trying to decide whether to geocode the address on the fly (using Google's Geocoding API) or to look up the city in a database pre-populated with lat/long. After I have the lat/long I will plot it on Google Maps.
Which do you think would perform better? With the database lookup, the table would have to be pretty large to account for all the cities I would need and I would have to fallback on the geocoding API anyway for any cities that I don't have in my database.
I wasn't sure if there is a common practice to this or not. I don't need a user's specific location, but just a city they are searching for.

The size of the table is no problem, as long as you index on the city name.
Performance of indexed database queries outspeed web API access by far.
An other point is, that you have better controll of the found data. For example, if you find more than one matching city, you can provide a choice of your DB entries, while Google sometimes reports none or some random (or at least unexpected) search result.
This is, why I had to change to a DB search first strategy in one of my project: Google somtimes didn't find my customers addresses but something total different (i.e. small villages with the same name as the expected bigger one)

Why not do both?
Have the address's geocoded information in your database as "Address Cache" and then call the Google Maps Geocode API only if the address doesn't already exist in your database. That's the approach I used in my Google Maps to SugarCRM integration. It works well. BTW, the Google Maps Geocode API is impressively fast, so users rarely notice. Yet, there is a 2,500/day limit on request and it's also throttled to about 10 requests per second. So, considering those limits, I think a combination database/geocode approach is much better in the long run.
https://github.com/jjwdesign/JJWDesign-Google-Maps

Related

Tools for estimating nearby locations without calling external API (i.e Google Maps)

In my rails application i have some model Location that holds the address and corresponding latitude and longitude.
At the main page, user can search for a location from which he wants to find the nearest places in location table.
I use JavaScript Google API for geocoding on the client side. Since the limit per user is 25000 req/day i guess i do not need to worry about it cause no one will want to search for a location so many times. I use Ajax for sending geocoded latitude and longitude from client to the server.
Now i'm on the sever side and have a reference point and table of locations. But at this moment, i guess i cannot use geocoder gem which use Google API for estimating some nearby locations, cause from the server side there is a limit of 2500 requests per day and i expect to exceed it(don't want to pay either).
What tool can i use to easily return some nearby locations without calling external API? I know that there are other API's but all of them have either limitations or obligations and since it's more like mathematical calculation i can't see the reason to play around with API at this point.
Assuming that you're using a relational database, you can use ElasticSearch to index your Locations and search through them using Geo Distance Range Query.
Other similar option would be use a Mongo collection and take advantage of the Mongo's $near operator.
Edit:
There're some questions related to your question which can be useful if you're using MySQL/PostgreSQL:
Find closest 10 cities with MySQL using latitude and longitude?
postgres longitude longitude query

Rails Geocoder MapIt functionality alternatives

I'm working with rails, geocoder and gmap4rails. Trying to figure out how to show boundaries of an ward district area in UK. Found mapit - Mapit by mySociaty . Would be amazing if anyone could suggest or point me in the direction for creating a similar functions in rails. I'm especially interested in functions like 'Touching this area' as showed in the mapit.mysociety.org site.
Google maps also show boundaries of an area e.g. google map example Shows boundaries for 'Roath' area. is there anyway retrieve that data from google ?
I also tested a lot of addresses with geocoder, looking up in google. Tried to Geocode from address administrative_area_level_1 2 and 3 as well as in locality or sublocality.
The results were very unreliable. Tried at least 20 addresses from birmigham city - only some of them returned administrative_area_level1,2,3 or locality or sublocality and most of the time it was wrong data.
Another website that is sort of showing ward areas with in a city is www.streetlife.com I think it only works for UK users. basically it takes a postcode from a user and then in their map they show users district and other district around it, which is pretty cool :) Does anyone know how it works ? or how to get something like that working in rails ?
I would really love to discuss this topic in more depth and figure out the best answers for it.
This question at gis.stackexchange is probably of use to you if you wish to use rails. The "touching" feature is fundamentally a PostGIS query, so you could build on top of anything that allowed you to make SQL queries to a PostgreSQL database - GeoDjango (on which MapIt is built) makes this nice, but you could certainly accomplish the same in rails with work.
You won't be able to get those boundaries you see out of Google Maps, I believe, as it's proprietary.
Geocoding is a separate topic - you may want to look at things like OpenStreetMap's Nominatim (MapQuest have a version) or GeoNames, which could supply a dataset you could use with geocoder - Ordnance Survey also publish GB datasets that could be of use.

Which Maps API should I use?

I am creating a webpage that includes maps for my software engineering thesis. The page will include following features:
Show a specific location and save it to a database;
Showing different roads in the same map and save then to database;
Getting the nearest road that passes nearby a specific location pointed by a user - a little search function;
Users might be allowed to create different roads, which can be saved in a database.
The thing is that the service (API) used should be free. For this reason, we might not be using Google Maps.
We are using Java for the Model Classes.
Which maps API can I use?
How can I ask it which of the roads is nearest to a certain point (location) on the map?
Google Maps API is Free as long as you are not using more than a certain amount of traffic. If its for a class project it should be fine, but if that project turned into a commercial site, it would become expensive.

Geolocation and getting a city from an input address (Rails)

The app I'm building needs to be able to match up users to events based on the city/town they're in. I'm still relatively new to Rails and completely new to Geolocation and using locations in an app. I'd figured on a design where users have one or many cities, and events would have one city which I'd hoped to extract without specifically asking the user for it, by getting it from the event address entered.
Mostly to provide some outside checking to help get the address entered correctly and consistently, but also to show a map, I installed this jquery address picker (https://github.com/sgruhier/jquery-addresspicker). Unfortunately the data returned by Google doesn't include a city but a "locality" or an "administrative area" that doesn't correlate reliably to city names. The localities being returned are more like what we in my home town would call "suburbs". What I need to procure is a city so I can allow users to search all events in their city rather than just the ones in their suburb.
Can anyone offer advice on how I could go about doing this? Many thanks.
Edit: Should maybe add that I'm wanting to do geocoding client-side so I don't run into problems with Google Maps limits or have to pay for geocoding etc.
There are some gems that provide you with that and may others geo related features, like calculating distances.
Here are the 2 most famous: https://github.com/alexreisner/geocoder and https://github.com/imajes/geokit
In the future I highly recommend you to head to https://www.ruby-toolbox.com/ to see what is available as a gem already and see what is the most popular at the moment.
For raw address info, use Google Maps API Reverse Geocoding which accepts lat/lon inputs and returns street address components. Modern browsers support location awareness (geolocation), with user permission, and will give you a lat/lon that "tends to be close" to where the browser is. That will probably get you a correct city/town in most cases.
The maps API is part of Google's broad suite of API tools -- there are gems that handle any Google API (well, most of them), or check out Google Maps for Rails, which will at the very least give you a good head start on how to use the API.
But if you're looking to validate postal code, this method will come up short, since the location awareness will vary in accuracy depending on browser, device (more accurate for mobile), the connection, population density, network coverage, and so on. Also, calling the
If you can get GPS-accurate lat/lon then it will be much more accurate ... except in some cases like in large cities, a single building will have its own postal code, so a few feet one way or the other might matter.

How to implement keyword and location search with MongoDB?

I am trying to implement a web/smart phone app that allow users to search for places based on keywords and location and here is the requirement:
Users shall be able to search by typing in keywords and location; Locations can be zip code, city/state or current location from the mobile app (lat and long)
We would like to be able to customize relevance score; We need to be able to define our own relevance algorithm based on keyword matching, location matching and some other parameters.
We use ASP.NET MVC as our web development framework and MongoDB as a data store. We also maintain a list of all zipcode and city/state as well as their centroid (lat/long) in our database. Our thought is override the scoring that the full-text system provide (like Lucene scoring) with our own algorithm. I am trying to find the best solution to address this. I am wondering whether should we use MongoDB full-text search or try to use Lucene .NET or perhaps Solr? Any help/pointer/comment is always apprecated!
So as a starting point, MongoDB does not have support for full-text search.
It has some regex capabilities and you can index on arrays. So you can do some things here, like building an array of keywords to make basic text search possible.
However, this is a long way from what Solr and Sphinx.
The other big problem you'll have is with relevance scoring. It's going to be very difficult to perform any type of server-side relevance scoring with MongoDB. There's no really efficient version of a server-side stored procedure. You'll likely have to pull the results to a client or server dedicated to that scoring.

Resources