0
votes

[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

1
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

1 Answers

0
votes

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