By: Aung Shine

In reply to Kevin Joseph Wolfe.

Hi KEVIN,
Thanks for reaching out to us. To get only the parameters you mentioned, adjust the XPath expressions used to select the specific elements from the XML response. Here’s the modified code:

Option Explicit

Function ReverseGeocode(lati As Double, longi As Double) As String
On Error GoTo ErrorHandler

Dim xD As New MSXML2.DOMDocument
Dim URL As String

xD.async = False
URL = "https://nominatim.openstreetmap.org/reverse?lat=" & CStr(lati) & _
"&lon=" & CStr(longi)

xD.Load (URL)

If xD.parseError.ErrorCode <> 0 Then
' Handle parsing errors
ReverseGeocode = "Error: " & xD.parseError.reason
Else
' Set XPath selection language
xD.SetProperty "SelectionLanguage", "XPath"

' Select specific elements using XPath
Dim country As MSXML2.IXMLDOMElement
Dim state As MSXML2.IXMLDOMElement
Dim city As MSXML2.IXMLDOMElement
Dim county As MSXML2.IXMLDOMElement
Dim zipCode As MSXML2.IXMLDOMElement

Set country = xD.SelectSingleNode("/reversegeocode/addressparts/country")
Set state = xD.SelectSingleNode("/reversegeocode/addressparts/state")
Set city = xD.SelectSingleNode("/reversegeocode/addressparts/city")
Set county = xD.SelectSingleNode("/reversegeocode/addressparts/county")
Set zipCode = xD.SelectSingleNode("/reversegeocode/addressparts/postcode")

' Build the result string in usual address manner
ReverseGeocode = country.Text & ", " & state.Text & ", " & city.Text & ", " & _
county.Text & ", " & zipCode.Text
End If
Exit Function
ErrorHandler:
' Handle runtime errors
ReverseGeocode = "Error: " & Err.Description
End Function

Hope this helped. Let us know your further queries.
Regards,
Aung
Team ExcelDemy