Book Image

OpenLayers 2.10 Beginner's Guide

Book Image

OpenLayers 2.10 Beginner's Guide

Overview of this book

Table of Contents (18 chapters)
OpenLayers 2.10
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for Action – creating a Vector Layer


In this first example, we'll use the code similar to what we just saw above—making use of all three classes to build our vector layer, which will request data from a URL.

There are two caveats to this example. The first is that it assumes you have a file called ex5_data.json in the same directory as your map file. You can find it in this book's source code files at http://vasir.net/openlayers_book/files/.

Secondly, unless you are running this on a server, it won't work. It will need a proxy host, which we'll cover right after this example. If you do not have access to a server, you can view the example in action at the URL above.

  1. Start a new map file using the base template from Chapter 1. We'll use the Protocol, Format, and Strategy classes. So first, let's create objects for each of them. Let's start with the format object, as the protocol object will use it. We'll use the GeoJSON format.

    //Create a Format object 
    var vector_format = new OpenLayers...