lib/geokit/geocoders/bing.rb in geokit-1.8.4 vs lib/geokit/geocoders/bing.rb in geokit-1.8.5

- old
+ new

@@ -2,10 +2,11 @@ module Geocoders # Bing geocoder implementation. Requires the Geokit::Geocoders::bing variable to # contain a Bing Maps API key. Conforms to the interface set by the Geocoder class. class BingGeocoder < Geocoder config :key, :options + self.secure = true private # Template method which does the geocode lookup. def self.do_geocode(address) @@ -18,11 +19,11 @@ def self.submit_url(address) culture = options && options[:culture] culture_string = culture ? "&c=#{culture}" : '' address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address - "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address_str)}?key=#{key}#{culture_string}&o=xml" + "#{protocol}://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address_str)}?key=#{key}#{culture_string}&o=xml" end def self.parse_xml(xml) return GeoLoc.new if xml.elements['//Response/StatusCode'].try(:text) != '200' loc = nil @@ -59,31 +60,34 @@ def self.set_address_components(loc, xml) set_mappings(loc, xml, XML_MAPPINGS) end + ACCURACY_MAP = { + 'High' => 8, + 'Medium' => 5, + 'Low' => 2 + } + + PRECISION_MAP = { + 'Sovereign' => 'country', + 'CountryRegion' => 'country', + 'AdminDivision1' => 'state', + 'AdminDivision2' => 'state', + 'PopulatedPlace' => 'city', + 'Postcode1' => 'zip', + 'Postcode2' => 'zip', + 'RoadBlock' => 'street', + 'Address' => 'address' + } + def self.set_precision(loc, xml) if xml.elements['.//Confidence'] - loc.accuracy = case xml.elements['.//Confidence'].text - when 'High' then 8 - when 'Medium' then 5 - when 'Low' then 2 - else 0 - end + loc.accuracy = ACCURACY_MAP[xml.elements['.//Confidence'].text] || 0 end if xml.elements['.//EntityType'] - loc.precision = case xml.elements['.//EntityType'].text - when 'Sovereign' then 'country' - when 'AdminDivision1' then 'state' - when 'AdminDivision2' then 'state' - when 'PopulatedPlace' then 'city' - when 'Postcode1' then 'zip' - when 'Postcode2' then 'zip' - when 'RoadBlock' then 'street' - when 'Address' then 'address' - else 'unkown' - end + loc.precision = PRECISION_MAP[xml.elements['.//EntityType'].text] || 'unknown' end end def self.set_bounds(loc, xml) if suggested_bounds = xml.elements['.//BoundingBox']