mapbox / mapbox-sdk-rb

A Ruby interface to Mapbox APIs.
Other
59 stars 32 forks source link

Readme Example Mapbox::Geocoder.geocode_reverse - Fails Using String Keys #13

Closed dthyresson closed 6 years ago

dthyresson commented 7 years ago

The README example:

# Reverse geocoding
placenames = Mapbox::Geocoder.geocode_reverse({
  "latitude" => 38,
  "longitude" => -100
})

fails

NoMethodError: undefined methodround' for nil:NilClass`

because https://github.com/mapbox/mapbox-sdk-rb/blob/master/lib/mapbox/geocoder.rb#L21 expects symbolic keys as in:

  placenames = Mapbox::Geocoder.geocode_reverse({
    latitude: 38,
    longitude: -100
  })

which returns

[
    {
        "attribution": "NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained.",
        "features": [
            {
                "center": [
                    -100.000007176662,
                    38.000312209294201
                ],
                "context": [
                    {
                        "id": "place.9991796195976750",
                        "text": "Jetmore",
toadkicker commented 6 years ago

You want to do something like this:

JSON.parse(location, symbolize_names: true)

edit:

That being said it could be useful if the geocoder accepted an array of [lat,lng] or a hash with indifferent access.