-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgeocode.bas
98 lines (92 loc) · 3.7 KB
/
geocode.bas
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Const vbErr = 3 ' Red
Const vbOK = 23 ' Cyan
Public Sub RecalculateSelection()
If TypeName(Selection) = "Range" Then
Selection.Calculate
End If
End Sub
Function GoogleReverseGeocode(lat As Double, lng As Double, key As String) As String
Dim xDoc As New MSXML2.DOMDocument
xDoc.async = False
xDoc.Load ("https://maps.googleapis.com/maps/api/geocode/xml?key=" & key & "&latlng=" & lat + "," & lng)
If xDoc.parseError.ErrorCode <> 0 Then
Application.Caller.Font.ColorIndex = vbErr
GoogleReverseGeocode = xDoc.parseError.reason
Else
xDoc.SetProperty "SelectionLanguage", "XPath"
result = xDoc.SelectSingleNode("/GeocodeResponse/status").Text
If result <> "OK" Then
Application.Caller.Font.ColorIndex = vbErr
GoogleReverseGeocode = result
Else
Application.Caller.Font.ColorIndex = vbOK
GoogleReverseGeocode = xDoc.SelectSingleNode("//formatted_address").Text
End If
End If
End Function
Function GoogleGeocode(address As String, key As String) As String
Application.Caller.Font.ColorIndex = xlNone
Dim xDoc As New MSXML2.DOMDocument
xDoc.async = False
xDoc.Load ("https://maps.googleapis.com/maps/api/geocode/xml?key=" + key + "&address=" + WorksheetFunction.EncodeURL(address))
If xDoc.parseError.ErrorCode <> 0 Then
Application.Caller.Font.ColorIndex = vbErr
GoogleGeocode = xDoc.parseError.reason
Else
xDoc.SetProperty "SelectionLanguage", "XPath"
result = xDoc.SelectSingleNode("/GeocodeResponse/status").Text
If result <> "OK" Then
Application.Caller.Font.ColorIndex = vbErr
GoogleGeocode = result
Else
Application.Caller.Font.ColorIndex = vbOK
GoogleGeocode = xDoc.SelectSingleNode("//lat").Text & "," & xDoc.SelectSingleNode("//lng").Text
End If
End If
End Function
Function NominatimReverseGeocode(lat As Double, lng As Double) As String
On Error GoTo eh
Dim xDoc As New MSXML2.DOMDocument
xDoc.async = False
Url = "https://nominatim.openstreetmap.org/reverse?lat=" & lat & "&lon=" & lng
xDoc.Load (Url)
If xDoc.parseError.ErrorCode <> 0 Then
Application.Caller.Font.ColorIndex = vbErr
NominatimReverseGeocode = xDoc.parseError.reason
Else
xDoc.SetProperty "SelectionLanguage", "XPath"
Dim loc As MSXML2.IXMLDOMElement
Set loc = xDoc.SelectSingleNode("/reversegeocode/result")
If loc Is Nothing Then
Application.Caller.Font.ColorIndex = vbErr
NominatimReverseGeocode = xDoc.XML
Else
Application.Caller.Font.ColorIndex = vbOK
NominatimReverseGeocode = loc.Text
End If
End If
Exit Function
eh:
Debug.Print err.Description
End Function
Function NominatimGeocode(address As String) As String
Application.Caller.Font.ColorIndex = xlNone
Dim xDoc As New MSXML2.DOMDocument
xDoc.async = False
xDoc.Load ("https://nominatim.openstreetmap.org/search?format=xml&q=" + WorksheetFunction.EncodeURL(address))
If xDoc.parseError.ErrorCode <> 0 Then
Application.Caller.Font.ColorIndex = vbErr
NominatimGeocode = xDoc.parseError.reason
Else
xDoc.SetProperty "SelectionLanguage", "XPath"
Dim loc As MSXML2.IXMLDOMElement
Set loc = xDoc.SelectSingleNode("/searchresults/place")
If loc Is Nothing Then
Application.Caller.Font.ColorIndex = vbErr
NominatimGeocode = xDoc.XML
Else
Application.Caller.Font.ColorIndex = vbOK
NominatimGeocode = loc.getAttribute("lat") & "," & loc.getAttribute("lon")
End If
End If
End Function