-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
bing.clj
67 lines (56 loc) · 1.56 KB
/
bing.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(ns geocoder.bing
(:require [geocoder.util :refer [fetch-json format-location]]))
(defn city
"Returns the city of `address`."
[address]
(:locality (:address address)))
(defn country
"Returns the country of `address`."
[address]
{:name (:country-region (:address address))})
(defn location
"Returns the geographical location of `address`."
[address]
(let [[y x] (:coordinates (:point address))]
{:lat y :lng x}))
(defn street-name
"Returns the street name of `address`."
[address]
(:address-line (:address address)))
(defn postal-code
"Returns the postal code of `address`."
[address]
(:postal-code (:address address)))
(defn region
"Returns the region of `address`."
[address]
(:state address))
(defn- request
"Make a Bing geocode request map."
[geocoder & [opts]]
{:request-method :get
:url "http://dev.virtualearth.net/REST/v1/Locations"
:query-params
(assoc opts :key (:api-key geocoder))})
(defn- fetch
"Fetch and decode the Bing geocode response."
[request]
(->> (fetch-json request)
:resource-sets
(mapcat :resources)))
(defn geocode-address
"Geocode an address."
[geocoder address & {:as opts}]
(-> (request geocoder opts)
(assoc-in [:query-params :query] address)
(fetch)))
(defn geocode-location
"Geocode a geographical location."
[geocoder location & {:as opts}]
(-> (request geocoder opts)
(update-in [:url] #(format "%s/%s" %1 (format-location location)))
(fetch)))
(defn geocoder
"Returns a new Bing geocoder."
[& [{:keys [api-key]}]]
{:api-key api-key})