This is a tutorial for using the
OpenCage geocoding API
in MATLAB.
Alexander Dentler has created two MATLAB functions,
one for forward geocoding
OpenCageForward,
and one for reverse
OpenCageReverse.
Please find the code for each
in the opencage-matlab repository on GitHub.
Background
The code examples below will use your geocoding API key once you
log in.
Before we dive in to the tutorial
- Sign up for an OpenCage geocoding API key.
- Play with the demo page, so that you see the actual response the API returns.
- Browse the API reference, so you understand the optional parameters, best practices, possible response codes, and the rate limiting on free trial accounts.
Example
%% INIT
clear
clc
close all
%% Parameters
key='YOUR-API-KEY';
%% Forward search
q='Prinsengracht 263-267, 1016 GV Amsterdam, Netherlands';
% plain search with one result
[results]=OpenCageForward(key,q);
% first result gives us longitude and latitude of the road we are
% looking for, second result is just Amsterdam
disp(results.results(1).formatted)
disp('is a')
disp(results.results(1).components.x_type)
disp(results.results(2).formatted)
disp('is a')
disp(results.results(2).components.x_type)
% Let us say we are interested in the geo info of London, and let us
% allow for up to the maximum number of results: 100
q='London';
[results]=OpenCageForward(key,q,'limit','100');
% we get 50 results. Say we want to restrict our search to the UK. We set
[results]=OpenCageForward(key,q,'limit','100','countrycode','uk');
% we get 3 results
disp(results.results(1).formatted)
disp('is a')
disp(results.results(1).components.x_type)
disp(results.results(2).formatted)
disp('is a')
disp(results.results(2).components.x_type)
disp(results.results(3).formatted)
disp('is a')
disp(results.results(3).components.x_type)
%% Reverse search
q='0 0';
[results]=OpenCageReverse(key,'0 0');
2,500 geocoding API requests/day - No credit card required