Skip to content
Tutorial : how to use OpenCage data API with OpenLayers
HTML JavaScript CSS
Branch: master
Clone or download
Latest commit 42e2f21 Oct 19, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Oct 19, 2019 Update Oct 19, 2019
index.html creation Oct 19, 2019
main.js adds a prompts for API key (use localstorage) Oct 19, 2019

Tutorial : how to use OpenCage data API with OpenLayers

Create the map

Here is bootstrap example from OpenLayers:

<!doctype html>
<html lang="en">
    <link rel="stylesheet" href="" type="text/css">
        #map {
            width: 100%;
            height: 100%;
            overflow: hidden;
        #map {
            position: absolute;
            z-index: 1;
            top: 0;
            bottom: 0;
    <script src=""></script>
    <title>Opencage Data with OpenLayers</title>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
        view: new ol.View({
          center: ol.proj.fromLonLat([7.7491, 46.0207]),
          zoom: 4

Add the geocoding widget to the OL map

To use the OL plugin, load the plugin's CSS and JavaScript files from jsDelivr:

<link href="" rel="stylesheet">
<script src=""></script>

Instantiate the geocoder with some options (ie. OpenCage API Key):

var geocoder = new Geocoder('nominatim', {
  provider: 'opencage', // <----- PROVIDER is opencage
  lang: 'en',
  placeholder: 'Search with OpenCage for ...',
  limit: 5,
  debug: false,
  autoComplete: false,
  keepOpen: true,

and add the Control to the map


Add a nice popup plugin

<link rel="stylesheet" href="" type="text/css" />
<script src=""></script>

Display chosen address

Do something when an address is chosen:

geocoder.on('addresschosen', function(evt) {;, evt.address.formatted);



Licensed under the MIT License

A copy of the license is available in the repository's LICENSE file.

You can’t perform that action at this time.