globally unique location id - ruby-on-rails

I'm trying to find a good way to store and connect users based upon their city.
Location names can come from a variety of places, including facebook connection, ip lookup, and event user input.
I want to be able to input an address and get back a unique location ID. NY and New York will be understood as the same, but cities in two locations are not understood as the same.
Can I use facebook's location databse for this? Or google's location api? Or would it be better to roll my own, doing searches and filtering to consecutively narrow down results, from country to state to city, while normalizing for abbreviations, internationalizations, and typos? Or is there a plugin that will do this for me?
Thanks!

I would use https://github.com/alexreisner/geocoder . In general you have to add latitude and longitude to your model (they will identify location) and then specify which field should be geocoded. It allows you to store locations from different sources: city name, ip address and others. There are many configuration options (eg. you can specify Geocoding Services). Check out the gem readme page for all the details.

Related

Restricting User based on GeoLocation in asp.net mvc

in my asp.net mvc app i have a survey Model that can be created by anyone. Moreover, i want people from specific part of world to participate (vote) in the survey. It is easy job if i know the location (it could be city, country or state etc.). i want to add this location restriction at the time of survey creation (i.e user could tell that people of Islamabad or punjab or Pakistan) could vote or fill out this survey form. Moreover, i want to add that location restriction is applicable (or expected) for small number of surveys (5 percent at most) so how to most efficiently implement this functionality.
You could do this a couple of ways:
Determine where the user is from based on a previous question asking their location. Not bullet proof as the user could easily say they are from somewhere they are not.
Obtain an IP -> Country mapping list that will provide you a lookup of the customer's IP address vs. their location. You would restrict based on this.
You can figure out someone's location using IP address. There are many services out there that offer IP address location. They will give you an approximation of the users location based on that.
Here is an example of the service:
You can also get their location using HTML5 geolocation features.
For your case using IP address is probably good enough. The HTML5 option is nice because if the user doesn't have a GPS device on their system it eventually falls back to using IP address location.
In order to get a users IP address in ASP.NET you can use
Request.Servervariables("REMOTE_ADDR")

obtaining foursquare venue id

i am doing some work for an iphone app that retrieves a bunch venues from a webservice, kinda like Yelp. They also want to be able to checkin to one such venue with Foursquare. Is it reasonable to demand that they also return a foursquare venue id, or do i have to look it up given a lat long? My concern is that even if i have a lat long, i dont necessarily know if the venue i looked up is the correct one. Do people just live with this uncertainty, or what? I should also elaborate that the UI is set up so that only one venue gets shown in the checkin UI. Maybe the solution is to provide multiple venues? Ideas? Opinions? Thanks!
If your other webservice offers Foursquare IDs, then getting them there will be your easiest solution. If not, you have two options:
Foursquare provides a location service that you could use to retrieve a list of nearby venues (https://developer.foursquare.com/venues/). That would give you a list of names, categories, addresses, and foursquare ids.
From there, you could search on your other webservice using name and address as needed and have the foursquare id straight from the source.
You can pass the search endpoint (https://developer.foursquare.com/docs/venues/search.html) a location and query string, then iterate over the results to see if you received a direct match.
This system is less precise than (1) (so, yes, you'll have to live with the uncertainty that a venue from your other service might not appear on foursquare, or might be at a slightly-incorrect location), but it will likely work in most situations.
I deal with this for my startup. Unfortunately, there's no good solution.
I have a batch job that given the lat/long and name, it searches the Foursquare API for any venue with the same name & within 0.1 miles. More than 5 times out of 10, the name will be different or there will be multiple venues with the same name. We had to manually set the Foursquare Id.

Is it possible to track the country where a user is accessing a particular website?

I wish to track a user's country from where my website is accesed .
ex; if a customer from a particular is accessing say america how to trace that the user is actually from america.
Is there any way
What you are talking about is called GeoIP and there are many ways to do it. Normally this is done using a third party that has a mapping of IP addresses to physical locations.
This is of course not 100% accurate, as people may be using VPNs, TOR or simply spoofing addresses.
It's not possible in all cases, but most IP addresses can be mapped to a location (even down to the city). There are quite a large number of such geomapping services.
Use MaxMind service.
http://www.maxmind.com/app/javascript_city
They got free and paid versions.
You can determine the country of the IP address of last proxy that a user is using. This is often their country, but not always.
Users can set out to obscure it e.g. by using TOR or another proxy service.
Or their ISPs might be passing them through NAT or through other countries.
And what do you do with the information? Offer them the site in their presumed-native language? Or customise your contact details appropriately?
So you have to think carefully about how you use this information. It is a good idea to present a page in the native language that you think the user is surfing from, but you must make it easy and obvious for the user to change their country. Not all surfers in any given country actually speak the language, and not all people can call toll-free numbers, and not all people in one country are wanting support that's specific to their country, they may be seeking support for when they are elsewhere or for a friend etc.

City/Country text field validation

I need to keep track of the users lat/lng/city/country for my application with the following two requirements:
1) Get the users lat/lng/city/country automatically. (This is easy, I can use the ip or if they have a browser that supports geolocation, even better).
2) The user is allowed to customize this location (maybe the ip address lookup didn't give an accurate city). The location is a freeform text field (not a dropdown). When the user enters a new location it should be validated against available cities/countries. If it validates against any one of them, select it and then retrieve the latlng for the new location. (This is what I'm having trouble with)
Also to clarify, this is a Rails 3 app using MongoDB. I am looking for either a single API or database that would allow me to do both (1) and (2). Has anyone done anything similar? Looking for some ideas as to how others have done this.
Your question isn't entirely clear as to what problem you are having. In general terms, I would do it like this:
have a Location model that stores location name and coordinates
when the user enters a location, send an Ajax request to look it up
if it's found, set the location in the session
if it isn't found, return a list of similarly named locations (in case there was a typo) and let the user choose one or stick to their input
when they are done with the input, insert a new location if required and store User.location_id.
You could use Google's Geocoding API to look up the coordinates of unknown locations.
I would recommend the Geokit Gem, it does a very nice job of providing a front end for several Geocoding APIs. I highly recommend sticking with Yahoo or Google, just for sheer data integrity issues.
There is a rails plugin, that adds some nice helpers to Activerecord. At the moment the main project is not rails 3 compatible, but there is at least one fork that has updated for rails 3.

Browse location with geokit

My user model is using geokit to find the address from an ip address. How do I let the user select a different country, state or city? I have checked all the documentation and I can't figure out how to retrieve a list of countries, states or cities.
Your question isn't 100% clear but you may be trying to do something Geokit isn't designed for.
Geokit basically does one thing: it takes an IP address, or a physical address, and gives you the lat/long. When you install it as a Rails plugin, you can geocode your models (usually with a street address) and it adds some useful stuff to your model classes. These extra things let you compute distances between your model objects.
AFAIK, you can't get a list of countries from Geokit. On my projects I've either used one of the free online lists, exported a list from another application, or subscribed to a service.

Resources