0

I've heard of Geocoder and Geokit but what I know is that they only work if we provide their functions some parameters . either city /country name or location co-ordinates . But what i want is to get my own location either 'Area name' or latitude or longitude in ruby on rails ,with any gem . Don't suggest request.location because it give me localhost address , not my internet ip .so plz suggest anything reasonable because i'm totally freaked out

Mani
  • 2,391
  • 5
  • 37
  • 81
  • 1
    You cannot test it in local without passing some fake IP address, please check this [answer](http://stackoverflow.com/a/17034967/3863146), and there's one more thing, you can only make some limited number of requests for getting user location after that you will start getting error messages. You can also catch this exception but in the end only limited requests are allowed. – Sahil Nov 30 '15 at 13:20
  • thanks sahil i think 127.0.0.1 will continue until we're at local , so we have to provide ip as mention in that answer – Mani Nov 30 '15 at 13:23
  • Checkout [this](https://github.com/alexreisner/geocoder#google-google) while choosing which service you want to use for determining location. – Sahil Nov 30 '15 at 13:28

2 Answers2

1

You can get the ip using

ip = request.remote_ip

or

ip = request.env['REMOTE_ADDR'] 

or if you are using devise for user authentication you can just search by the ip obtained via devise

ip = user.last_sign_in_ip || user.current_sign_in_ip

Use Geocoder's search method to get location and coordinates.

location = Geocoder.search(ip)
country = location["country_name"]
state = location["region_name"]
city = location["city"]
zipcode = geoloc["zipcode"]
latitude = geoloc["latitude"]
longitude = geoloc["longitude"]
area_code = geoloc["area_code"]
Oss
  • 4,232
  • 2
  • 20
  • 35
0

You can also try this

location = IpGeocoder.geocode(request.env['REMOTE_ADDR'])

here is the documentation for the same

Chirag Shah
  • 509
  • 1
  • 10
  • 25
  • but for that thing we need the ip of the user , which we don't have and at this time `request.env['REMOTE_ADDR']` show us the `127.0.0.1` – Mani Nov 30 '15 at 14:19
  • @ImranNaqvi it happens on when you work with local, see here http://stackoverflow.com/questions/6115589/geocoder-how-to-test-locally-when-ip-is-127-0-0-1?lq=1 – Chirag Shah Dec 01 '15 at 05:39
  • I hate it ;( - but i found a solution which i'll explain later – Mani Dec 01 '15 at 06:50