diff --git a/README.md b/README.md index 5d92a9a592c5ac8c5ba1117d52695985891362e3..54b0b24931f078dedfebe5b2b21f4d20603d59e2 100644 --- a/README.md +++ b/README.md @@ -478,6 +478,17 @@ Yahoo BOSS is **not a free service**. As of November 17, 2012 Yahoo no longer of * **Terms of Service**: http://www.developer.nokia.com/Develop/Maps/TC.html * **Limitations**: ? +#### Here/Nokia (`:here`) + +* **API key**: required +* **Quota**: Depending on the API key +* **Region**: world +* **SSL support**: yes +* **Languages**: The preferred language of address elements in the result. Language code must be provided according to RFC 4647 standard. +* **Documentation**: http://developer.here.com/rest-apis/documentation/geocoder +* **Terms of Service**: http://developer.here.com/faqs#l&t +* **Limitations**: ? + #### ESRI (`:esri`) * **API key**: none diff --git a/lib/geocoder/lookup.rb b/lib/geocoder/lookup.rb index fbfec2f8d364e557841e7cab7636256f54d7cea4..b92713b16ef2340921d5c3df26dcda1d375999d5 100644 --- a/lib/geocoder/lookup.rb +++ b/lib/geocoder/lookup.rb @@ -33,6 +33,7 @@ module Geocoder :nominatim, :mapquest, :ovi, + :here, :baidu, :cloudmade, :geocodio, diff --git a/lib/geocoder/lookups/here.rb b/lib/geocoder/lookups/here.rb new file mode 100644 index 0000000000000000000000000000000000000000..7083bedfabf2b11079014b39713fb627f7166c38 --- /dev/null +++ b/lib/geocoder/lookups/here.rb @@ -0,0 +1,62 @@ +require 'geocoder/lookups/base' +require 'geocoder/results/here' + +module Geocoder::Lookup + class Here < Base + + def name + "Here" + end + + def required_api_key_parts + [] + end + + def query_url(query) + "#{protocol}://#{if query.reverse_geocode? then 'reverse.' end}geocoder.api.here.com/6.2/#{if query.reverse_geocode? then 'reverse' end}geocode.json?" + url_query_string(query) + end + + private # --------------------------------------------------------------- + + def results(query) + return [] unless doc = fetch_data(query) + return [] unless doc['Response'] && doc['Response']['View'] + if r=doc['Response']['View'] + return [] if r.nil? || !r.is_a?(Array) || r.empty? + return r.first['Result'] + end + [] + end + + def query_url_params(query) + options = { + :gen=>4, + :app_id=>api_key, + :app_code=>api_code + } + + if query.reverse_geocode? + super.merge(options).merge( + :prox=>query.sanitized_text, + :mode=>:retrieveAddresses + ) + else + super.merge(options).merge( + :searchtext=>query.sanitized_text + ) + end + end + + def api_key + if a=configuration.api_key + return a.first if a.is_a?(Array) + end + end + + def api_code + if a=configuration.api_key + return a.last if a.is_a?(Array) + end + end + end +end diff --git a/lib/geocoder/results/here.rb b/lib/geocoder/results/here.rb new file mode 100644 index 0000000000000000000000000000000000000000..ea82ff92be366abf30219af0ec637f7be470a854 --- /dev/null +++ b/lib/geocoder/results/here.rb @@ -0,0 +1,62 @@ +require 'geocoder/results/base' + +module Geocoder::Result + class Here < Base + + ## + # A string in the given format. + # + def address(format = :full) + address_data['Label'] + end + + ## + # A two-element array: [lat, lon]. + # + def coordinates + fail unless d = @data['Location']['DisplayPosition'] + [d['Latitude'].to_f, d['Longitude'].to_f] + end + + def state + address_data['County'] + end + + def province + address_data['County'] + end + + def postal_code + address_data['PostalCode'] + end + + def city + address_data['City'] + end + + def state_code + address_data['State'] + end + + def province_code + address_data['State'] + end + + def country + fail unless d = address_data['AdditionalData'] + if v = d.find{|ad| ad['key']=='CountryName'} + return v['value'] + end + end + + def country_code + address_data['Country'] + end + + private # ---------------------------------------------------------------- + + def address_data + @data['Location']['Address'] || fail + end + end +end diff --git a/test/fixtures/here_madison_square_garden b/test/fixtures/here_madison_square_garden new file mode 100644 index 0000000000000000000000000000000000000000..464b8f851ef5795b68c6f543274c34de0e517ac7 --- /dev/null +++ b/test/fixtures/here_madison_square_garden @@ -0,0 +1,72 @@ +{ + "Response": { + "MetaInfo": { + "Timestamp": "2013-02-08T16:26:39.382+0000" + }, + "View": [ + { + "_type": "SearchResultsViewType", + "ViewId": 0, + "Result": [ + { + "Relevance": 1.0, + "MatchLevel": "houseNumber", + "MatchQuality": { + "State": 1.0, + "City": 1.0, + "Street": [ + 1.0 + ], + "HouseNumber": 1.0 + }, + "MatchType": "pointAddress", + "Location": { + "LocationId": "NT_ArsGdYbpo6dqjPQel9gTID_4", + "LocationType": "point", + "DisplayPosition": { + "Latitude": 40.7504692, + "Longitude": -73.9933777 + }, + "NavigationPosition": [ + { + "Latitude": 40.7500305, + "Longitude": -73.9942398 + } + ], + "MapView": { + "TopLeft": { + "Latitude": 40.7515934, + "Longitude": -73.9948616 + }, + "BottomRight": { + "Latitude": 40.7493451, + "Longitude": -73.9918938 + } + }, + "Address": { + "Label": "4 Penn Plz, New York, NY 10001, United States", + "Country": "USA", + "State": "NY", + "County": "New York", + "City": "New York", + "Street": "Penn Plz", + "HouseNumber": "4", + "PostalCode": "10001", + "AdditionalData": [ + { + "value": "United States", + "key": "CountryName" + }, + { + "value": "New York", + "key": "StateName" + } + ] + } + } + } + ] + } + ] + } +} diff --git a/test/fixtures/here_no_results b/test/fixtures/here_no_results new file mode 100644 index 0000000000000000000000000000000000000000..26fdaf93a8c450af5e6229de2ee34f5d1127a774 --- /dev/null +++ b/test/fixtures/here_no_results @@ -0,0 +1,8 @@ +{ + "Response": { + "MetaInfo": { + "Timestamp": "2013-02-08T16:41:16.723+0000" + }, + "View": [] + } +}