Xbasic

geocode_address Function

Syntax

dim result as P = geocode_address(C address [,C method [,C apiKey]])

Arguments

addressCharacter

The location to geocode in the format of a street address.

methodCharacter

Default = "Google". The web service to use for address geocoding. The geocode_address() function supports the following web services:

Method
Description
Google

Uses the Google Maps API. See https://developers.google.com/maps/documentation/geocoding/start#auth for more information about authentication, API keys, and quotas.

Nominatim

Uses OpenStreetMap data to encode the location. See http://nominatim.openstreetmap.org/ for more information.

OpenCage

Uses the OpenCage geocoding service. Requires an OpenCage API key. See https://opencagedata.com/ for more information.

apiKeyCharacter

Default = "". If the method used is "Google", this parameter is used to specify your Google API Key.

Returns

resultPointer

Returns an object with the following properties:

error_codeNumeric

The error code, if any error occurred. If no errors occurred, error_code will be zero.

error_textCharacter

If an error occurred, a message with information about the error. If no error occurred, error_text will be a blank string.

latCharacter

If the address was successfully geocoded, the latitude of the location. The lat property will not be available if geocode_address() fails.

lonCharacter

If the address was successfully geocoded, the longitude of the location. The lon property will not be available if geocode_address() fails.

zipcodeCharacter

If the address was successfully geocoded using the OpenCage method, the postal zip code of the location.

cityCharacter

If the address was successfully geocoded using the OpenCage method, the normalized city name.

stateCharacter

If the address was successfully geocoded using the OpenCage method, the state code.

what3wordsCharacter

If the address was successfully geocoded using the OpenCage method, the what3words address.

confidenceNumeric

If the address was successfully geocoded using the OpenCage method, the confidence score of the match.

Description

Geocode an address. Returns .lat and .lon. Also returns .error_code and .error_text. Address must be entered with commas between each part. e.g. 123 Main St., Boston, MA, 02116, USA. Method can be 'Nominatim' or 'Google'. Default is 'Google'

Example

dim location as p
location = geocode_address("1 Bellevue Ave, Newport, RI 02840")
? location
= error_code = 0
error_text = ""
lat = "41.4875748"
lon = "-71.3103195"

? location.lat
= "41.4875748"

? location.lon
= "-71.3103195"


? geocode_address("1 Unicorn Way, Providence, RI", "Nominatim")
= error_code = -1
error_text = "Invalid data in address."

OpenCage Geocoding Service

You can also use the OpenCage service to geocode addresses using geocode_address(). OpenCage may be a less expensive alternative to the Google geocoding service.

Example: OpenCage

Example:

key = "your OpenCage API key"
address = "36 Bigelow street, Cambridge MA 02139 USA"
dim p as p
p = geocode_address(address,"opencage",key)

See Also