Hi Henry,
Doesn''t include any err testing/checking, but ... you could do
something like:
### in some rails project root:
$ cat ./tmp/t.runnable
def parse_lat_lng(json_str)
lat, lng = [0.0, 0.0]
location =
JSON.parse(json_str)[''results''][0][''geometry'']
[''location'']
lat, lng = location[''lat''],
location[''lng'']
return [lat, lng]
end
#------------------
json_str = <<EOS
{
"status": "OK",
"results": [ {
"types": [ "point_of_interest",
"establishment" ],
"formatted_address": "Buckingham Palace, Westminster, London,
UK",
"address_components": [ {
"long_name": "Buckingham Palace",
"short_name": "Buckingham Palace",
"types": [ "point_of_interest",
"establishment" ]
}, {
"long_name": "London",
"short_name": "London",
"types": [ "locality", "political" ]
}, {
"long_name": "Westminster",
"short_name": "Westminster",
"types": [ "administrative_area_level_3",
"political" ]
}, {
"long_name": "Greater London",
"short_name": "Gt Lon",
"types": [ "administrative_area_level_2",
"political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level_1",
"political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 51.5007890,
"lng": -0.1422640
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 51.4938426,
"lng": -0.1582714
},
"northeast": {
"lat": 51.5077344,
"lng": -0.1262566
}
}
}
} ]
}
EOS
lat, lng = parse_lat_lng(json_str)
puts "lat=#{lat} lng=#{lng}"
$ ./script/runner ./tmp/t.runnable
lat=51.500789 lng=-0.142264
Jeff
On May 24, 7:02 pm, Henry Oss
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> The recently released Google maps API v3 returns geocoding info in json
> format as shown below. I am sure there is an elegant way to extract the
> lat/lng but I have failed to find it after many attempts - can anyone
> help? Thanks in advance.
>
> {
> "status": "OK",
> "results": [ {
> "types": [ "point_of_interest",
"establishment" ],
> "formatted_address": "Buckingham Palace, Westminster,
London, UK",
> "address_components": [ {
> "long_name": "Buckingham Palace",
> "short_name": "Buckingham Palace",
> "types": [ "point_of_interest",
"establishment" ]
> }, {
> "long_name": "London",
> "short_name": "London",
> "types": [ "locality", "political" ]
> }, {
> "long_name": "Westminster",
> "short_name": "Westminster",
> "types": [ "administrative_area_level_3",
"political" ]
> }, {
> "long_name": "Greater London",
> "short_name": "Gt Lon",
> "types": [ "administrative_area_level_2",
"political" ]
> }, {
> "long_name": "England",
> "short_name": "England",
> "types": [ "administrative_area_level_1",
"political" ]
> }, {
> "long_name": "United Kingdom",
> "short_name": "GB",
> "types": [ "country", "political" ]
> } ],
> "geometry": {
> "location": {
> "lat": 51.5007890,
> "lng": -0.1422640
> },
> "location_type": "APPROXIMATE",
> "viewport": {
> "southwest": {
> "lat": 51.4938426,
> "lng": -0.1582714
> },
> "northeast": {
> "lat": 51.5077344,
> "lng": -0.1262566
> }
> }
> }
> } ]}
>
> --
> Posted viahttp://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group
athttp://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.