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.
Related
I have 2 models one for customer and one for vendor, I'm trying do make a site that when customer sign in, they can search using their ip address and search for near by location, the result will be some vendor with nearby address. So far, I plan to use geocoder and gmap4rails gem. I'm still not sure how I would store the vendors address using geocoder so that when the customer search, the correct result will be presented. Can some one give suggestion on how to design this feature?
Since you're just looking for a "design" and not code, I can just outline the broad steps:
ip form submitted =>
lookup geodata for local businesses =>
store geodata in "vendor" model =>
The only non-standard rails part of this is the geo-lookup.
There are multiple ways to do this, most certainly using some external API. I don't think the geocoder or gmap4rails gems have this functionality.
While you're probably using gmap4rails for your view layer, I'm not sure you'll need the geocoder gem if you're using a different service to perform the vendors lookup. This external service might accomplish the same thing.
As for which external service to use: I'd look at Yelp or one of Google's APIs.
In response to your comment:
When a user logs in, you'll probably want to set a session cookie for them.
There is no reason to have the ip form "belong to" any user. When the form is submitted, you'd be able to look up the current signed in user from the controller action which is hit when the form is submitted.
If you store vendor data in your database, you can then present the data in the view.
If these concepts are new to you, I'd recommend going through a detailed tutorial of Rails.
I'm making a web application in Rails 4 that is going to allow users to create trips. These trips are going to be built dynamically using Google Maps API based upon user input. IE, users can create different waypoints and locations that they visited. I want users to be able to add waypoints and descriptions about particular stops.
But I want these locations to be stored so that there's a search function where users can search for different regions such as around a particular address or state.
I'm not looking for anything overly specific here, I just want an idea of how to set it up.
What I've thought of so far is that Users create trips which have many waypoints that in turn are models. I'm not sure that is ideal however.
Thoughts? Even a link to a post I might have missed is helpful. Thanks.
Edit: In case anyone runs across this problem in the future. I went with Google Map's Directions API. It lets you pass in waypoints as an array of objects. I used JavaScript to add waypoints as necessary and then allowed the user to submit to how the map looked. Then, if they liked it, they submitted the trip form which logged the input values into the db as attributes to the trip object.
I'm working on a rails app that needs to route users to a specific URL based on their location. Preferably something that will present them the appropriate content based on location with the ability for them to be able to view content for other locations.
Specifically, think of the location interface for Craigslist... Users are presented content from the city they are in and still allowed to select and view another city.
I've seen a few posts that answer parts of this question, but I'm trying to plan out the best solution.
It looks like there is going to need to be something, probably cookie based, that sets a 'default' location for a given user and still allows them to select other locations.
Again, just looking for concept/planning assistance and any direction on any gems that might be applicable.
Thanks in advance!
http://dev.maxmind.com/geoip/geolite is a free geo-ip database that works pretty well. It makes some mistakes (it put a client's office of mine in Kirkland, WA when they are in fact in downtown Seattle, WA). Certainly is good enough for Craigslist level specificity since you'd be re-routing both those people to "seattle" anyway. There's a ruby gem for it as well - "geoip-c". It's very easy to use.
The other option would be to use HTML5's "gimme your location" functionality. More intrusive for the user, but might be more specific.
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")
Hii,
We want to redirect our users to one of our web pages corresponding to the users city (location based upon the users ip address and using some ip location databases)
My question is, how to make it work fast? for example in website gropoun, whenever the user visits, it instantly takes the user to its city page.
Thanks.
Edit: We are using PHP
Do you want to do this server or client side? If client side (ie, using javascript), you can use one of many geoip services out there. One in particular is Yahoo!'s YGL
http://developer.yahoo.com/yql/
http://developer.yahoo.com/yql/console/?q=select%20City,RegionName,CountryName%20from%20ip.location%20where%20ip%3D'8.8.8.8'&env=store://datatables.org/alltableswithkeys
Also, you can do it server-side using pretty much and language or framework. You could make API or service calls to third party geoip providers, or you can load the data into your database and do your own look up.
http://www.maxmind.com/app/geolitecity
http://ipinfodb.com/ip_database.php
http://ipinfodb.com/ip_location_api.php
You will also need to "default" to a region or zipcode as every IP address can not be determined. For example, one web application that I currently work on has a 95% USA audience, so we default to the geographical center of the country which is 66952.
You should determine user ip from httpRequest afterwards use some kind of database for example geoip
Cache, cache, cache everything. Cache lookups in your IP table, cache the results for individual users in their session or cookies, cache the rendered localization information portion of your pages (or at least the query intensive parts.)
There are more details that could be given, but it all depends on what your bottlenecks are. (After all there's no point in implementing complex caching on the routing side of things if the bottleneck is in rendering localized information because your DB calls take almost half a second to run). I cannot tell you where the bottlenecks in your application are / will be. You'll need to profile it first -- then optimize on the basis of what the profiler tells you.
I have done this for a few clients server-side using the lookup service http://ipinfodb.com/ip_location_api.php.
Just remember to store IP addresses and locations in the database so you do not do redundant lookups. I used the time zone data to determine the visitor's region.