0

[Expected Coordinates vs Real1]

    def get_coords(address)
        coords = Geocoder.search(address)
        self.lat = coords[0].data["lat"].to_f
        self.lng = coords[0].data["lon"].to_f
    end

On the front-end a user enters an address for a new TravelCenter, Restaurant, Lodging, or CoffeeShop. In Rails, I use Geocoder to get the latitude and longitude which are saved in the database. Then back on the front-end, I display a marker for each "place" in the given area.

The problem is, the coordinates are way way way way off when creating a new entry in this manner. The first image shows the latest TravelCenter in the Rails Console, and on the left side of the image shows the real world coordinates of the actual truck stop that it represents. Notice that the longitude is too far to the left. On the front end the blue dot that should be on the EAST side of the word Amarillo is on the WEST side instead.

Blue dot in the wrong place

So basically...what gives? Is there something wrong with this particular API?

Furthermore, is there a less messy way to get them coordinates? I have no idea why Geocoder.search returns an array and why the lat and lng are buried so deep.

Update: Left, expected location. Right, Actual location. Conclusion: Ruby Geocoder is garbage. Actual vs Expected

  • Ruby geocoder can't even FIND some places, like the Big Texan Steak Ranch at 7701 Interstate 40 Access Rd, Amarillo, TX 79118..."coords" is coming up nil here. – Aaron Godhard Dec 31 '19 at 05:44

1 Answers1

0

you can try this method below, and you don't have to convert from string to float see this reference if you need more detail

def get_coords(address)
    coords = Geocoder.search(address)
    # your result is in array coords.first.coordinates
    # first index is your lat and second is lng
    self.lat = coords.first.coordinates[0]
    self.lng = coords.first.coordinates[1]
end
widjajayd
  • 6,090
  • 4
  • 30
  • 41
  • That doesn't change the fact that the coordinates are incorrect, am I wrong? – Aaron Godhard Dec 31 '19 at 05:20
  • based from my experience not every address can be converted to coordinates, some times if you misspelled the address other than google database it will return nil, so it's not you / geocoder that wrong, it's just so many possibility for address name, for example some address in my local town using VI instead of 6 – widjajayd Dec 31 '19 at 06:11
  • and how to deal with that?, in my software I put some manual input, so if geocoder not recognize the address, the user can enter manually their coordinate using 2 text box that I provided , user then can check their coordinate through google maps – widjajayd Dec 31 '19 at 06:14
  • There's no way to know whether the Geocoder can't find the address until after the user has submitted the form – Aaron Godhard Jan 01 '20 at 21:12