<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -4,73 +4,127 @@ require 'rexml/document'
 
 module Ym4r
   module GoogleMaps
-    module Geocoding
+	module Geocoding
 
-      GEO_SUCCESS = 200
-      GEO_MISSING_ADDRESS = 601
-      GEO_UNKNOWN_ADDRESS = 602
-      GEO_UNAVAILABLE_ADDRESS = 603
-      GEO_BAD_KEY = 610
-      GEO_TOO_MANY_QUERIES = 620
-      GEO_SERVER_ERROR = 500
-      
-      #Gets placemarks by querying the Google Maps Geocoding service with the +request+ string
-      def self.get(request)
-        
-        url = &quot;http://maps.google.com/maps/geo?q=#{request}&amp;key=#{API_KEY}&amp;output=xml&quot;
-        begin
-          xml = open(URI.encode(url)).read
-        rescue
-          raise ConnectionException.new(&quot;Unable to connect to Google Maps Geocoding service&quot;)
-        end
-        
-        doc = REXML::Document.new(xml) 
-                        
-        response = doc.elements['//Response']
-        placemarks = Placemarks.new(response.elements['name'].text,response.elements['Status/code'].text.to_i)
-        response.elements.each(&quot;Placemark&quot;) do |placemark|
-          data = placemark.elements
-          data_country = data['descendant::CountryNameCode']
-          data_administrative = data['descendant::AdministrativeAreaName']
-          data_sub_administrative = data['descendant::SubAdministrativeAreaName']
-          data_locality = data['descendant::LocalityName']
-          data_dependent_locality = data['descendant::DependentLocalityName']
-          data_thoroughfare = data['descendant::ThoroughfareName']
-          data_postal_code = data['descendant::PostalCodeNumber']
-          placemarks &lt;&lt; Geocoding::Placemark.new(data['address'].text,
-                                                 data_country.nil? ? &quot;&quot; : data_country.text,
-                                                 data_administrative.nil? ? &quot;&quot; : data_administrative.text,
-                                                 data_sub_administrative.nil? ? &quot;&quot; : data_sub_administrative.text,
-                                                 data_locality.nil? ? &quot;&quot; : data_locality.text,
-                                                 data_dependent_locality.nil? ? &quot;&quot; : data_dependent_locality.text,
-                                                 data_thoroughfare.nil? ? &quot;&quot; : data_thoroughfare.text,
-                                                 data_postal_code.nil? ? &quot;&quot; : data_postal_code.text,
-                                                 *(data['descendant::coordinates'].text.split(&quot;,&quot;)[0..1].collect {|l| l.to_f }))
-        end
-        placemarks
-      end
+	  GEO_SUCCESS = 200
+	  GEO_MISSING_ADDRESS = 601
+	  GEO_UNKNOWN_ADDRESS = 602
+	  GEO_UNAVAILABLE_ADDRESS = 603
+	  GEO_BAD_KEY = 610
+	  GEO_TOO_MANY_QUERIES = 620
+	  GEO_SERVER_ERROR = 500
+	  
+	  #Gets placemarks by querying the Google Maps Geocoding service with the +request+ string. Options can either an explicity GMaps API key (&lt;tt&gt;:key&lt;/tt&gt;) or a host, (&lt;tt&gt;:host&lt;/tt&gt;). 
+	  def self.get(request,options = {})
+	    api_key = options[:key]
+	    output =  options[:output] || &quot;kml&quot;
+	    url = &quot;http://maps.google.com/maps/geo?q=#{URI.encode(request)}&amp;key=#{api_key}&amp;output=#{output}&quot;
 
-      #Group of placemarks returned by the Geocoding service. If the result is valid the +statius+ attribute should be equal to &lt;tt&gt;Geocoding::GEI_SUCCESS&lt;/tt&gt;
-      class Placemarks &lt; Array
-        attr_accessor :name,:status
+	    res = open(url).read
 
-        def initialize(name,status)
-          super(0)
-          @name = name
-          @status = status
-        end
-      end
+	    case output.to_sym
+	    when :json
+	      res = eval(res.gsub(&quot;:&quot;,&quot;=&gt;&quot;)) #!!!EVAL EVAL EVAL EVAL!!! hopefully we can trust google...
+	      placemarks = Placemarks.new(res['name'],res['Status']['code'])
+	      if res['Placemark']
+	        placemark = res['Placemark']
+	        
+	        placemark.each do |data|
+	          
+	          data_country = data['Country']['CountryNameCode'] rescue &quot;&quot;
+	          data_administrative = data['Country']['AdministrativeArea']['AdministrativeAreaName'] rescue &quot;&quot;
+	          data_sub_administrative = data['Country']['AdministrativeArea']['SubAdministrativeArea']['SubAdministrativeAreaName'] rescue &quot;&quot;
+	          data_locality = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'] rescue &quot;&quot;
+	          data_dependent_locality = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['DependentLocality']['DependentLocalityName'] rescue &quot;&quot;
+	          data_thoroughfare = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['DependentLocality']['Thoroughfare']['ThoroughfareName'] rescue &quot;&quot;
+	          data_postal_code = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['DependentLocality']['Thoroughfare']['PostalCode']['PostalCodeNumber'] rescue &quot;&quot;
+	          lon, lat = data['Point']['coordinates'][0,2]
+	          data_accuracy = data['Accuracy']
+	          unless data_accuracy.nil?
+	            data_accuracy = data_accuracy.to_i
+	          end
+	          
+	          placemarks &lt;&lt; Geocoding::Placemark.new(data['address'],
+	                                                 data_country,
+	                                                 data_administrative,
+	                                                 data_sub_administrative,
+	                                                 data_locality,
+	                                                 data_dependent_locality,
+	                                                 data_thoroughfare,
+	                                                 data_postal_code,
+	                                                 lon, lat, data_accuracy)
+	          
+	        end
+	      end
+	    when :kml, :xml
+	      
+	      doc = REXML::Document.new(res) 
 
