Openlayers Cookbook

Source Code Examples for the OpenLayers Cookbook

OpenLayers Cookbook

This is the source code that accompanies the book OpenLayers Cookbook from Packt Publishing.

You can view it online at: (

Author: Antonio Santiago

Version 1.1


Version 1.1

  • Use of Dojo Toolkit from Google CDN (<script src="//"></script>)
  • Updated Dojo version to 1.8
  • Updated OpenLayers version to 2.12
  • Source code updated to use Dojo 1.8 syntax (using HTML5 data attribute) instead old one (non standar dojoType).
  • Source code highlighted via CodeMirror project.
  • Use of relative paths to load data files (attached to the project).

Version 1.0

This is the original version available at Packt Publishing site.

How to run the examples ?


To run the code samples of the book you need HTTP server with PHP support. PHP is only required for those recipes that load content via AJAX (like WFS), so you can run many of them on a server without PHP support.


Copy the project folder within your HTTP server and open the 'index.html' file, for example, accessing: http://localhost:8080/openlayers-cookbook/index.html.

We have prepared an application with a main menu on top, where user can choose and run the desired recipe.

Each recipe will be opened in a new tab, where user will see two subtabs, first with the recipe results and the second with the recipe code.

How it works ?

When the application starts, by accessing to the index.html the chapters.json file is read.

The chapters.json file contains the name of chapters, recipes and references to each recipe applications, for example:

    "name": "Chapter01 - Mapping Basics",
    "recipes": [
        {"name": "Creating simple map", "url": "recipes/ch01/ch01_simple_map.html"},
        {"name": "Base and non-base layers", "url": "recipes/ch01/ch01_base_non_base.html"},
        {"name": "Avoiding base layers", "url": "recipes/ch01/ch01_avoid_baselayer.html"},
        {"name": "Playing with map options", "url": "recipes/ch01/ch01_playing_map_options.html"},
        {"name": "Managing map's stack layers", "url": "recipes/ch01/ch01_managing_layers.html"},
        {"name": "Managing controls", "url": "recipes/ch01/ch01_managing_controls.html"},
        {"name": "Moving around the map view", "url": "recipes/ch01/ch01_moving_around.html"},
        {"name": "Restricting the map extent", "url": "recipes/ch01/ch01_restricting_view.html"},
        {"name": "Tweening", "url": "recipes/ch01/ch01_tweening.html"}

Content of the project folder

The summary of the project folder is:

  • \css - CSS files for the user interface.

  • \defs - Projection definitions from the Proj4js project required on some recipes of the book.

  • \js - Contains code for the libraries the project depends on.

  • \js\OpenLayers-2.12 - OpenLayers library version 2.11. The book samples has coded with this version, so here is the same version. Maybe some things can be changes or deprecated in future version, so it is better to have the exact version here.

  • \js\proj4js-1.1.0 - Proj4js project code. OpenLayers depends on this project for some operations.

  • \js\codemirror-2.34 - CodeMirror project code. The source code is highlighted using it with the eclipse theme.

  • \nbproject - NetBeans custom files. The project was coded using NetBeans IDE. This folder is helpful for those user that wants to edit book samples on NetBeans.

  • \recipes - The set of recipes ordered by chapter number.

  • \recipes\data - Some data files (images, JSON, etc) used on the recipes.

  • \utils - Contains PHP programs used on some recipes.

  • \utils\proxy.php - A proxy PHP script from MapBuilder project. This is required on some recipes.

  • \utils\points.php - A PHP script that generates random points and returns them in JSON format.


Creative Commons License
OpenLayers Cookbook Examples by Antonio Santiago is licensed under a Creative Commons Attribution 3.0 Unported License.
Based on a work at acanimal/Openlayers-Cookbook.

Popular Cookbook Projects
Popular Openlayers Projects
Popular Learning Resources Categories

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.