This VBA module provides functions to interact with the Google Geocoding API. It can perform both addresses to latitude / longitude conversion (geocoding) and latitude / longitude to address conversion (reverse geocoding). The code supports various types of API plans, including free, Enterprise, and Premium.
Please note : The Google Maps Platform Premium Plan is no longer available for sign-up or new customers since November 1, 2018.
Released as a standalone from the [FineDustMonitorWithGPS] project.
Performance improvements (up to 2× as faster than before) in the ADDRGEOCODE function.
Now supports Unicode using Microsoft ActiveX Data Objects Library in the ADDRGEOCODE function.
Added GEOSage sample files.
GEOSage sample includes Excel files that use demonstation data using Google Maps Geocoding API Key. The API key used in this project for geocoding and reverse geocoding feature is not provided for your use. The mock data demonstrates all functions with static result values as Google Maps geocoding API Key and VBA Add-in code are not included in the GEOSage sample.
Added GEOSage.bas file to support directly import from Microsoft Excel.
- Obtain a Google Maps API key from Google Developers Console.
- Replace the placeholder
[Your Google Maps API Key]
in the code with your actual API key. - For Enterprise Geocoder, also provide your client ID by replacing the placeholder
[Your Google Maps ClientID]
.
- gstrGeocodingDomain : The domain for the Google Geocoding API.
- gstrGeocodingURL : The endpoint for geocoding requests.
- gintType : The type of API plan to use. Set to 0 for free, 1 for Enterprise, and 2 for Premium.
- gstrKey : Your Google Maps API key.
- gstrClientID : Your Google Maps client ID for Enterprise Geocoder.
- Enable [Developer] tab in Microsoft Excel.
- Within the [Visual Basic] IDE, add "Microsoft XML, v6.0", "Microsoft ActiveX Data Objects 6.1 Library" both as References. Could be found within [Tools] - [References].
Converts an address to latitude and longitude.
Parameters:
address
: The address to be geocoded.
Returns:
- A string containing the latitude and longitude, separated by a comma, or an error message if not found.
Encodes a string for use in a URL.
Parameters:
StringVal
: The string to be encoded.SpaceAsPlus
: Optional boolean to encode spaces as plus signs (+
) instead of%20
.
Returns:
- The URL-encoded string.
Converts latitude and longitude to an address.
Parameters:
lat
: The latitude.lng
: The longitude.
Returns:
- The address corresponding to the given latitude and longitude, or an error message if not found.
Generates a Base64-encoded HMAC-SHA1 hash.
Parameters:
strTextToHash
: The text to be hashed.strSharedSecretKey
: The shared secret key for hashing.
Returns:
- The Base64-encoded HMAC-SHA1 hash.
Decodes a Base64-encoded string to a byte array.
Parameters:
strData
: The Base64-encoded string.
Returns:
- The decoded byte array.
Encodes a byte array to a Base64-encoded string.
Parameters:
arrData
: The byte array to be encoded.
Returns:
- The Base64-encoded string.
- Microsoft XML, v6.0 (MSXML2.DOMDocument60, MSXML2.XMLHTTP60)
- ADODB.Stream for UTF-8 encoding
Takes in the address of the location we want to geocode and returns the first latitude, longitude pair from GEOSage.
Takes in a latitude, longitude pair and returns the first formatted address from GEOSage.
Sub ExampleUsage()
Dim address As String
Dim latlng As String
Dim lat As String
Dim lng As String
Dim addressFromLatLng As String
address = "B92 GARAGE, REDMOND, WA"
latlng = ADDRGEOCODE(address)
Debug.Print "Geocoded Address: " & latlng
lat = "37.4219999"
lng = "-122.0840575"
addressFromLatLng = REVSGEOCODE(lat, lng)
Debug.Print "Reverse Geocoded Address: " & addressFromLatLng
End Sub
- Functionality for ArcGIS, Bing Maps, Data Science Toolkit etc.
- This code includes a sleep function to avoid exceeding the rate limit for the API calls. Adjust the sleep duration as necessary.
- The gintType constant determines which type of API plan to use. Make sure to set it according to your API plan.
Copyright ⓒ HappyBono 2020 - 2025. All rights Reserved.
This project is licensed under the MIT License. See the LICENSE
file for details.