-      #A result from the Geocoding service.
-      class Placemark &lt; Struct.new(:address,:country_code,:administrative_area,:sub_administrative_area,:locality,:dependent_locality,:thoroughfare,:postal_code,:longitude,:latitude)
-        def lonlat
-          [longitude,latitude]
-        end
+	      response = doc.elements['//Response']
+	      placemarks = Placemarks.new(response.elements['name'].text,response.elements['Status/code'].text.to_i)
+	      response.elements.each(&quot;.//Placemark&quot;) do |placemark|
+	        data = placemark.elements
+	        data_country = data['.//CountryNameCode']
+	        data_administrative = data['.//AdministrativeAreaName']
+	        data_sub_administrative = data['.//SubAdministrativeAreaName']
+	        data_locality = data['.//LocalityName']
+	        data_dependent_locality = data['.//DependentLocalityName']
+	        data_thoroughfare = data['.//ThoroughfareName']
+	        data_postal_code = data['.//PostalCodeNumber']
+	        lon, lat = data['.//coordinates'].text.split(&quot;,&quot;)[0..1].collect {|l| l.to_f }
+	        data_accuracy = data['.//*[local-name()=&quot;AddressDetails&quot;]'].attributes['Accuracy']
+	        unless data_accuracy.nil?
+	          data_accuracy = data_accuracy.to_i
+	        end
+	        placemarks &lt;&lt; Geocoding::Placemark.new(data['address'].text,
+	                                               data_country.nil? ? &quot;&quot; : data_country.text,
+	                                               data_administrative.nil? ? &quot;&quot; : data_administrative.text,
+	                                               data_sub_administrative.nil? ? &quot;&quot; : data_sub_administrative.text,
+	                                               data_locality.nil? ? &quot;&quot; : data_locality.text,
+	                                               data_dependent_locality.nil? ? &quot;&quot; : data_dependent_locality.text,
+	                                               data_thoroughfare.nil? ? &quot;&quot; : data_thoroughfare.text,
+	                                               data_postal_code.nil? ? &quot;&quot; : data_postal_code.text,
+	                                               lon, lat, data_accuracy )
+	      end
+	    end
+	    
+	    placemarks
+	  end
 
-        def latlon
-          [latitude,longitude]
-        end
-      end
+	  #Group of placemarks returned by the Geocoding service. If the result is valid the +status+ attribute should be equal to &lt;tt&gt;Geocoding::GE0_SUCCESS&lt;/tt&gt;
+	  class Placemarks &lt; Array
+	    attr_accessor :name,:status
+
+	    def initialize(name,status)
+	      super(0)
+	      @name = name
+	      @status = status
+	    end
+	  end
+
+	  #A result from the Geocoding service.
+	  class Placemark &lt; Struct.new(:address,:country_code,:administrative_area,:sub_administrative_area,:locality,:dependent_locality,:thoroughfare,:postal_code,:longitude,:latitude,:accuracy)
+	    def lonlat
+	      [longitude,latitude]
+	    end
+
+	    def latlon
+	      [latitude,longitude]
+	    end
+	  end
+
+	  def self.accuracy(code)
+	    {0 =&gt; &quot;Unknown location&quot;,
+	     1 =&gt; &quot;Country level accuracy&quot;,
+	     2 =&gt; &quot;Region (state, province, prefecture, etc.) level accuracy&quot;,
+	     3 =&gt; &quot;Sub-region (county, municipality, etc.) level accuracy&quot;,
+	     4 =&gt; &quot;Town (city, village) level accuracy&quot;,
+	     5 =&gt; &quot;Post code (zip code) level accuracy&quot;,
+	     6 =&gt; &quot;Street level accuracy&quot;,
+	     7 =&gt; &quot;Intersection level accuracy&quot;,
+	      8 =&gt; &quot;Address level accuracy&quot;}[code]
+	    
+	  end
     
       #Raised when the connection to the service is not possible
       class ConnectionException &lt; StandardError</diff>
      <filename>lib/ym4r/google_maps/geocoding.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>7717f07d93a85295107c288f56d1dd502c7aefd6</id>
    </parent>
  </parents>
  <author>
    <name>gvellut</name>
    <email>gvellut</email>
  </author>
  <url>http://github.com/nofxx/ym4r/commit/6c19973ec0645ba4a32e6a59b81948ece620c1f0</url>
  <id>6c19973ec0645ba4a32e6a59b81948ece620c1f0</id>
  <committed-date>2008-02-07T04:45:32-08:00</committed-date>
  <authored-date>2008-02-07T04:45:32-08:00</authored-date>
  <message>qquelques changements</message>
  <tree>ac43442da004cf4360665fbd513503f39a9bf249</tree>
  <committer>
    <name>gvellut</name>
    <email>gvellut</email>
  </committer>
</commit>
