Rails How to get latitude and longitude from string address and put it on google map?

10,897

Solution 1

I agree, geokit is pretty useful...you can do things like:

require 'geokit'
include GeoKit::Geocoders

coords = MultiGeocoder.geocode(location)
puts coords.lat
puts coords.lng

Where location is a string location (like an address). It works pretty well.

You can ALSO reverse geocode, which pulls a string address out of a lat/lng coordinate pair. Pretty spiffy.

Solution 2

Another geocoder option for Ruby is Geocoder: https://github.com/alexreisner/geocoder

  location = Geocoder.search( ... )
  location[0].latitude
  location[0].longitude

Solution 3

I suggest the gem and plugin geokit. It does exactly what you are looking for and more.

Share:
10,897
Adrian Serafin
Author by

Adrian Serafin

Updated on July 18, 2022

Comments

  • Adrian Serafin
    Adrian Serafin almost 2 years

    Here's user story:

    User enters address: Paris, France. Then I would like to display google map center on Paris with one movable marker. Then user point with marker to its exact location and clicks save. Address string and map coordinates are saved to database.

    After some search I know I can display google map with variuos gems: cartographer for example. My question is how to get coordinates or how to pass address to cartographer so it centers map in Paris?

  • Mohd Anas
    Mohd Anas about 10 years
    I am getting this error "uninitialized constant GeoKit (NameError)" even i have installed Geokit....Any suggestions ..thanks in advance
  • Calvin Jones
    Calvin Jones about 10 years
    me as well I think the gem is old
  • Shalafister's
    Shalafister's over 7 years
    Geocoder is really useful. But lately I am getting "query limit reached" error. But I am way under my limit.