Geocoder, How to Test Locally When Ip Is 127.0.0.1

How to get city name and Remote IP address from Geocoder gem?

The geocoder gem provides a #location method on the request object (source) which returns a location result with latitude and longitude:

request.location

=> #<Geocoder::Result::Freegeoip:0x007f8ecca5d608
@cache_hit=true, @data= {
"ip"=>"199.21.87.210", "country_code"=>"US",
"country_name"=>"United States", "region_code"=>"CA", "region_name"=>"California", "city"=>"Berkeley", "zipcode"=>"", "latitude"=>37.8716, "longitude"=>-122.2728, "metro_code"=>"807", "areacode"=>"510"
}>

To test this functionality in the browser, try using ngrok to expose your local server and throw some debugger statements in your controller to see what the request.location object looks like.

Rails - How to use a geocoder to get the current location via IP

Nielsen,

While it is great for address geocoding using the configured 'lookup' service, unfortunately the geocoder gem is hardcoded to use only Freegeoip for all IP address lookups. And I agree with you that Freeogeoip has fairly poor accuracy (not good enough for my use case).

From geocoder.rb:

def lookup(query)
if ip_address?(query)
get_lookup(ip_lookups.first)
else
get_lookup(Configuration.lookup || street_lookups.first)
end
end

##
# All IP address lookups, default first.
#
def ip_lookups
[:freegeoip]
end

I think that a better solution would be to use the geoip gem (http://geoip.rubyforge.org/) with the free MaxMind GeoCityLite database, or if you need even more accuracy you can get the premium version. I am getting significantly more accuracy with GeoCityLite then Freegeoip, even though Freegeoip claims to be derived from the same original database...

I am using the following GeoIP call to resolve the location for an IP address:

@geoip_city = GeoIP.new('lib/GeoLiteCity.dat').city(request.remote_ip)

There are two things to keep in mind:

  1. This will only work accurately on live requests, as your local
    network/loopback adapter will not provide an IP that works for the
    request.remote_ip call.

  2. The flat file lookup is very fast, but I am still not sure if there
    is any potential for memory leaks or other scalability problems. I am still testing this solution so I will update response if I find anything significant.

Get real IP address in local Rails development environment

As far as I can see there is no standard method for getting the remote address of your local machine. If you need the remote address for (testing) your geocoding, I suggest adding 127.0.0.1 to your database table that matches IP addresses with locations.

As a last resort you could also hard code your remote address. E.g. like this:

class ApplicationController < ActionController::Base
def remote_ip
if request.remote_ip == '127.0.0.1'
# Hard coded remote address
'123.45.67.89'
else
request.remote_ip
end
end
end

class MyController < ApplicationController
def index
@client_ip = remote_ip()
end
end

Get locations nearby request.location with ruby/rails geocoder gem

If you have the longitude and latitude of the user's location, you can make a query like so

location_info = request.location

@locations = Location.near([location_info.latitude, location_info.longitude], radius_distance_you_want_to_include)

It would also be expected that your table, i.e. in the above example Location there should be lat and lon fields.

Is this something you were looking for?

EDIT: I'm not sure if request.location will work in the model. In case it doesn't, you can always set it as a parameter into a model method inside your controller so that your logic stays separated. But if you can, keep request.location in the model.

Googlemaps API Key for Localhost

  1. Go to this address: https://console.developers.google.com/apis
  2. Create new project and Create Credentials (API key)
  3. Click on "Library"
  4. Click on any API that you want
  5. Click on "Enable"
  6. Click on "Credentials" > "Edit Key"
  7. Under "Application restrictions", select "HTTP referrers (web sites)"
  8. Under "Website restrictions", Click on "ADD AN ITEM"
  9. Type your website address (or "localhost", "127.0.0.1", "localhost:port" etc for local tests) in the text field and press ENTER to add it to the list
  10. SAVE and Use your key in your project


Related Topics



Leave a reply



Submit