Skip to content
Snippets Groups Projects
Commit f2601e86 authored by Alex Reisner's avatar Alex Reisner
Browse files

Fix incompatibility with certain Rails versions: don't use Hash.from_xml...

Fix incompatibility with certain Rails versions: don't use Hash.from_xml (XmlMini) to parse Google response--some versions of ActiveSupport lowercase all hash keys.
parent be9d57df
No related branches found
No related tags found
No related merge requests found
...@@ -28,19 +28,18 @@ module Geocoder ...@@ -28,19 +28,18 @@ module Geocoder
# Returns array [lat,lon] if found, nil if not found or if network error. # Returns array [lat,lon] if found, nil if not found or if network error.
# #
def self.fetch_coordinates(query) def self.fetch_coordinates(query)
data = self.search(query) doc = self.search(query)
# Make sure search found a result. # Make sure search found a result.
unless data and data['kml']['response']['status']['code'] == "200" unless (e = doc.elements['kml/Response/Status/code']) and e.text == "200"
return nil return nil
end end
# Isolate the relevant part of the result. # Isolate the relevant part of the result.
place = data['kml']['response']['placemark'] place = doc.elements['kml/Response/Placemark']
# If there are multiple results, blindly use the first. # If there are multiple results, blindly use the first.
place = place.first if place.is_a?(Array) coords = place.elements['Point/coordinates'].text
coords = place['point']['coordinates']
coords.split(',')[0...2].reverse.map{ |i| i.to_f } coords.split(',')[0...2].reverse.map{ |i| i.to_f }
end end
...@@ -50,7 +49,8 @@ module Geocoder ...@@ -50,7 +49,8 @@ module Geocoder
module ClassMethods module ClassMethods
## ##
# Find all ads within a radius (in miles) of the given location (string). # Find all objects within a radius (in miles) of the given location
# (address string).
# #
def near(location, radius = 100, options = {}) def near(location, radius = 100, options = {})
latitude, longitude = Geocoder.fetch_coordinates(location) latitude, longitude = Geocoder.fetch_coordinates(location)
...@@ -94,10 +94,10 @@ module Geocoder ...@@ -94,10 +94,10 @@ module Geocoder
end end
## ##
# Calculate the distance between two points (Haversine formula). Takes two # Calculate the distance between two points on Earth (Haversine formula).
# sets of coordinates and an options hash: # Takes two sets of coordinates and an options hash:
# #
# :units : <tt>:mi</tt> for miles (default), <tt>:km</tt> for kilometers # +units+ :: <tt>:mi</tt> for miles (default), <tt>:km</tt> for kilometers
# #
def self.distance_between(lat1, lon1, lat2, lon2, options = {}) def self.distance_between(lat1, lon1, lat2, lon2, options = {})
# set default options # set default options
...@@ -191,6 +191,6 @@ module Geocoder ...@@ -191,6 +191,6 @@ module Geocoder
# http://code.google.com/p/gmaps-api-issues/issues/detail?id=233 # http://code.google.com/p/gmaps-api-issues/issues/detail?id=233
doc = resp.body.sub('UTF-8', 'ISO-8859-1') doc = resp.body.sub('UTF-8', 'ISO-8859-1')
Hash.from_xml(doc) REXML::Document.new(doc)
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment