OpenLayers 2.10 初学者指南


OpenLayers 2.10 Beginner's Guide Create, optimize, and deploy stunning cross-browser web maps with the OpenLayers JavaScript web-mapping library Erik Hazzard BIRMINGHAM - MUMBAI This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 OpenLayers 2.10 Beginner's Guide Copyright © 2011 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: March 2011 Production Reference: 1110311 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849514-12-5 www.packtpub.com Cover Image by Jose Argudo (josemanises@gmail.com) This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Credits Author Erik Hazzard Reviewers Xurxo Méndez Pérez Alan Palazzolo Ian Turton Couzic Mikael Acquisition Editor Usha Iyer Development Editor Maitreya Bhakal Technical Editors Pallavi Kachare Indexers Hemangini Bari Rekha Nair Editorial Team Leader Aanchal Kumar Project Team Leader Priya Mukherji Project Coordinator Jovita Pinto Proofreader Steve Maguire Graphics Nilesh Mohite Production Coordinator Adline Swetha Jesuthas Cover Work Adline Swetha Jesuthas This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 About the Author Erik Hazzard is a web developer—designer, Open Source advocate, and VI user. He loves to learn, teach, and occasionally blogs on his website at http://vasir.net/. As a professional web developer of five years, Erik specializes in Python and JavaScript, using open source software whenever possible. When he's not developing web applications, he's often developing or designing video games. He works at FREAC (Florida Resources and Environmental Analysis Center), a great place with great people that does all kinds of GIS and web development work. I'd like to thank the developers of OpenLayers, who continually do a fantastic job of developing the best web-mapping framework. I'd like to also thank my friends and mentors Ian Johnson and David Arthur for giving me the confidence and support I needed to get into web development. I'd like to thank Georgianna Strode and Stephen Hodge for their guidance, advice, and providing me with the opportunity to become a better web developer. I could not have written this book without the help of the great team at Packt; I hope every author can be as lucky as me to have such an excellent group of people to work with. I'd like to thank my parents for their never ending support. Lastly, I'd like to thank my love, Alisen, for her understanding and taking the time to help me make sure that the book is as easy to read as possible. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 About the Reviewers Xurxo Méndez Pérez was born in 1983 in Ourense, a little town in the south of Galicia, Spain. He lived there until he started the study for a degree in IT in the University of A Coruña, which finalized in 2008. For the last two years he has been working, at the Computer Architecture Group of the University of A Coruña developing GIS applications (making intensive use of many OGC standards) like Sitegal and SIUXFor (web GIS based applications to manage land properties and promote their good uses in the Galician region), MeteoSIX (a GIS system that provides access to geolocated observed and forecasted meteorological data in Galicia) and others. He also has large experience (3+ years) as a developer of mobile applications, having played first with JavaME, but nowadays he specializes in Google Android, with more than a dozen developed applications, some of them combining concepts like GIS and geolocation, real time responsiveness, and multiuser needs. Alan Palazzolo has been building web applications big and small for over five years, most of which have been with the open source, content management system Drupal, and along the way has picked up some experience in data visualization and mapping. He is a strong believer and advocate for the open source methodology in software and in life. He was involved in starting a Free Geek chapter in the Twin Cities, and constantly tries to use technology, and specifically the Internet, to enhance the lives of those that are less fortunate than most. Ian Turton is a geography researcher at the Pennsylvania State University. He became a geographer by accident nearly 20 years ago and hasn't managed to escape yet. During that period he was a co-founder of the GeoTools open source Java toolkit that is now used as the basis of many geographic open source projects. He continues to serve on the Project Steering Committee for the project as well as committing new code and patches. He has also taught the very popular course "Open Web Mapping" using open standards and open source programs at the Pennsylvania State University and the University of Leeds. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? ‹‹ Fully searchable across every book published by Packt ‹‹ Copy and paste, print and bookmark content ‹‹ On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents Preface 1 Chapter 1: Getting Started with OpenLayers 7 What is OpenLayers? 8 Why use OpenLayers? 8 What, technically, is OpenLayers? 8 Client side 8 Library 9 Anatomy of a web-mapping application 9 Web map client 10 Web map server 10 Relation to Google / Yahoo! / and other mapping APIs 11 Layers in OpenLayers 11 What is a Layer? 12 The OpenLayers website 12 Time for action – downloading OpenLayers 13 Making our first map 15 Time for action – creating your first map 15 How the code works 17 Understanding the code—Line by line 18 JavaScript object notation 21 Behind the scenes—Object Oriented Programming (OOP) 24 Interaction happens with objects 25 MadLibs 25 Time for Action – play MadLibs 25 Programming with OOP 26 Subclasses 26 Now what? 27 API docs 28 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ ii ] Where to go for help 28 This book's website 28 Mailing lists 28 IRC 29 OpenLayers source code repository 29 Summary 30 Chapter 2: Squashing Bugs With Firebug 31 What is Firebug? 32 Setting up Firebug 32 Time for Action – downloading Firebug 32 Firebug controls 34 Panels 34 Console panel 35 HTML panel 35 CSS panel 37 Script panel 37 DOM panel 38 Net panel 38 Panel conclusion 41 Using the Console panel 42 Time for Action – executing code in the Console 42 Time for Action – creating object literals 43 Object literals 44 Time for Action – interacting with a map 45 API documentation 47 Summary 47 Chapter 3: The 'Layers' in OpenLayers 49 What's a layer? 50 Layers in OpenLayers 50 Base layer 51 Overlay layers 51 Time for Action – creating a map with multiple layers 51 Creating layer objects 54 Layer.WMS class 55 WMS layer parameters: 55 Parameters versus arguments 57 Time for Action – configuring the options parameter 58 Configuring layer options 61 wms_state_lines layer options 61 Scale dependency 61 wms_layer_labels layer options 62 The visibility property 62 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ iii ] The opacity property 62 Map tiles 62 Many images make up a map 63 Available layer properties 65 Data types 66 OpenLayers.Layer class properties 66 Modifying layer properties 71 The OpenLayers.Layer class 71 Subclasses 71 Layer Class—Sub and super classes 72 Other layer types 72 Layer.ArcGIS93Rest 72 Layer.ArcIMS 73 Layer.Google 73 Time for Action – creating a Google Maps layer 73 Layer.Grid 75 Layer.Image 76 Time for Action – using the image layer 76 Image layer parameters 77 Layer.MapGuide 78 Layer.TileCache 79 Layer.Vector 79 Layer.VirtualEarth 79 Layer.WFS 80 Layer.WMS 80 Layer.Yahoo 80 Accessing layer objects 80 Time for Action – accessing map.layers 80 Time for Action – accessing layer objects in Firebug 82 Accessing layer properties 82 map.layers 82 Storing references to layer objects 83 Layer class methods 85 Time for Action – defining a global layer object variable 85 Layer class method definitions 86 Summary 88 Chapter 4: Wrapping Our Heads Around Projections 89 Map projections 90 Why on earth are Projections used? 90 Projection characteristics 90 Area 90 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ iv ] Scale 91 Shape 91 Other characteristics 91 Types of projections 92 EPSG codes 92 Time for Action – using different projection codes 93 Specifying a different projection 94 Longitude/Latitude 95 Latitude 96 Longitude 96 Time for Action – determining LonLat coordinates 96 OpenLayers projection class 97 Creating a projection object 97 Parameters 97 Functions 98 Transforming projections 99 Time for Action – coordinate transforms 99 The Proj4js library 100 Time for Action – setting up Proj4js.org 101 Defining custom projections 102 Summary 102 Chapter 5: Interacting with Third Party APIs 103 Third party mapping APIs 103 Map mashups 104 OpenLayers and third party APIs 104 Google Maps 104 Differences between Google Maps version 2 and version 3 105 Time for Action – using Goole Maps V3 (standard way) 105 Creating a Google Map layer object 108 Google layer properties 108 sphericalMercator {Boolean} 109 type {GmapType} 109 V2 GMapType values 110 Time for Action – creating a Google Map layer with V2 (Deprecated) 111 Yahoo! Maps API 113 Time for Action – using the Yahoo! Maps Layer 113 Yahoo! Maps Layer class properties 115 Yahoo! Maps Layer types 115 Microsoft's mapping API 115 Time for Action – creating a Bing/Virtual Earth Layer 115 VirtualEarth layer class properties 117 Possible type values 118 OpenStreetMap 118 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ v ] Time for Action – creating an OpenStreetMap Layer 118 Accessing your own OSM tiles 119 Spherical Mercator 120 Spherical Mercator—EPSG code 120 Time for Action – using Spherical Mercator 121 Map properties with Spherical Mercator layers 122 maxExtent 122 maxResolution 122 units 123 projection 123 Using Google Maps and other layers 123 Time For Action – creating your first mashup 124 WMS with Spherical Mercator/third party map layers 127 Summary 127 Chapter 6: Taking Control of Controls 129 What are controls? 130 Using controls in OpenLayers 130 Adding controls to your map 130 Time for Action – creating a map with no controls 131 Time for Action—Adding controls to a map 132 Adding controls by passing in an array of controls 135 Adding controls to map with addControl() and addControls() 135 Removing controls 136 OpenLayers.Control class 136 OpenLayers.Control properties 137 OpenLayers.Control functions 137 OpenLayers.Control subclasses 138 OpenLayers.Control.ArgParser 138 OpenLayers.Control.Permalink 139 OpenLayers.Control.Attribution 139 Attribution properties 139 Time for Action – using attributions 140 OpenLayers.Control.EditingToolbar 141 OpenLayers.Control.Graticule 141 Graticule properties 142 OpenLayers.Control.KeyboardDefaults 143 KeyboardDefaults properties 143 OpenLayers.Control.LayerSwitcher 143 LayerSwitcher properties 143 LayerSwitcher functions 144 OpenLayers.Control.MousePosition 144 MousePosition properties 144 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ vi ] OpenLayers.Control.Navigation 145 Navigation properties 145 OpenLayers.Control.NavigationHistory 146 NavigationHistory properties 146 NavigationHistory functions 146 Time for Action – using the NavigationHistory control 146 OpenLayers.Control.NavToolbar 147 OpenLayers.Control.OverviewMap 147 OverviewMap properties 148 OverviewMap functions 150 OpenLayers.Control.PanPanel 150 PanPanel properties 151 OpenLayers.Control.PanZoom 151 OpenLayers.Control.PanZoomBar 151 PanZoomBar properties 151 OpenLayers.Control.Scale 151 Scale properties 152 OpenLayers.Control.ScaleLine 152 ScaleLine properties 152 OpenLayers.Control.ZoomPanel 153 Panels 153 Control types 153 Time for Action – using Panels 154 OpenLayers.Control.Panel 157 Panel properties 158 Panel functions 159 Now what? 159 Creating our own controls 159 OpenLayers.Control.Button 159 Button properties 160 Button functions 161 Creating a custom button 161 Time for Action – creating a simple button 161 Other control types 165 Process for creating other button control types 165 Events 165 Event listeners and handlers 165 Custom events 166 Creating a TYPE_TOGGLE control 166 Time for Action – creating a custom TYPE_TOGGLE control 166 Summary 170 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ vii ] Chapter 7: Styling Controls 171 What is CSS? 172 Ideas behind CSS and HTML 172 Editing CSS 172 HTML elements 173 HTML—IDs and classes 173 Styling HTML elements with CSS 174 Using CSS in your code 175 Time for Action – using external CSS files 176 Cascading Style Sheets—Inheritance 178 Order of inheritance 178 Referencing elements 179 OpenLayers and CSS 180 Styling OpenLayers—using themes 180 Creating your own themes 181 OpenLayers—class names and IDs 181 Time for Action – styling controls 182 Time for Action – styling the LayerSwitcher control 186 Other resources 188 Summary 189 Chapter 8: Charting the Map Class 191 The Map class 192 Creating a map object 192 Map class properties 192 Map properties 193 allOverlayers 193 controls 193 displayProjection 194 div 194 Time for Action – using the allOverlays Map property 194 eventListeners 196 fallThrough 197 layers 197 maxExtent 198 minExtent 198 restrictedExtent 198 numZoomLevels 198 Time for Action – setting zoom levels and maxExtent 199 Map properties—Continued 200 Resolutions 200 Time for Action – using resolutions array 201 Map/Layer property inheritance 201 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ viii ] Map properties discussion—Continued 202 maxResolution 202 minResolution 202 Time for Action – using Min and Max resolution 202 scales 204 maxScale 204 minScale 204 Time for Action – Using scales 205 panMethod 206 panDuration 207 Time for Action – working with Pan animations 207 projection 208 theme 208 tileSize 208 unit 208 Map functions 209 Control related 209 Time for Action – using control methods 210 Extent/Coordinate/Bounds related 210 Methods 211 Time for Action – using coordinate related functions 213 Layer related functions 214 Other functions 214 Doing stuff with events 215 Map event types 216 Using map events 216 Using the eventListeners property 217 Time for Action – using eventListeners 217 Using map.events.register 218 Event object 218 Time for Action – working with Map events 219 Multiple maps 220 Using multiple map objects 220 Time for Action – using multiple map objects 221 Multiple maps and custom events 223 Time for Action – creating a multiple map and custom event application 223 Summary 225 Chapter 9: Using Vector Layers 227 What is the Vector Layer? 228 What makes the Vector Layer special? 229 The Vector Layer is client side 229 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ ix ] Other uses 229 What is a 'Vector'? 230 Time for Action – creating a Vector Layer 230 How the Vector Layer works 232 How the Vector Layer is rendered 233 SVG 233 Canvas 233 VML 233 'Renderers' array 233 Time for Action – changing the Renderers array 234 Vector Layer class 235 OpenLayers.Layer.Vector properties 235 OpenLayers.Layer.Vector methods 237 Working with features 237 Time for Action – adding features 237 Vector Layer methods (Continued) 239 Time for Action – destroying features 239 Vector Layer methods (Continued) 241 Time For Action – working with feature events 242 Vector Layer class events 243 Vector Layer event types 244 Time For Action – using Vector Layer events 246 Time For Actions – working with more events 247 Geometry and Feature classes 248 Geometry class 249 Geometry subclasses—Theory 249 Geometry class methods 250 Time for Action – using Geometry class methods 251 Geometry subclasses 253 Geometry subclass methods 255 Feature class 255 How the Feature class works 255 Feature subclasses 256 Feature functions 256 Instantiating a feature object 256 Interacting with Features using Control.SelectFeature 257 Time For Action – using the SelectFeature control 257 Control.SelectFeature class 262 SelectFeature control properties 262 SelectFeature control methods 264 The Vector class, part two 264 Format, protocol, and strategy classes 265 Who invited these classes over? 265 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ x ] Brief overview of the three classes 266 How these three classes interact 267 Example instantiation 267 Time for Action – creating a Vector Layer 268 Cross server requests 269 Using the Vector Layer without a Protocol class 270 Time for Action – using the Format and Strategy classes alone 270 Format class 273 Format class properties 273 Format class methods 273 Format subclasses 274 Strategy class 274 Strategy.BBOX 274 Strategy.Cluster 274 Strategy.Filter 275 Strategy.Fixed 275 Strategy.Paging 275 Strategy.Refresh 275 Strategy.Save 276 Summary 276 Chapter 10: Vector Layer Style Guide 277 Styling the Vector Layer 277 Applying styles 278 What are symbolizers? 278 Time For Action – applying some basic Styling 279 The StyleMap class 281 What is an 'intent'? 281 The Style class 281 Symbolizer properties 282 List of common symbolizer properties 282 Time for Action – common style examples 284 Remaining symbolizer properties 286 Attribute replacement 287 Time For Action – working with attribute replacement 287 Rules and filters 289 How do we follow rules? 289 Using addUniqueValueRules 290 Calling the addUniqueValueRules function 290 The intent parameter 290 The property parameter 290 The symbolizer_lookup parameter 291 The context parameter 291 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ xi ] Time For Action – using addUniqueValueRules 291 Rules and filters 294 How do they work? 295 How do we use them? 295 Time for Action – using rules and filters 295 OpenLayers.Rule class 298 OpenLayers.Filter class 299 Filter Subclasses 299 Filter.Comparison 299 Filter.FeatureId 303 Feature.Logical 303 Time For Action – figuring out logical filters 304 Feature.Spatial 308 Summary 309 Chapter 11: Making Web Map Apps 311 Development strategies 311 Creating a web map application using Flickr 312 Note on APIs 312 Accessing the Flickr public data feeds 312 How we'll do it 313 Time For Action – getting Flickr data 313 Why did we do this? 314 Reducing possible errors 314 Time for Action – adding data to your map 315 Time for Action – extract style 317 Turning our example into an application 318 Adding interactivity 319 Selecting features 319 Time for Action – adding some interactivity 319 Using real time data with a ProxyHost 325 Time for Action – getting dynamic data 325 Wrapping up the application 326 Recap 326 The plan 327 Changing the URL 327 Time For Action – adding dynamic tags to your map 327 Deploying an application 330 Building the OpenLayers Library file 330 Always try to serve small files 330 Using the OpenLayers build file 331 Configuring the build script 331 Time for Action – building a Config file 332 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Table of Contents [ xii ] Running the build script 333 Time for Action – running the Build script 333 Summary 334 Appendix: Pop Quiz Answers 335 Chapter 2: Squashing Bugs With Firebug 335 Chapter 3: The 'Layers' in OpenLayers 335 Chapter 4: Wrapping Our Heads Around Projections 335 Chapter 6: Taking Control of Controls 335 Chapter 7: Styling Controls 336 Chapter 8: Charting the Map Class 336 Chapter 9: Using Vector Layers 336 Chapter 10: Vector Layer Style Guide 336 Chapter 11: Making Web Map Apps 336 Index 337 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Preface Web mapping is the process of designing, implementing, generating, and delivering maps on the World Wide Web and its products. OpenLayers is a powerful, community driven, open source, pure JavaScript web-mapping library. With it, you can easily create your own web map mashup using WMS, Google Maps, and a myriad of other map backends. Interested in knowing more about OpenLayers? This book is going to help you learn OpenLayers from scratch. OpenLayers 2.10 Beginner's Guide will walk you through the OpenLayers library in the easiest and most efficient way possible. The core components of OpenLayers are covered in detail, with examples, structured so that you can easily refer back to them later. The book starts off by introducing you to the OpenLayers library and ends with developing and deploying a full-fledged web map application, guiding you through every step of the way. Throughout the book, you'll learn about each component of the OpenLayers library. You'll work with backend services like WMS, third-party APIs like Google Maps, and even create maps from static images. You'll load data from KML and GeoJSON files, create interactive vector layers, and customize the behavior and appearance of your maps. There is a growing trend in mixing location data with web applications. OpenLayers 2.10 Beginner's Guide will show you how to create powerful web maps using the best web mapping library around. This book will guide you to develop powerful web maps with ease using the open source JavaScript library OpenLayers. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Preface [ 2 ] What you need for this book The only thing you'll need for this book is a computer and text editor. Your operating system will come with a text editor, and any will do, but if you are using Windows I recommend using Notepad++ (http://notepad-plus-plus.org/), VI if you are using Linux, and Textmate if on OSX. An Internet connection will be required to view the maps, and you'll also need a modern web browser such as Firefox, Google Chrome, Safari, or Opera. While a modern browser is required to get the most of the library, OpenLayers even provides support for non standards based browsers such as Internet Explorer (even IE6, to some extent). No knowledge of Geographic Information Systems (GIS) is required, nor is extensive JavaScript experience. A basic understanding of JavaScript syntax and HTML / CSS will greatly aid in understanding the material, but is not required. What this book covers Chapter 1: Getting Started with OpenLayers. This chapter will introduce OpenLayers and some programming concepts behind it. It covers how to create a map, walking through how to set up the code and configure various settings. Chapter 2: Squashing Bugs with Firebug. This chapter will cover setting up the Firebug plugin, which we'll use throughout the book, so that we can do simple debugging and better understand how OpenLayers works behind the scenes. Chapter 3: The 'Layers' in OpenLayers. Here, we'll cover one of the core classes of OpenLayers—the Layer class. We'll discuss what a 'Layer' is, how to work with layers and the different layer classes. Chapter 4: Wrapping our Heads Around Projections. This chapter will cover a few basic geography concepts and why understanding them will help us use OpenLayers. We'll also cover projections, why they are used, and how to use them. Chapter 5: Interacting With Third Party APIs. This chapter will focus on creating an OpenLayers map using different third party APIs, such as Google Maps and OpenStreetMaps. Chapter 6: Taking Control of Controls. We'll cover another core class of OpenLayers, the Control class. We'll cover what controls are and discuss the various types of controls, along with how to work with the events. Chapter 7: Giving Controls Some Style. This chapter will walk through how OpenLayers uses CSS to style controls. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Preface [ 3 ] Chapter 8: Charting the Map Class. This chapter will discuss another core component of OpenLayers—the Map class. We'll learn about how to map functions and their properties, along with how to set up multiple maps on the same page. Chapter 9: Using Vector Layers. Here, we'll learn what a Vector layer is and how it works. We'll also cover how to work with the data, such as KML files. Chapter 10: Vector Layer Style Guide. In this chapter we'll cover how to style the vector layer and how to use the Rule and Filter classes. Chapter 11: Creating Web Map Applications. This final chapter will go over how to build a web-mapping application from scratch, and how to use the OpenLayers build file. Who this book is for This book is for anyone who has any interest in using maps on their website, from hobbyists to professional web developers. OpenLayers provides a powerful, but easy-to-use, pure JavaScript and HTML (no third-party plug-ins involved) toolkit to quickly make cross- browser web maps. A basic understanding of JavaScript will be helpful, but there is no prior knowledge required to use this book. If you've never worked with maps before, this book will introduce you to some common mapping topics and gently guide you through the OpenLayers library. If you're an experienced application developer, this book will also serve as a reference to the core components of OpenLayers. How to read this book This book is primarily designed to be read from start to finish, with chapters building on each other and increasing in complexity. At the same time, however, the chapters are modular so that each can also serve as reference once you've learned the material. This book should preferably be read straight through first, of course, and then serve as a reference later. Conventions In this book, you will find several headings appearing frequently. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Preface [ 4 ] To give clear instructions of how to complete a procedure or task, we use: Time for action – heading 1. Action 1 2. Action 2 3. Action 3 Instructions often need some extra explanation so that they make sense, so they are followed with: What just happened? This heading explains the working of tasks or instructions that you have just completed. You will also find some other learning aids in the book, including: Pop quiz – heading These are short questions intended to help you test your own understanding. Have a go hero – heading These set practical challenges and give you ideas for experimenting with what you have learned. You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning. Code words in text are shown as follows: "You can download it as either a tar.gz or .zip." A block of code is set as follows: Lines [21] to [23] if(!map.getCenter()){ map.zoomToMaxExtent(); } When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold: var wms_layer = new OpenLayers.Layer.WMS( 'WMS Layer Title', 'http://vmap0.tiles.osgeo.org/wms/vmap0', This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Preface [ 5 ] {layers: 'basic'}, {} ); New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "By default, your map adds an argParser control which will try to pull information from a permalink.". Warnings or important notes appear in a box like this. Reader feedback Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of. To send us general feedback, simply send an e-mail to feedback@packtpub.com, and mention the book title via the subject of your message. If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail suggest@packtpub.com. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors. Customer support Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase. Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Preface [ 6 ] Errata Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code— we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www. packtpub.com/support. Piracy Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at copyright@packtpub.com with a link to the suspected pirated material. We appreciate your help in protecting our authors, and our ability to bring you valuable content. Questions You can contact us at questions@packtpub.com if you are having a problem with any aspect of the book, and we will do our best to address it. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 1Getting Started with OpenLayers Within the past few years, the popularity of interactive web maps has exploded. In the past, creating interactive maps was reserved for large companies or experts with lots of money. But now, with the advent of free services like Google and Yahoo! Maps, online mapping is easily accessible to everyone. Today, with the right tools, anyone can easily create a web map with little or even no knowledge of geography, cartography, or programming. Web maps are expected to be fast, accurate, and easy to use. Since they are online, they are expected to be accessible from anywhere on nearly any platform. There are only a few tools that fulfill all these expectations. OpenLayers is one such tool. It's free, open source, and very powerful. Providing both novice developers and seasoned GIS professionals with a robust library, OpenLayers makes it easy to create modern, fast, and interactive web-mapping applications. In this chapter we will ‹‹ Learn what OpenLayers is ‹‹ Discuss some web mapping application concepts ‹‹ Make our First Map ‹‹ Cover concepts behind OpenLayers, such as Object Oriented Programming ‹‹ Provide information on resources outside of this book This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 8 ] What is OpenLayers? OpenLayers is an open source, client side JavaScript library for making interactive web maps, viewable in nearly any web browser. Since it is a client side library, it requires no special server side software or settings—you can use it without even downloading anything! Originally developed by Metacarta, as a response, in part, to Google Maps, it has grown into a mature, popular framework with many passionate developers and a very helpful community. Why use OpenLayers? OpenLayers makes creating powerful web-mapping applications easy and fun. It is very powerful but also easy to use—you don't even need to be a programmer to make a great map with it. It's open source, free, and has a strong community behind it. So if you want to dig into the internal code, or even improve it, you're encouraged to do so. Cross browser compatibility is handled for you—it even works in IE6. OpenLayers is not tied to any proprietary technology or company, so you don't have to worry so much about your application breaking (unless you break it). At the time of writing, support for modern mobile and touch devices is in the works (with many proof of concept examples), and should be in the official library in the near future—if they aren't by the time you're reading this. OpenLayers allows you to build entire mapping applications from the ground up, with the ability to customize every aspect of your map—layers, controls, events, etc. You can use a multitude of different map server backends together, including a powerful vector layer. It makes creating map 'mashups' extremely easy. What, technically, is OpenLayers? We said OpenLayers is a client side JavaScript library, but what does this mean? Client side When we say client side we are referring to the user's computer, specifically their web browser. The only thing you need to have to make OpenLayers work is the OpenLayers code itself and a web browser. You can either download it and use it on your computer locally, or download nothing and simply link to the JavaScript file served on the site that hosts the OpenLayers project (http://openlayers.org). OpenLayers works on nearly all browsers and can be served by any web server or your own computer. Using a modern, standard-based browser such as Firefox, Google Chrome, Safari, or Opera is recommended. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 9 ] Library When we say library we mean that OpenLayers is an API (Application Programmer Interface) that provides you with tools to develop your own web maps. Instead of building a mapping application from scratch, you can use OpenLayers for the mapping part, which is maintained and developed by a bunch of brilliant people. For example, if you wanted to write a blog you could either write your own blog engine, or use an existing one such as WordPress or Blogger and build on top of it. Similarly, if you wanted to create a web map, you could write your own from scratch, or use software that has been developed and tested by a group of developers with a strong community behind it. By choosing to use OpenLayers, you do have to learn how to use the library (or else you wouldn't be reading this book), but the benefits greatly outweigh the costs. You get to use a rich, highly tested and maintained code base, and all you have to do is learn how to use it. Hopefully, this book will help you with that. OpenLayers is written in JavaScript, but don't fret if you don't know it very well. All you really need is some knowledge of the basic syntax, and we'll try to keep things as clear as possible in the code examples. If you are unfamiliar with JavaScript, Mozilla provides phenomenal JavaScript documentation at https://developer.mozilla. org/en/javascript. Anatomy of a web-mapping application First off—what is a 'web-mapping application'? To put it bluntly, it's some type of Internet application that makes use of a map. This could be a site that displays the latest geo-tagged images from Flickr (we'll do this in Chapter 11), a map that shows markers of locations you've traveled to, or an application that tracks invasive plant species and displays them. If it contains a map and it does something, you could argue that it is a web map application. The term can be used in a pretty broad sense. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 10 ] So where exactly does OpenLayers fit in? We know OpenLayers is a client side mapping library, but what does that mean? Let's take a look at the following screenshot: This is called the Client / Server Model and it is, essentially, the core of how all web applications operate. In the case of a web map application, some sort of map client (e.g., OpenLayers) communicates with some sort of web map server (e.g., a WMS server or the Google Maps backend). Web map client OpenLayers lives on the client side. One of the primary tasks the client performs is to get map images from a map server. Essentially, the client has to ask a map server for what you want to look at. Every time you navigate or zoom around on the map, the client has to make new requests to the server—because you're asking to look at something different. OpenLayers handles this all for you, and it is happening via asynchronous JavaScript (AJAX) calls to a map server. To reiterate—the basic concept is that OpenLayers sends requests to a map server for map images every time you interact with the map, then OpenLayers pieces together all the returned map images so it looks like one big, seamless map. In Chapter 2, we'll cover this concept in more depth. Web map server A map server (or map service) provides the map itself. There are a myriad of different map server backends. A small sample includes WMS, Google Maps, Yahoo! Maps, ESRI ArcGIS, WFS, and OpenStreet Maps. If you are unfamiliar with those terms, don't sweat it. The basic principle behind all those services is that they allow you to specify the area of the map you want to look at (by sending a request), and then the map servers send back a response containing the map image. With OpenLayers, you can choose to use as many different backends in any sort of combination as you'd like. OpenLayers is not a web map server; it only consumes data from them. So, you will need to be able to access some type of web map service. Don't worry though. Fortunately, there are a myriad of free and/or open source web map servers available that are remotely hosted or easy to set up yourself, such as MapServer. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 11 ] Throughout this book, we'll often use a freely available web mapping service from OSGeo, so don't worry about having to provide your own. With many web map servers you do not have to do anything to use them—just supplying a URL to them in OpenLayers is enough. OSGeo, OpenStreet Maps, Google, Yahoo!, and Bing Maps, for instance, provide access to their map servers (although, some commercial restrictions may apply with various services in some situations). Relation to Google / Yahoo! / and other mapping APIs The Google, Yahoo!, Bing, and ESRI Mappings API allow you to connect with their map server backend. Their APIs also usually provide a client side interface (at least in the case of Google Maps). The Google Maps API, for instance, is fairly powerful. You have the ability to add markers, plot routes, and use KML data (things you can also do in OpenLayers)—but the main drawback is that your mapping application relies totally on Google. The map client and map server are provided by a third party. This is not inherently a bad thing, and for many projects, Google Maps and the like are a good fit. However, there are quite a few drawbacks. ‹‹ You're not in control of the backend ‹‹ You can't really customize the map server backend, and it can change at any time ‹‹ There may be some commercial restrictions, or some costs involved ‹‹ These other APIs also cannot provide you with anything near the amount of flexibility and customization that an open source mapping application framework (i.e., OpenLayers) offers Layers in OpenLayers So, what's with the Layer in OpenLayers? Well, OpenLayers allows you to have multiple different 'backend' servers that your map can use. To access a web map server, you create a layer object and add it to your map with OpenLayers. For instance, if you wanted to have a Google Maps and a WMS service displayed on your map, you would use OpenLayers to create a GoogleMaps layer object and a WMS layer object, and then add them to your OpenLayers map. We'll soon see an example with a WMS layer, so don't worry if you're a little confused. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 12 ] What is a Layer? Like layers of an onion, each layer is above and will cover up the previous one; the order that you add in the layers is important. With OpenLayers, you can arbitrarily set the overall transparency of any layer, so you are easily able to control how much layers cover each other up, and dynamically change the layer order at any time. For instance, you could have a Google map as your base layer, a layer with satellite imagery that is semi-transparent, and a vector layer all active on your map at once. A vector layer is a powerful layer that lets us add markers and various geometric objects to our maps—we'll cover it in Chapter 9. Thus, in this example, your map would have three separate layers. We'll go into much more depth about layers and how to use and combine them in Chapter 3. The OpenLayers website The website for OpenLayers is located at http://openlayers.org/. To begin, we need to download a copy of OpenLayers (or, we can directly link to the library—but we'll download a local copy). You can download the compressed library as either a .tar.gz or .zip, but both contain the same files. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 13 ] Let's go over the links: ‹‹ Link to the hosted version: If you do not want to actually download OpenLayers, you can instead link to the OpenLayers library by adding this script URL to your site in a 7. 27. 28. 29. 30.
31.
32. 33. 3. Open up index.html in your web browser. You should see something similar to: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 17 ] What just happened? We just created our first map using OpenLayers! If it did not work for you for some reason, try double checking the code and making sure all the commas and parentheses are in place. You can also refer to the Preface where a link to code samples used in the book is given. By default, we're given a few controls if we don't specify any. We will use the file we created as a template for many examples throughout the book, so save a copy of it so you can easily reference it later. The control on the left side (the navigation buttons) is called the PanZoom control. You can click the buttons to navigate around the map, drag the map with your mouse/use the scroll wheel to zoom in, or use your keyboard's arrow keys. We'll cover controls in far greater detail in Chapter 6. How the code works Now, let's take a look at the code—line by line. Before we do that, let's include a quick reference to the line numbers at which the requirements from the previous section occur at. These are the core things that you need to do to have a functioning map. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 18 ] We'll denote line numbers with brackets—[x], where x is the line number. 1. Including the OpenLayers library files: Line [6] 2. Creating an HTML element for our map: Lines [30] and [31]
3. Creating a map object from the Map class: Line [12] map = new OpenLayers.Map('map_element', { }); 4. Creating a layer object from a Layer class: Lines [13] to [18] var wms_layer = new OpenLayers.Layer.WMS( 'WMS Layer Title', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {} ); 5. Adding the layer to the map: Line [20] map.addLayer(wms_layer); 6. Defining the map's extent: Lines [21] to [23] if(!map.getCenter()){ map.zoomToMaxExtent(); } Understanding the code—Line by line Lines [1] to [5]: Sets up the HTML page. Every HTML page needs an and tag, and the extraneous code you see specifies various settings that inform your browser that this is an HTML5 compliant page. For example, we include the DOCTYPE declaration in line [1] to specify that the page conforms to standards set by the WC3. We also specify a tag, which contains the title that will be displayed on the page. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 19 ] This is the structure that all our code examples will follow, so this basic code template will be implicitly assumed in all examples that follow throughout the book. Line [6]: <script type='text/javascript' src='OpenLayers.js'></script> This includes the OpenLayers library. The location of the file is specified by the src='OpenLayers.js' attribute. Here, we're using a relative path. As the index. html page is in the same folder as the OpenLayers.js file, we don't have to worry about specifying the path to it. The file could be either on your computer or another computer—it doesn't matter much, as long as the browser can load it. We can also use an absolute path, which means we pass in a URL that the script is located at. OpenLayers.org hosts the script file as well; we could use the following line of code to link to the library file directly: <script type='text/javascirpt' src='http://openlayers.org/api/ OpenLayers.js'></script> Notice how the src specifies an actual URL—this is how we use absolute paths. Either way works, however, throughout the book we'll assume that you are using a relative path and have the OpenLayers library on your own computer/server. If you use the hosted OpenLayers library, you cannot be sure that it will always be available, and it may change overnight (and changes when the library is updated)—so using a local copy is recommended. Line [7]: Starts a <script> block. We'll set up all our code inside it to create our map. Since the OpenLayers library has been included in line [5], we are able to use all the classes and functions the library contains. Line [8]: var map; Here we create a global variable called map. In JavaScript, anytime we create a variable we need to place var in front of it to ensure that we don't run into scope issues (what functions can access which variables). When accessing a variable, you do not need to put var in front of it. Since we are defining map as a variable at the global level (outside of any functions), we can access it anywhere in our code. Soon we will make this map variable our map object, but right now it is just an empty global variable. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 20 ] Line [11]: Creates a function called init. When the page loads (via body onload='init();' on line [29]), this function will get called. This function contains all of our code to set up our OpenLayers map. If you are familiar with JavaScript, you do not have to put all the code in a function call—you could, for instance, just put the code at the bottom of the page and avoid a function call all together. Creating a function that gets called when the page loads is a common practice and so we will be doing it throughout the book. Line [12]: map = new OpenLayers.Map('map_element', { }); Remember that global map variable? Well, now we're making it a map object, created from the OpenLayers.Map class. It is also referred to as an instance of the Map class. We'll talk about what this means later in this chapter in the Object Oriented Programming section. The map object is the crux of our OpenLayers application— we call its functions to tell the map to zoom to areas, fire off events, keep track of layers, etc. Now, let's look at the right hand side of the equal sign (=): new means that we are creating a new object from the class that follows it. OpenLayers.Map is the class name which we are creating an object from. Notice that something is inside the parenthesis: ('map_element', {}). This means we are passing two things into the class (called arguments, and you pass them in separated by a comma). Every class in OpenLayers expects different arguments to be passed into it, and some classes don't expect anything. The Map class expects two parameters. The first argument, map_element, is the ID of the HTML element that the map will appear in. The second argument, { }, are the map options, consisting of key:value pairs (e.g., {key:value} ). This is also called JavaScript Object Notation, a way to create objects on the fly. We'll cover this in more depth very shortly in the next section. Also, you are not required to include this argument if it is empty (even though we just did it), but we are just doing it here for consistency. Because we passed in map_element as the first parameter, we will have an HTML element (almost always a <div>) with the ID of map_element. The HTML element ID can be anything, but for the sake of clarity and to avoid confusion, we call it map_element. Line [13]: var wms = new OpenLayers.Layer.WMS( Here, we create a layer object for the map to use from the WMS subclass of the Layer class. In OpenLayers, every map needs to have at least one layer. The layer points to the 'back end', or the server side map server, as we discussed earlier. The layer can be any of a multitude of different services, but we are using WMS here. WMS, which stands for Web Map Service, is an international standard defined by the Open Geospatial Consortium (OGC). The arguments we can pass in for layers are dependent on the layer class—we cover layers in detail in Chapter 3. If you don't want to wait, you can also check out the documentation at http://dev.openlayers.org/docs/files/OpenLayers/Layer-js.html to see what arguments different layers of classes expect. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 21 ] Notice we don't include everything on one line when creating our layer object—this improves readability, making it easier to see what we pass in. The only difference is that we are also adding a new line after the commas which separate arguments, which doesn't affect the code (but does make it easier to read). Line [14]: 'WMS Layer Title', This is the first parameter passed in; the layer's title. Most layer classes expect the first parameter passed in to be the title of the layer. This title can be anything you would like, the main purpose of it is for human readability—it is displayed in controls such as the layer list. Line [15]: 'http://vmap0.tiles.osgeo.org/wms/vmap0', The URL is the second parameter that the WMS layer class expects to receive. For now, we're using a publicly available WMS service from OSGeo. We will cover in depth the WMS in Chapter 3. For now, all you need to know is that this is the base URL, which the layer will be using. Line [16]: {layers: 'basic'}, The third parameter is an anonymous object containing the layer properties (similar in format to the previous options object on line [12]), and is specific to the WMS layer class. These are the things that are actually added (more or less) straight into the GET call to the map server backend when OpenLayers makes requests for the map images. JavaScript object notation In OpenLayers, we pass in anonymous objects to classes a lot. In JavaScript, anonymous objects are comma separated key:value pairs, and are set up in the format of {key1:value1, key2:value2}. They are, basically, objects that are created without deriving from a class. This format is also referred to as JavaScript Object Notation. When we say key1:value1, it's similar to saying "key1 = value1", but we use a colon instead of an equals sign. We can also create an anonymous object and pass it in instead of creating it on the line, for example: var layer_parameters = {layers: 'basic'}; var wms = new OpenLayers.Layer.WMS('layer_title', 'url', layer_parameters, …); With a WMS layer, we need to pass in, at a minimum, a layers key. In this case it has the value of 'basic'. This layer parameter specifies layers that exist on the map server. So, when you ask the WMS server from a map image with the layer 'basic', it sends you back an image that is composed of that layer. You can also ask for multiple layers from the map server. In this case, we only want the WMS service to give us back an image that contains a layer called 'basic'. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 22 ] Let's get back to the code. Line [17]: { } The fourth parameter is an optional options object, an anonymous object in the format we just discussed. These properties are generally shared by every OpenLayers Layer class. For instance, regardless of the Layer type (e.g., WMS or Google Layer), you can pass in an opacity setting (e.g., {opacity: .8} for 80 percent opacity). So, regardless of whether you are working with a WMS or a Vector layer, this opacity property can apply to either layer. Since this is the last thing passed into the Layer object creation call, make sure there is not a leading trailing comma. Trailing commas are a common error and are often tedious to debug. This options object is optional, but we will often use it, so it's a good habit to keep our code consistent and provide an empty object (by {}), even if we aren't passing anything into it yet. Line [18]: ); This simply finalizes the object creation call. Line [20]: map.addLayer(wms); Now that we have a wms_layer object created, we need to add it to the map object. Notice we are calling a function of the map object. There are actually a few ways to go about adding a layer to a map object. We can use the above code (by calling map.addLayer), where we pass in an individual layer, or we could use map.addLayers: map.addLayers( [layer1, layer2, ...] ); Here, we pass an array of layers. Both methods are equally valid, but it may be easier to pass in an array when you have multiple layers. You can also create the layer objects before you create the map object and pass the layer objects into the map when you create it, for instance: map = new OpenLayers.Map('map_element', {layers: [layer1, layer2, …]}); All ways are valid, but we will usually use addLayer or addLayers throughout the book. Line [21] - [23]: if(!map.getCenter()){ map.zoomToMaxExtent(); } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 23 ] Finally, we must specify the map's viewable area. Here, the actual code that moves the map is map.zoomToMaxExtent(), which zooms the map to the map's maximum extent. It is inside an if statement. This if statement checks to see whether the map already has a center point. The reason why we add in this check is because, by default, your map can accept a specially formatted URL that can contain an extent and layers to turn on/off. This is, in more common terms, referred to as a permalink. If we did not check to see if a center has already been set, permalinks would not work. By default, your map adds an argParser control which will try to pull information from a permalink. We cover this in Chapter 6, but to see it in action now you can simply add the following to your URL, which will zoom the map to the same coordinate and zoom level: ?zoom=4&lat=56&lon=-116 So, your URL might look like c:/code/index. html?zoom=4&lat=56&lon=-116 There are a few ways to set the map's extent. If you know you want to show everything, the map.zoomToMaxExtent() function is a quick and good way to do it. There are other ways as well, such as map.zoomToExtent(new OpenLayers.Bounds([minx,miny,maxx,maxy]); There are even more ways though. If you know a specific location you want the map to start at, this is another way to do it: map.setCenter(new OpenLayers.LonLat(x,y)); map.zoomTo(5); Where x,y are the Lon/Lat values, and 5 is the zoom level you wish to zoom to. By default, your map will have 16 zoom levels, which can be configured by setting the numZoomLevels property when creating your map object. More ways exist, but these are the most common strategies. The basic idea is that you need to specify a center location and zoom level—setting the extent accomplishes this, as does explicitly setting the center and zoom level. Line [24]: } This simply finishes the init() function. Lines [26], [27]: These lines close the script tag and head tag. Line [29]: <body onload='init();'> This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 24 ] This starts the body tag. When the page is finished loading, via the onload='init();' attribute in the body tag, it will call the JavaScript init() function. We have to wait until the page loads to do this because we cannot use the map div (or any HTML element) until the page has been loaded. Another way to do this would be to put the init() call in a JavaScript tag at the bottom of the page (which would not be called until the page loads), but both methods accomplish the same thing. When browsers load a page, they load it from top to bottom. To use any DOM (Document Object Model) elements (any HTML element on your page) in JavaScript, they first have to be loaded by the browser. So, you cannot reference HTML with JavaScript before the browser sees the element. It'd be similar to trying to access a variable that hasn't yet been created. Even though we have JavaScript code that references the map_element div at the top of the page, it is not actually executed until the page is loaded (hence the need for the onload and init() function call). Line [30] and [31]: <div id='map_element' style='width: 500px; height: 500px'></div> To make an OpenLayers map, we need an HTML element where the map will be displayed in. Almost always this element will be a div. You can give it whatever ID you would like, and the ID of this HTML element is passed into the call to create the map object. You can style the div however you would like—setting the width and height to be 100 percent, for instance, if you wanted a full page map. It would be best to style the elements using CSS, but styling the div in line like this works as well. Lines [32] and [33]: These lines finalize the page by closing the remaining tags. Behind the scenes—Object Oriented Programming (OOP) Now, let's talk about how this stuff works from a more theoretical and technical level. OpenLayers employs Object Oriented Programming (OOP) techniques, meaning that to use with the library, we create objects from built in classes that OpenLayers provides. What does this mean? You already are familiar with what classes and objects are, but you just may not know it. Think of the concepts of a class and object in terms of the parts of speech. For example, think of what the abstract idea of a noun means—a person, place, thing, or idea. Noun itself (the abstract idea) is a class. But the actual, concrete words that qualify as a noun are objects. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 25 ] Interaction happens with objects Ironman and Batman are two separate words (or, in terms of OOP, two objects), but they belong to the same noun class. A class is primarily used to generate objects; we interact with objects. Consider these two sentences: "Noun was in a fight with noun." "Ironman was in a fight with Batman." Now, the second sentence is more of an actual sentence. We use the words Ironman and Batman in the sentence (which are objects; they are instances of the noun class). We don't use noun (a class) in the sentence, because interacting with a class like this doesn't make much sense. This is one of the important concepts of OOP—we interact with objects, and we generate objects through classes. MadLibs Madlibs, for those unfamiliar with it, is a game where you are given text with some missing words. The point is to come up with words to fill in the blanks, but each blank can only contain a certain type of word (noun, adjective, verb, and so on). The type of word is a class and the actual word you insert is an object. Time for Action – play MadLibs Finish this sentence, by replacing verb and noun with verb and noun 'objects': I verb up to the noun. It's about 7 or 8 o'clock. I looked at my noun. I was there, to verb on my throne as prince of noun(place). What Just Happened? You just did a Madlibs, demonstrating some of the concepts of OOP. As you can see, it doesn't make much sense to read the sentence as "I verb up to the noun". Since verb and noun are classes, we don't use the actual term verb or noun in the sentence. So, the idea would be to generate separate objects, one of the class noun and one of the class verb. For example, the previous sentence could be completed like: "I pulled up to the house". Pulled and house are objects that are instances of the verb and noun classes, respectively. We use words that belong to those classes, which are objects. Another term used when referring to objects is instance, which is used to designate the class the object is derived from, for example, Frodo (a person / hobbit) is an instance of a noun. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 26 ] Programming with OOP The same concept applies to programming. The only thing we can really do with a class is to create an object from it. Objects are derived from classes—you can interact and do things with objects, but not with classes. So, in OpenLayers, we need to create objects from the built in classes to be able to really do anything. The main thing we need are map and layers objects. If we want to create an OpenLayers map, we need a map object, and we create it in the following manner: var map = new OpenLayers.Map( … ); The new declaration means that we want to create a new object from the OpenLayers.Map class. The ellipsis (...) in the parenthesis presents things we pass into the class to create our object, called arguments. Each class expects different arguments to be passed into it. It is similar to the Madlibs example—the noun class accepts only certain words. If we were to create a word object in JavaScript, the code may look something like this: var my_word_object = new Noun('Ironman'); Here, we pass in 'Ironman' to the noun class, and now we have our word object. Because the Noun class only accepts nouns, this would work fine. If we try to pass in a verb, for instance, it would not work because the noun class cannot accept things that are verbs. Similarly, the Map class expects different arguments to be passed into it than the Layer class does. Subclasses There are also subclasses, which are classes derived from another class and inherit all the attributes from the 'base' class it inherits from. Subclasses can also override properties and methods that they inherit. For instance, let's say we have a Dog class that is a subclass of the Animal class. The Dog class would inherit all the attributes of the base Animal class—such as, perhaps, a speak method. Now, the Dog class would override the speak method and it would bark (or 'yap' annoyingly) when called. The Dog class might also provide additional methods that weren't in the base Animal class, such as, perhaps, a wag_tail method. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 27 ] 'Base' class and 'Subclasses' are both classes; the terminology just helps to clear up what class inherits from what other class. There are many subclasses in OpenLayers, for example, the GoogleMap Layer class is a subclass of the base Layer class, and the Navigation control class is a subclass of the base Control class. Subclasses are still classes, and the exact same concept applies; we still need to generate objects from the class to use it. The previous section was just an introduction to OOP. While you don't necessarily need to know a whole lot more about OOP concepts to use this book, a great resource to learn more about the concepts can be found at http://en.wikipedia.org/wiki/Object- oriented_programming. Classes are easy to spot in OpenLayers code. By convention, in OpenLayers (and many other places) class names are CamelCased, which means the first letter of each word is capitalized, while objects are not. For example, MyClass would be an example of a class name, while my_object would be an example of an object. Now what? Our coverage of the sample code was not meant to be extremely thorough; just enough to give you an idea how it works. We'll be covering OOP concepts in more detail throughout the chapters, so if anything is bit unclear, don't worry too much. As OpenLayers is a library and provides functions for you, it is important to know what those functions are and what they do. There are many places to do this, but the best source is the API docs. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 28 ] API docs The API documentation is always up to date and contains an exhaustive description of all the classes in OpenLayers. It is usually the best first place to go when you have a question. You can access the documentation at http://dev.openlayers.org/docs/files/ OpenLayers-js.html. It is continually updated and contains a wealth of information. We will constantly refer to it throughout the book, so keep the link handy! Sometimes, however, the API docs may not seem clear enough, but there are plenty of other resources out there to help you. Where to go for help Books are great, but they're basically just a one way form of communication. If you have any questions that the book does not answer, Google is the best first place to go. Mailing lists and IRC are other great resources. Sometimes it's hard to formulate the right question, but there is help! This book's website The extension website for this book can be found at http://vasir.net/openlayers_ book. Current, up to date corrections and code fixes, along with more advanced tutorials and explanations, can be found there—and also on my blog at http://vasir.net/blog. You can also grab the code and more information about this book at Packt Publishing's website for the book, located at https://www.packtpub.com/openlayers-2-1- javascript-web-mapping-library-beginners-guide/book. Mailing lists The OpenLayers mailing list is an invaluable resource that lets you not only post questions, but also browse questions others have asked (and answered). There are two main OpenLayers news groups—Users and Dev. The Users list is where the majority of questions are asked. Dev is reserved for development of the OpenLayers library itself. If you have questions about how to use OpenLayers, they belong in the Users list, not the Dev list. You can subscribe to the mailing list at http://lists.osgeo.org/mailman/listinfo/ openlayers-users. There are various ways to browse the content, and I prefer to use Nabble. You can view the lists at http://osgeo-org.1803224.n2.nabble.com/OpenLayers-f1822462.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 1 [ 29 ] Please do a thorough search before posting questions, as it is likely that a question similar to yours has already been asked and solved. If you have a question about using OpenLayers, please use the User list. Please do not post questions to the Dev list, unless it has to do strictly with development of the OpenLayers library itself. When posting a question, please be as thorough as possible, stating your problem, what you have done, and the relevant source code (e.g. "I have a problem with using a WMS layer. I have tried this and that, and here is what my source code looks like..."). A good guideline for asking questions in a way that will best elicit a response can be found at http://www. catb.org/~esr/faqs/smart-questions.html. IRC Internet Relay Chat (IRC) is another great place to go if you have questions about OpenLayers. IRC is used for group communication; a big chat room, essentially. If you have exhausted Google and the mailing list, IRC provides you with real time with other people interested in OpenLayers. Generally, the people who hang out in the OpenLayers chat room are very friendly, but please try to find an answer before asking in IRC. The server is irc.freenode.net and the chat room is #openlayers. You can download an IRC client online; a good Windows one is mIRC (http://mirc.com). More information about how to use IRC can be found at http://www.mirc.com/install.html. OpenLayers source code repository Traditionally, OpenLayers has used SVN as its revision management system. At the time of writing, however, the source code repository location is hosted at GitHub. You can access the entire code repository at http://github.com/openlayers/openlayers. Feel free to download a copy and play around with it yourself. There is a process to actually get your code permanently added to the official source code base, but it cannot hurt to download a copy of the code base and look around it yourself to figure out how it's really working! This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Getting Started with OpenLayers [ 30 ] Summary In this chapter we were introduced to OpenLayers and learned a bit about it. We saw what Web Map Applications are and how they work. After that, we created our first map with OpenLayers, then analyzed how the code works. Then we covered a fundamental concept, Object Oriented Programming, which we'll need to know about while really working with OpenLayers. Lastly, resources for help and information outside this book were provided. Now that we have a basic handle on OpenLayers, we'll jump straight into debugging OpenLayers. By doing so, we'll cover more thoroughly many of the concepts that we've discussed, such as how OpenLayers requests and receives map images, and how it puts them together. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 2Squashing Bugs With Firebug OpenLayers is, at a fundamental level, not doing anything that is conceptually too hard to grasp. It gets map images from a server, and puts them together. From a technical level, however, there is a lot of work going on, and it might seem magical how it all works together so well. Fortunately, there are many tools to dispel any potential magical thinking we might have and show us how OpenLayers is working behind the scenes. Firebug, a free and open source plugin for Firefox, is one such great tool. Speeding up development time, viewing network communication, and squashing bugs are just a few things that Firebug, and other web development tools, do that make them hard to live without. To really use OpenLayers effectively and to its full potential, we need to understand how it works. In this chapter, we'll try our best to do just that, by using web development tools to examine OpenLayers' inner workings. By doing so, we'll accomplish two things. First, we'll become familiar with these tools which will significantly help us when developing our maps. Secondly, and more importantly for now, we'll gain a better understanding of how OpenLayers works. Throughout this chapter we'll cover: ‹‹ What Firebug and other development tools are ‹‹ Setting up Firebug ‹‹ Each of the Firebug Panels ‹‹ Using the JavaScript Command Line Console panel This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 32 ] What is Firebug? Firebug is a free, open source addon for Firefox. If you do not have Firefox, I recommend downloading it (it is also free and open source). However, other modern and standards based browsers, such as Google's Chrome, Apple's Safari, and Opera, also work well and have great built in developer tools. Firebug, and other web development tools, makes the web development process much easier and quicker. What do I mean by this? With these tools, we can change anything on our site, on the fly, without editing or saving any files. We can type in JavaScript code with a command line interface and execute it immediately. We can view all the requests that our web page sends to servers, along with the server's reply. For example, if our map isn't able to get back map images from the server, we could examine the requests our page is making and find out if we have any typos or haven't set up our map layer properly. Using these tools makes it a lot easier to develop not only an OpenLayers mapping application, but any web application, and makes it easier to fix any bugs we encounter in the process. We'll focus on Firebug in this chapter and refer back to it throughout the book, but other tools such as Google's Chrome and Apple's Safari's built in developer tools work just as well (although some functionality may vary). Setting up Firebug Since Firebug is an extension of Firefox, you'll need to first install Firefox. You can download it for free at http://getfirefox.com. After that, Firebug can be freely downloaded at http://getfirebug.com. When you click on the link to download Firebug, Firefox will prompt you with a message asking if you wish to install the plugin. After installing, all you have to do is restart Firefox and you'll be good to go. Time for Action – downloading Firebug If you do not already have an up to date version of Firefox installed, please do so now. After you have installed Firefox, set up Firebug by following these steps: 1. Go to http://getfirebug.com. 2. Click on the Install Firebug for Firefox button. Once you click this, you should see a message similar to this: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 33 ] 3. Click Install, wait for it to finish installing, and then restart Firefox. 4. Now that Firebug is installed, you should see a Firebug icon on the bottom right side of your screen. What Just Happened? When Firebug is not enabled, the Firebug Icon is gray. When it is in enabled, it has an orange color—this is just a quick way for you to tell if Firebug is enabled or not for the current page you're on. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 34 ] When you click on the Firebug icon (near the bottom right of your browser's window), Firebug will open and you can start using it. But before we start, let's take a look at what Firebug looks like after initially installing it and clicking on the Firebug icon: The top row contains two icons (a Firebug icon and a Page Inspector icon), multiple panels that provide specific functionality (they look and act similar to tabs, but the technical term is "panels"), a search box, and finally minimize, maximize, and close the buttons. Let's go over the items, left to right, one at a time. The position of the icons may change over time as Firebug is updated; but the general functionality should remain (more or less) the same. Firebug controls Firebug icon: The Firebug icon on the top left contains various commands and options related to Firebug when you click on it. Page Inspector icon: This icon, a cursor inside a rectangle, is the HTML Inspector. When you click on it, your mouse cursor will identify HTML elements on the web page. So, when you mouse over anything on a website, the element will be outlined in blue and the HTML panel will open up and show you the element your mouse is over. Panels The next set of controls is called panels; each panel provides a different type of function. The panels act like tabs (the two terms can be used interchangeably), but Firebug refers to them as 'panels' in the documentation. Let's go over each panel, since they are, essentially, what makes up Firebug. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 35 ] Console panel Firebug's Console panel is where we'll spend most of our time. It acts as a powerful JavaScript command line, or interpreter, which means we can type in JavaScript code and execute it right away—no need to save or edit any files. One thing that makes this so useful is that we can interact directly with the DOM (Document Object Model—any HTML element on the webpage), including any existing JavaScript code the page contains. So, this means we're able to interact with our OpenLayers map on the fly, issuing command and testing code to instantly see what works and what doesn't. As you can imagine, this saves a ton of time! By default, the Console panel is disabled. To enable it, click on the arrow near the Console text and select Enabled. HTML panel The HTML panel provides not just a display of the HTML source code, but also the ability to quickly edit any HTML element and its associated style. You can add and remove HTML elements, edit HTML attributes, and change nearly anything about the page without having to save any files. It's great for development. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 36 ] How it works Firebug automatically builds a tree structure from your HTML code, allowing you to expand and hide each HTML tag. It is important to note that the code you see in the HTML panel is generated HTML code—the code in the panel may not be exactly the same as the page's source code. HTML panel contents Here is what the HTML tab looks like when Firebug is opened while viewing a webpage: On the left side, Firebug shows us the HTML of the page. We can right click on any tag and do various things—such as copying the HTML to the clipboard, deleting the element, changing the tag attributes, and more. On the right side, we see the associated style information for the element we have selected. Here, we can modify or add properties and they will instantly appear on the page. In this example we selected a div element with a class of 'left_sidebar_item'. Looking at the CSS on the right side, there is no definition for the 'left_sidebar_item' class (if there was, we would see something called .left_sidebar_item, with a period in front of it to indicate that it is indeed a class). We do, however, see a definition for #left_sidebar, which is a parent div of the currently selected div. If you are unfamiliar with HTML or CSS, the w3schools site is a great resource. For more information on HTML, visit http://www.w3schools.com/ html/default.asp and for CSS visit http://www.w3schools.com/ css/default.asp. What does this mean? Well, Firebug lists all inherited style information, and parent element styles propagate down to all their child elements (each child has all its parent's styles, unless the child overrides a style, which doesn't happen in this example). That's why we also see the body, html definition, and every div will display that, since every div sits inside the <html> and <body> tags of the web page. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 37 ] By double clicking on pretty much anything in the HTML or CSS list you can quickly change values and names. Any change you make will immediately show up on the page, which makes it very easy to change style in real time and see how the page is affected without having to edit and save any files. Play around with it a bit—if you mess anything up, you can just reload the page. When editing pages with Firebug, any changes you make will disappear when you refresh the page. You are not editing the actual web server's files with Firebug—instead, you are editing a copy that is on your computer that only you can see when you make changes to it. So, if you make any changes and want them to be saved, you'll have to edit your actual source code. CSS panel This panel provides similar functionality as the CSS sidebar in the HTML panel we just talked about. It also provides the option to edit any CSS document associated with the page, not just the style of a selected element. We need not talk much about this panel, and for the purposes of this book we won't spend much time here. But if you are a web designer, this is another powerful panel that can greatly speed up your development time. Script panel The Script panel is very powerful. Not only does it allow you to view all the JavaScript code associated with the page, it is a great real-time code debugger. You can set watch expressions, view the stack, set breakpoints, etc. If those terms are foreign, don't worry, we won't be spending much time with this panel. However, before we move on to the next panel I want to quickly talk about enabling breaking on errors. With this option enabled, Firebug will stop the web page whenever a JavaScript error is encountered. This makes it very easy to quickly pinpoint where your page is blowing up at. To enable it, simply click on the pause-button icon. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 38 ] Keep note of when you enable it—I've been frustrated more than once when developing because I forgot that it had been enabled. When it is enabled, the Script text will glow yellow, as demonstrated in the above screenshot. You can also enable/disable it through the Console panel. Unfortunately (depending on your viewpoint), we can't go much more in depth with these tools, as it is outside the scope of this book. But please feel free to play around with them, as Firebug's powerful debugger is a great resource. More information on it can be found at http://getfirebug.com/errors. DOM panel The next panel is the Document Object Model panel, or DOM panel. The DOM is, basically, a representation of HTML elements as objects. The DOM panel automatically sets up a tree structure to represent our HTML page, allowing us to view everything our HTML page contains. We can also see JavaScript variables and their values, as well as functions, objects, and more. When using the DOM panel, attributes and properties are colored black, and functions and methods are colored green. We won't go much more in depth here, but the DOM panel is a very valuable tool, especially when you want to take a peek at JavaScript components. By using the DOM panel (and assuming we are looking at a page that includes OpenLayers), we can quickly see all of OpenLayer's classes, functions, etc. It is not a replacement for the API docs, but serves as a good, quick way to view such information. Net panel Firebug's Net panel is a tool we will often use throughout this book. Basically, it provides a way for us to monitor network activity by viewing all the requests and responses the web page is making. We can see any Asynchronous JavaScript (AJAX) request the page makes. Without AJAX, we would have to refresh our entire page anytime we wanted to do anything with our OpenLayers map. So, for example, every time you zoom in, OpenLayers makes a series of requests to the map server to get new map images, and the map server's response is a new map image that OpenLayers then displays. This request/response method is handled via AJAX—without it, we would have to refresh the entire page after every request. AJAX is a method by which, through JavaScript, you can send a request to a server and receive a response without actually refreshing the page. Traditionally, if you want your web page to get an update from the server, you would need to have the page itself send a request. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 39 ] The Net panel allows us to see the URL that is being requested, the GET or POST parameters, the server's response, the size of the response in KB, and the time it took to complete the request. You may also have to enable the Net panel—it can be enabled in a similar way we enabled the Console panel, by clicking on the arrow next to the Net text on the top tab. Let's take a look at what the Net panel looks like for the example from Chapter 1: Before we talk about the requests being made, take a look at the toolbar above the lists of requests—the one that contains the links Clear, Persist, All, HTML, etc. Clicking on Clear will do what its name implies—clears out the list of requests. Clicking on Persist will cause the list of requests to persist, or not get deleted, on page reloads. The next grouping of links allows us to filter the requests by type. Because we are getting back only images from the map server in this case, all the requests you see would belong to the Images option. If the CSS option was clicked, we would not see those requests, as they are not CSS files. Now, let's break down the actual request list. Request list The request list shows us all the requests the page makes. Each URL in the previous screenshot is a URL that OpenLayers is making a request to. By clicking on the + (plus) sign next to each request, we can get more information about the request, including the full request URL and the response. When we click on the plus sign, we get a box with Params, Headers, Response, and Cache tabs. The Params tab lists all the parameters, or key/value pairs the URL contains. The Response tab provides us with the server's response to our request. For the purpose of this book, we do not need to worry about the other two tabs. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 40 ] Parameters Take a look again at the list—before we mouse over the text in the screenshot, the titles contain GET vmap0?LAYERS. The GET specifies that the request type is GET, which basically means we are embedding variables inside the URL itself with key=value pairs, separated by a & sign. When we mouse over a URL, we can see more of it—as in the previous screenshot, we see a bunch of variables in the format key=value&key=value& . . .. These values correspond to the values listed in the Params tab when you expand the URL. If you take a look at the link we have our mouse over, you'll notice that the URL is the same as the URL from the example in Chapter 1, http://labs.metacarta.com/wms/vmap0. However, there is an additional text after vmap0; a question mark followed by key=value pairs. The question mark signifies the start of the key=value pairs in a GET request. Let's take a look at some of the variables the URL contains. ?LAYERS=basic&SERVICE=wms . . . The LAYERS key should look familiar, as we specified in Chapter 1. In that example, we used the code: {layers: 'basic'}, When we defined the key:value pair, we were essentially telling OpenLayers what to pass into the actual URL it generates. From the generated URL, we can see that it did indeed pass over the right layer name. Now, as we discussed earlier, this layer name, basic, is the layer we want from the WMS service. The layer name we pass in here affects what image the WMS server responds with. In this case, we'll get back an image with the layer named basic. Notice that there are many more parameters (separated by & signs) than what we passed in. OpenLayers automatically adds these parameters for you, so we get back the right image of a map without us having to manually build each URL. To see the entire key:value pairs, or parameters, expand the URL and look at the Params tab. Note that these GET variables, such as LAYERS and SERVICE are specific to WMS. Other layer types will use different values. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 41 ] BBOX parameter One of the important variables to take a look at in the URL is the BBOX, or Bounding Box, parameter. Expand one of the URLs by clicking on the plus sign next to it and take a look in the Params tab. This BBOX number is the extent of that individual map image piece. You'll see a number in the following format: minx, miny, maxx, maxy These four numbers form a rectangle that contains the extent of the map image. A number example might be something like -73.5, 39.3, -67.5, 45. These numbers will depend on the projection your map is in—but don't worry too much about that for now. If you look at the BBOX parameter of other URLs, you'll notice that part (or all) of the BBOX values change for each request. URLs that are grouped together in the request list share some values for their BBOX parameter. It is analogous to a grid; each request returns an individual cell of the grid. (This functionality is not the same for all Layer types in OpenLayers, but this basic concept holds for the majority of Layer types, including the WMS layer in this example.) Pop Quiz– panel 1. What panel would you use if you wanted to execute JavaScript code? a. The Net panel b. The Console panel c. The DOM panel d. The HTML panel Panel conclusion Each panel serves a certain purpose and all of Firebug's panels are extremely useful, but throughout the book we will be mainly focusing on the following panels: ‹‹ Console panel (Command Line JavaScript) ‹‹ HTML panel ‹‹ Net panel These three panels will be used the most throughout the book. We'll occasionally come back to the other panels, but we won't spend a whole lot of time with them. However, before we conclude this chapter, let's get a bit more familiar with the Console panel, since we'll be making heavy use of it in the coming chapters. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 42 ] Using the Console panel We talked a bit about what the console panel is—essentially, a JavaScript command line. We can execute any JavaScript code we want, and interact with any page element. There are two primary components to the Console panel—the console log area and the input area. The console log area will display information about any errors, along with displaying any code that is entered. The input area allows us to either enter a single line of code or, by clicking on the red arrow on the right side of the input box, multiple lines of code. Before we start using the console with our maps, let's get familiar with the console by executing some JavaScript code. Time for Action – executing code in the Console We're going to do some basic JavaScript coding via the Firebug console; specifically, just calling a built in alert() function to display an alert. 1. Open up Firefox. It doesn't matter at this point what website (if any) that you go to, since we will be writing a stand alone code. 2. Open up Firebug by clicking on the Firebug icon. Go to the Console panel. If it is not enabled, enable it. 3. Now, at the bottom of your screen you'll see an area where you can enter code, designated by >>>. Clicking anywhere after that will allow you to enter the code. 4. Type in the following code, and then hit Enter. alert('Narwhals like bacon'); 5. You should see an alert box pop up with the text Narhwals like bacon (or whatever string you passed into the alert function). After the code is executed, it will appear in the log above the input line. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 43 ] What Just Happened? We just executed some JavaScript code without having to edit and save any files. Although we did a simple alert, we are really not limited by what we can do. Anything that we could save in a JavaScript file, we could enter in the Console. You'll also notice that the same code that we typed in appeared in the log area. We'll also get an error message if any errors occur with the code—go ahead and try it! Instead of typing alert('My alert'); type something like fakealert('Boom');. This will give you a reference error, since nowhere is the function fakealert() defined—alert(), on the other hand, is a built in function, so we can call it from any page. That's pretty much to it! The rest just builds on those principles. Let's go ahead and do just one more thing, something only slightly more involved, before jumping into manipulating an OpenLayers page. Time for Action – creating object literals We're going to introduce object literals and get acclimated with how to manipulate them now, so we can better work with OpenLayers code. 1. Open up Firefox and Firebug's Console panel (enabling it if it disabled)—again, it doesn't matter right now what page you're on. 2. Click on the red arrow on the bottom right, above the Firebug icon. This will open up a side panel where we can type in multiple lines of code. The code will not be executed when we press Enter, like in single line mode. Instead, we can execute the code by either pressing Ctrl + Enter or clicking Run. 3. Type in the following code, and then execute it by pressing Ctrl+Enter or clicking Run. var my_parameters = {'answer': 42, 'question': null}; console.log(my_parameters); 4. The above code should display, in the console log area, something similar to Object { answer=42 }. Click on it, and the DOM panel will open, showing you all the information about the object you just created. 5. Click on the Console panel to get back to it. In the input box, add the following code to the existing code and execute it: console.log(my_parameters.answer); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 44 ] 6. You should see a line of output in the console area containing the number 42. What Just Happened? We just created what is called in JavaScript an anonymous object, or object literal. Now, we have discussed how objects are created from classes, and in JavaScript we need to use the new keyword to instantiate an object from a class. But there is no new keyword here! Object literals The key concept here is that we are just creating a single object that does not derive from a class. Since object literals (anonymous objects) do not derive from a class, it is, essentially, an empty object. It contains only what we explicitly define. The value associated with a key can be almost anything—a string, integer, function, array, or even another object literal. We encountered object literals in Chapter 1 when we discussed JavaScript Object Notation—they were the {key:value} pairs used to define the parameters and options of our layer and objects. The only difference is that we did not assign a variable to them; we simply passed them in when we created our layer object. Object literals are extremely useful for a variety of tasks, and it is a great way to package information in an easy to use form. They are in the form of {key:value, key2:value2}. We can access any property of an object literal by using dot notation, in the form of my_ object_literal.key. The key, like before, is the key part of the key:value pair. In the above code, we call console.log(my_parameters.answer); and the value of the key answer is displayed in the console's log area. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 45 ] console.log(): The Firebug function console.log() is a function that will, essentially, display what you pass into it in the console log. You can pass in variables, strings, objects; anything, and it will display in the log. It comes in handy often, so getting familiar with it is a good idea. We will use object literals frequently when making our maps—so if they don't make much sense yet, don't worry. The basic idea to grasp, and the primary way we will use them, is that they are essentially key:value pairs. Before we end this chapter, let's do one quicker example where we interact with an OpenLayers map using the Console panel. Time for Action – interacting with a map We'll use the map we created in Chapter 1 to do this example, interacting with our OpenLayers map by calling various functions of the map. 1. Open up the map from Chapter 1 in Firefox. Enable Firebug and the Console panel. If you would like, you can take a look at the Net panel and view the network activity to see the requests your page is making. 2. Go to the Console panel, input and then execute the following code: console.log(map); 3. You should see the map object information come up in the console log. Click on it, and take a moment to look over the various attributes it has. Near the bottom, you can see a list of all the functions that belong to it (which are also referred to as methods). Take note of the function names, as we'll be using them. 4. Go back to the Console panel, type in and execute the following code: map.zoomIn(); map.getExtent(); 5. Take note of the extent. Clear out the code you typed in, then type in the following and execute it: map.zoomToMaxExtent(); map.getExtent(); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 46 ] 6. Now, let's take a look at some properties of the map object. We can access the map properties using the dot notation, which we discussed previously. Clear any code you've typed so far, input and execute the following code: console.log(map.id); console.log(map.numZoomLevels); 7. Refer back to the functions of the map object (by running console.log(map); then clicking on the output in the log area). Try playing around with different functions and attributes the map object has. To access the functions, you just need to type in map.function();. You can also access the properties of the map by typing map.key, where key would be something like id (so the full code would be map.id). The attributes are black in the DOM panel, and the functions are colored green. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 2 [ 47 ] What Just Happened? We just executed some functions of our map and accessed some properties of it. All we have to do is call our object, map, followed by a period, then a function or property it owns. Using this dot notation (e.g., map.zoomIn();), we can access any property or function of the map object. We also saw how the DOM panel comes in handy, and took a look at functions that we can call, which the map object owns. Any function listed there can be called via map. functionname();, but some functions require parameters to be passed in or they will not work. But where can we go to figure out more information about the functions and what they require? Have a Go Hero – experiment with functions Try to call different functions that you see listed in the DOM tab. Many functions will not work unless you pass certain arguments into them, but don't be afraid of errors! Poke around the various functions and properties and try to interact with them using the Console tab like in the example above. API documentation The API documentation for the Map class, which our map object derives from (and thus, inherits all the functions and properties of the class) provides more detailed explanations of the properties, functions, and what we can do with them. They can be found at http:// dev.openlayers.org/apidocs. Even though Firebug is a great resource to quickly interact with code and learn from it, the API docs present an extra level of information that Firebug cannot necessarily provide. Summary In this chapter, we learned more about how OpenLayers works. We learned how to set up and use Firebug and other Web Development tools. We then took a look at the panels that Firebug provides and what they are used for. Finally, we spent time with the Console panel—something you'll be making extensive use of throughout this book (and when you're developing your own web maps). This chapter aimed to provide some foundational knowledge of web development tools for getting into both OpenLayers and general web development. Web development tools, like Firebug, are one of the biggest assets in our toolkit. They speed up development time, help us identify bugs, interact with our code better, and much more. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Squashing Bugs With Firebug [ 48 ] Firebug and such tools can also degrade performance if you are just browsing the web, so it is probably best to leave them disabled unless you're using them. For the code exercises in the following chapters, it will be very beneficial if you use Firebug to first test the code, to see what it's doing. That way, you immediately know, so to say, where each piece of the puzzle fits in; you know what each line of code actually does, and how it affects the entire project. In the next chapter, we'll really dive into OpenLayers, covering perhaps the most fundamental topic in OpenLayers: the Layer class. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 3The 'Layers' in OpenLayers Maps can contain an overwhelming amount of information, but some maps don't show enough. Figuring out just what information to display on a map is certainly an art form, and creating printed maps with just the right balance of information is quite difficult. Fortunately, creating maps for the web is slightly easier in this respect, because we can let the user determine what information they want to see. Imagine two people looking at a city map—one person just cares about the bus routes, while the other wants to only know about bicycle routes. Instead of creating two maps, we could create a single map with two different layers, one for each route. Then the user can decide if they want to see the bus routes, bicycle routes, both, or none at all. OpenLayers provides us with a variety of layer types to choose from and use. We can do all sorts of things—such as changing layer opacity, turning the layers on or off, changing the layer order, and much more. In this chapter we'll go over what Layers are—both in the abstract and concrete sense (via OpenLayer's Layer class). By the end, you will possess enough expertise to use different types of Layers on your map and interact with them. In this chapter, we will: ‹‹ Learn what layers are ‹‹ Show the difference between base layers and overlay layers ‹‹ Talk about the WMS layer class ‹‹ Learn about the layer class properties ‹‹ Cover other types of layers ‹‹ Discuss layer class functions This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 50 ] What's a layer? A layer is basically a way to show multiple levels of information independent of each other. Layers are not just a mapping or cartography concept; graphic designers and digital artists make heavy use of layers. Imagine a printed-out map of a city. Let's say you also have two sheets of transparent paper. One sheet has blue lines that indicate bus routes, and the other sheet contains green lines that indicate bicycle routes. Now, if you placed the transparent sheet of paper with bicycle routes on top of the map, you would see a map of the city with the bicycle routes outlined. Putting on or taking off these transparent pieces of paper would be equivalent to turning a layer on or off. The order you place the sheets on top of each other also affects what the map will look like—if two lines intersect, you would either see the green line on top or the blue line on top. That's the basic concept of a layer. Layers in OpenLayers OpenLayers is a JavaScript framework, and as discussed earlier is built using Object Oriented Programming. When we want to actually create a layer, we create (or instantiate) an object from an OpenLayers Layer class. OpenLayers has many different Layer classes, each allowing you to connect to a different type of map server 'back end.' For example, if you wanted to connect to a WMS map server, you would use the Layer.WMS class, and if you wanted to use Google Maps you'd use the Layer.Google class. Each layer object is independent of other layer objects, so doing things to one layer won't necessarily affect the other. How many layers can I have? The safest maximum amount of layers you can have on a map at one time depends largely on the user's machine (i.e., their processing power and memory). Too many layers can also overwhelm users; many popular web maps (e.g., Google and Yahoo!) contain just a few layers. If you need to use tons of layers (say, more than fifty), it might be a better idea to create/ destroy them as necessary, as having too many layers may slow down your map on some machines. Whatever the purpose of your web map application is, you will need at least one layer to have a usable map. An OpenLayers map without any layers would be sort of like an atlas without any maps. You need at least one layer—at least one Base layer. All other layers that 'sit above' the base layer are called Overlay layers. These are the two 'types' of layers in OpenLayers. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 51 ] Base layer A base layer is at the very bottom of the layer list, and all other layers are on top of it. This would be our printed out map from the earlier example. The order of the other layers can change, but the base layer is always below the overlay layers. By default, the first layer that you add to your map acts as the base layer. You can, however, change the property of any layer on your map to act as the base layer (by setting the isBaseLayer property to True). You may also have multiple base layers. However, only one base layer can be active at a time. When one base layer is turned on, all the other base layers are turned off. Overlay layers (non base layers), however, do not behave this way—turning on or off overlay layers will not affect other overlay layers. Base layers are similar to radio buttons—only one can be active at a time. Overlay layers are similar to check boxes—you can have as many on or off as you'd like. At the time of writing, there was a discussion to remove the 'base layer' terminology and replace it with 'mutually exclusive layers.' If this is the case when you are reading this, the ideas and concepts work the same as the base layer/overlay concept. Overlay layers Any layer that is not a base layer is called an overlay layer. Like we talked about, the order that you add layers to your map is important. Every time you add a layer to the map, it is placed above the previous one. Throughout the rest of this book, we'll be using the map we created in Chapter 1's example as a sort of template. The only thing that will change is that you will not be using the lines of code that create the WMS layer and add it to the map. You will need to delete those lines of code yourself, or refer to the template from the book's website at http://vasir.net/openlayers_book/. Time for Action – creating a map with multiple layers Let's create a map with two WMS layers. One layer will act as our base layer, and the other will be an overlay layer containing labels for country, state, and city names. 1. Create a copy of the file you made for the last example of Chapter 1 and remove the existing WMS layer code (or, use the template from http://vasir.net/ openlayers_book/). You can name it whatever you'd like, but we'll refer to it as chapter3_ex1_wms_layers.html. Make sure it is in the same directory as your OpenLayers.js file. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 52 ] 1. First we're going to remove everything that was in the init() function. Your function should now look like this: function init() { } 2. Next, inside the init() function, we're going to setup our map object like before: map = new OpenLayers.Map('map_element', {}); 3. Now we're going to create our first layer. We'll use a WMS layer and ask the WMS server for the layer 'basic' (a layer on the WMS service). We'll also explicitly set it to be a base layer. var wms_layer_map = new OpenLayers.Layer.WMS( 'Base layer', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {isBaseLayer: true} ); 4. Let's create a second layer object now. It will also be a WMS layer. This time, we're going to ask for a few different layers from the WMS service—a bunch of labels. We're also going to set the transparent property to true, so the map images which the server sends back will be transparent. We'll also set the opacity to be 50 percent (by setting the opacity to .5). This layer will be an overlay layer. var wms_layer_labels = new OpenLayers.Layer.WMS( 'Location Labels', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'clabel,ctylabel,statelabel', transparent: true}, {opacity: .5} ); 5. Time to add the layers to the map. We'll use the addLayers function and pass in an array of layer objects. map.addLayers([wms_layer_map, wms_layer_labels]); 6. Now, let's add a Layer Switcher control that will show us the layers on the map. map.addControl(new OpenLayers.Control.LayerSwitcher({})); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 53 ] 7. Finally, we need to set the map center information. This last step needs to be repeated for all further examples throughout the book—you need to be sure to include it even if it isn't explicitly asked for. All our maps will need to have their extent set somehow, and this is one standard way to do so (refer to Chapter 1 for more explanation). if(!map.getCenter()){ map.zoomToMaxExtent(); } 8. Save the file, and then open it up in your web browser (preferably Firefox, since we'll be using Firebug). Because we're just working with HTML and JavaScript, you don't need to place this on a server or anything. You can simply open the file with your web browser directly from the folder. You should see something like this: What Just Happened? We just created a map with two WMS layers and a Layer Switcher Control that allows us to turn on and off layers. Controls are what OpenLayers provides that allows us to actually interact with the map and layers. To use controls, we create objects from different Control classes. By default, all maps get a Navigation control object which allows us to pan and zoom the map. In Chapter 6, we'll cover controls in depth. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 54 ] In the previous example, our layer objects were called wms_base_layer and wms_overlay_layer. Throughout this book, this will be the format I will use to name objects—all lowercase with underscores (_), no spaces. You can, of course, use whichever convention you like, as long as your naming scheme is consistent. Creating layer objects The process to work with layers consists of two steps: 1. Create the layer object. 2. Add the layer object to the map. You can use either map.addLayer(layer) to add an individual layer, or map.addLayers([layer1, layer2, ...]) to add an array of layers, like in the previous example. These two steps can actually be combined into one step (by instantiating the layer object when calling the addLayer function—this works, but I don't recommend it as it makes it a little harder to work with the layer object). By now, we have a bit of experience instantiating objects from the WMS Layer class. Let's take a look at the code that creates our wms_base layer object. var wms_layer_map = new OpenLayers.Layer.WMS( 'Base layer', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {isBaseLayer: true} ); Each item inside the parentheses, after OpenLayers.Layer.WMS(, are called arguments which we pass in while creating the object. But how did I know what arguments to pass in? We didn't write the class, so we don't know what it expects to take in. So, as with nearly any third party library, we have to refer to the documentation to see what arguments the class expects. OpenLayers has great documentation, and we'll be using it throughout the book. Since we're using a WMS Layer, let's take a look at the documentation for the WMS class at http://dev.openlayers.org/docs/files/OpenLayers/Layer/WMS-js.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 55 ] Layer.WMS class There's a lot of information in the API docs, but let's look specifically at the section titled Constructor. This will tell us how to create a WMS layer object. The process varies for different types of layer but let's focus on the WMS class for now. Take a look at the Parameters sub section—this specifies what arguments this specific class expects to take in: Parameters Description name {String} A name for the layer. url {String} Base url for the WMS (e.g. http://vmap0.tiles.osgeo.org/ wms/vmap0). params {Object} An object with key/value pairs representing the GetMap query string parameters and parameter values. options {Object} Hashtable of extra options to tag onto the layer. The parameters tell us about the order of arguments to pass in, and what each argument means. The word in between the curly brackets ({ }) refers to the data type of parameter. So, {string} means the parameter should be a string. Let's take a look at the wms_base_layer layer instantiation code and see how we use the four parameters. WMS layer parameters: The four WMS layer parameters are as follows: Name The first parameter is the layer's name and it should be a string. We pass in 'Base Layer'. Notice how we enclose Base Layer in quotes—this is how we signify that it is a string. The title can be anything that you like—if you have a layer switcher control, the title will show up in it. Keep in mind that there is a comma after the closing quote, which means that we're done with this first argument and are ready to proceed to the next one. This name parameter is present in nearly all Layer classes. URL The URL is the second parameter, and it should also be a string. It specifies the URL of the web map server. We pass in 'http://vmap0.tiles.osgeo.org/wms/vmap0'. This URL parameter is present in most Layer classes. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 56 ] Params Params is the third parameter, and it is an anonymous objector consisting of key:value pairs. This parameter specifies server side settings that affect the map image, which the WMS server returns. The key:value pairs you pass in here will be appended (more or less) to the URL that OpenLayers generates when it makes requests to the map server. For example, in the previous example when creating the wms_layer_labels we passed in {layers: 'clabel,ctylabel,statelabel'}. In this case, we are asking the WMS server to give us back a map image with the server side layers called 'clabel,ctylabel,statelabel' turned on. We specify multiple server side layers—it doesn't matter what or how many server side layers we request though, because the WMS Layer object on the client side is still considered by OpenLayers to be a single layer object. This params parameter is present in most Layer classes. Possible params keys and values The possible keys and values for this params object depend on the map server you are working with. Unfortunately, covering them all for all layer types is outside the scope of this book. We'll only be using a few WMS parameters throughout this book—layers (to specify what layers the WMS service should give us), transparent (to ask for transparent images, for things such as label layers), and srs (to specify the projection). For now, at least, our main concerns are just figuring out what layer names are on the WMS server. To figure this out, you can issue a GETCAPABILITIES request in the URL (SERVICE=WMS must also be specified). For instance, to get the possible layers from the WMS service we've been using so far go to the following URL: http://vmap0.tiles.osgeo.org/wms/vmap0?SERVICE=WMS&REQUEST=GETCAPABIL ITIES If you are interested in more information about WMS, the specifications can be found at http://www.opengeospatial.org/standards/wms. Options Options is the last parameter and is an anonymous object that specifies the layer object's settings. You are not required to pass in this parameter. The options object contains properties for the client side OpenLayers Layer object. These are the settings for the layer object itself, so all Layer classes have this parameter. To define various properties of the OpenLayers Layer object, we use this options argument. Properties include settings such as isBaseLayer, opacity, and visibility. Since the layer properties are client side settings, the WMS server (or whichever map server the layer uses) doesn't know about them. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 57 ] The possible values are, basically, anything that you find in the API documentation for either the base Layer class (at http://dev.openlayers.org/docs/files/OpenLayers/ Layer-js.html), or the specific subclass you're working with, e.g., the WMS Layer Class, at http://dev.openlayers.org/docs/files/OpenLayers/Layer/WMS-js.html. You can use this options parameter to initialize any of the layer parameters described in the API—the best way to get more familiar with it is to just play around with the different properties listed in the docs. The Layer class docs can be found at http://dev.openlayers.org/docs/files/ OpenLayers/Layer-js.html. Parameters versus arguments These two terms are often confused and used interchangeably (but usually, that's ok). Parameters are what the items are called during the class or function definition, but when we call the function, the actual values of the parameters are referred to as arguments. For example, this is how we create a function in JavaScript: function add_numbers(a, b){ return a + b; } This is referred to as the add_numbers function definition. Here a and b are referred to as parameters. Now, take a look at how we call it: var the_sum = add_numbers(13, 37); Here, 13 and 37 are referred to as the arguments. In the add_numbers function, 13 acts as a and 37 acts as b. Both terms are technically talking about the same thing. Parameter is the term to use when talking about function definitions, and argument is the term to use when talking about function calls. The distinction is sort of like that of a meteoroid (when it's in space), a meteor (when it's in the atmosphere), and a meteorite (when it hits the ground)—these three terms refer to the same object. Now, let's take a look at some of the different arguments we can pass in when creating our layer objects. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 58 ] Time for Action – configuring the options parameter The options parameter is something that is present in all Layer classes, so let's get a bit more familiar with it. I suggest opening up the Layer documentation and following along, and even trying to add in the options yourself from the possible list of layer properties (at http:// dev.openlayers.org/docs/files/OpenLayers/Layer-js.html). 1. Use the template (or code from Chapter 1 with the WMS layer code removed). We'll be adding some WMS layers, and we'll refer to this file as chapter_3_ex2_ options_config.html. 2. First we'll add in a layer that contains the 'basic' layer from the WMS server, like in the previous example. // Setup our two layer objects var wms_layer_map = new OpenLayers.Layer.WMS( 'Base layer', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {isBaseLayer: true} ); 3. Next we'll create another layer object. We'll create a layer using labels, like in the previous example. This time though, let's set the layer's options to include visibility: false. This will cause the layer to be hidden by default. The layer definition should now look like: var wms_layer_labels = new OpenLayers.Layer.WMS( 'Location Labels', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'clabel,ctylabel,statelabel', transparent: true}, {visibility: false, opacity:0.5} ); 4. Time to create another layer. We'll set the layer params to ask the WMS service for the stateboundary layer. Then, we'll specify the options so that it won't display in the layer switcher control (via displayInLayerSwitcher: false) and set a minimum scale at which it will be visible (via minScale). That means this layer will only show up once we've reached a certain scale. Add the following to your init() function. var wms_state_lines = new OpenLayers.Layer.WMS( 'State Line Layer', 'http://labs.metacarta.com/wms/vmap0', {layers: 'stateboundary', This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 59 ] transparent: true}, {displayInLayerSwitcher: false, minScale: 13841995.078125} ); 5. Now let's add a layer that will show a different layer from the WMS service and we'll set the layer object's opacity to .8 (or 80 percent): var wms_water_depth = new OpenLayers.Layer.WMS( 'Water Depth', 'http://labs.metacarta.com/wms/vmap0', {layers: 'depthcontour', transparent: true}, {opacity:0.8} ); 6. Finally, we'll create a layer object that shows some road layers from the WMS service and has an options object containing the transitionEffect: resize property. This causes the layer to have a 'resize' animation when zooming in or out. var wms_roads = new OpenLayers.Layer.WMS( 'Roads', 'http://labs.metacarta.com/wms/vmap0', {layers: 'priroad,secroad,rail', transparent: true}, {transistionEffect:'resize'} ); 7. Now we just add the layers to the map (replace the previous addLayers function call with this one): map.addLayers([ wms_layer_map, wms_layer_labels, wms_state_lines, wms_water_depth, wms_roads]); 8. Save the file and then open it in your web browser. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 60 ] 9. Zoom in a few times around the Gulf of Mexico. You'll start to notice black contour lines in the ocean when you zoom in, and you should see a resize effect from the base ground layer. Depending on where you zoom, you should see something like this: What Just Happened? We just created another map with a few more layers and demonstrated some more layer options. The map looks slightly different than the first example's map because we are using different WMS server side layers (which are configured in each layer's params argument). For now, let's focus on the fourth argument passed in—the options argument. This argument is used in all Layer classes, so when we move on to other types of Layers you'll be able to use all the same properties we're talking about. The options argument controls the layer's object's properties. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 61 ] Configuring layer options Let's take a look at a couple of the layer's options argument we passed in during the previous example. Pay close attention to the commas. Both arguments and key:value pairs are separated by a comma, but make sure you do not have a comma after the last pair. Trailing commas will usually break your map and are usually a pain to debug—so if your map isn't working and you don't see any helpful error messages in Firebug, check for trailing commas. A great site to help with your code is http://jslint.com. wms_state_lines layer options This layer's options are: { displayInLayerSwitcher: false, minScale: 13841995.078125} The first property, displayInLayerSwitcher, can be either true or false, and determines if the layer will appear in the layer switcher control (if there is a layer switcher control). In our example, we do have a layer switcher control and you'll notice that we don't see it in the list of layers. The layer is still there, and we can programmatically turn it on or off through the setVisibility() function, e.g. wms_state_lines. setVisibility(false);. In fact, when you turn on or off a layer through the layer switcher control, this setVisibility function is what is being called. This property is useful when you want to include layers in your map that you don't want to let the user control. The second property is minScale: 13841995.078125. The value for this property is float, a number that can contain a decimal point. This minScale property determines the minimum scale the map must be at before the layer is displayed, which basically means how far we must be zoomed in before the layer will be turned on. There is also a maxScale property which determines how far we can zoom in before the layer is turned off. The term for this behavior is referred to as scale dependency. Scale dependency Scale dependency can be controlled either from the client or server side (or both). You'll notice that the wms_water_depth layer (with black contour lines in the water to specify depth) does not turn on until we start to zoom in. We haven't set the minScale property for the wms_water_depth layer, so why don't we see it at all zoom levels? The reason is WMS server has its own scale dependencies on the server side, so even if we wanted to see this layer when the map is zoomed out we can't because the server does not allow it. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 62 ] When we set the minScale or maxScale properties on a layer, we are specifying client side scale dependency—so even if the server allows it, we're telling OpenLayers not to show it. To determine the current scale of the map, you can call the map.getScale(); function which will show you the current scale value. wms_layer_labels layer options This layer's options property consists of: {visibility: false, opacity:0.5} The visibility property We've already covered the first property, so let's talk about the visibility property. Its value can be either true (by default) or false. This visibility property controls if a layer is visible or not. Setting it to false will make it hidden, but the user can turn it on by enabling it in the layer switcher control. The map.getVisibility(); and map. setVisibility({{Boolean}}); functions refer to this property. {{Boolean}} means we can pass in a Boolean, in other words, we can pass in either true or false. The opacity property The next property is opacity. It accepts a float with values between 0 and 1. A value of 0 means the layer will be completely transparent, and a value of 1 means the layer will be completely opaque. We set it to 0.5 here, so it will be 50 percent opaque. If you turn on the layer in the map (you can click on the layer in the layer switcher to enable it), you'll notice the labels are sort of see-through. This opacity setting helps you to create more visually pleasing maps, as by enabling multiple layers and changing their opacities you can produce some nifty effects. Map tiles Let's take a short break now and talk about just a little bit of theory. It has been mentioned that OpenLayers works by requesting 'map tiles' from a backend map server, but I haven't really gone much into what that means. Nearly all layers work on this map tiling principle (except the Vector layer and the Image layer). So, what is it exactly and how does it work? Think of how you might go about creating a web map from scratch—specifically, how would you handle the map image itself? You could go about it in two ways. First, you could just send back one giant image of a map. If the user zooms in, the server sends back an even bigger, more zoomed in image of a map. Using this strategy, you'd very quickly be sending over exponentially large image files every time the user zooms. This is why this method is not really practical. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 63 ] Many images make up a map The second way is that you break up the desired map image into a bunch of smaller images, or map tiles. So, no matter how far the user zooms in, the server only has to send over a relatively small amount of images. This sort of map tiling strategy is what nearly every web map does—Google Maps, Yahoo! Maps, most OpenLayers Layers, etc. When you request a new extent, OpenLayers asks for a new set of map images. Each map image is the same size (e.g., 256 x 256 pixels); this is referred to as the tile size, and can be specified when creating your map. Let's take a look at an example of a basic OpenLayers map. In our example, when you're completely zoomed out, the entire visible area will fit in one map tile image. When the client asks for the map's max extent, only one map tile needs to be returned. OpenLayers will figure out what extents to ask the map server for. In this case, it only needs to ask for one map tile, because we're zoomed out so far that the entire world will fit in one map image. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 64 ] At this point, you might be wondering if there is any benefit in breaking the images into tiles, as we're only getting back one image. However, when we zoom-in, it becomes more clear. Now we've zoomed in once. OpenLayers has to figure out what the extent of your map is, and how many tiles it has to ask the map server for. OpenLayers calculates the extent for each tile, and then sends a request to the map server to get an image of the requested extent. So, in this example, OpenLayers determines that it needs to send two requests to the map server. Each request will result in a response of a map image, and then OpenLayers will piece the tiles together. Keep in mind that the tile size is still the same as it was in the previous request. Your map only has one tile size, and in this case (and by default) it is 256 x 256 pixels. You can change the size of the tiles with the map's tileSize property, which we'll talk more about in Chapter 8 on the Map class. So what happens when we zoom in and move the map around a little bit? This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 65 ] In this case, OpenLayers requests more tiles because the total map area is outside what you can see (called the Viewport). The tiles are all still 256 x 256 pixels, but now tiles lie outside of the map's visible range. Any time the user moves their map to a new area (by zooming or panning), OpenLayers figures out how many tiles it needs to get, the extent for each tile, and where to place them. Available layer properties Let's go over the properties available for OpenLayers version 2.9, as we'll be using them throughout the book. These properties can be used by any Layer class, as all Layer subclasses (such as the WMS and Google Maps Layer) inherit from them. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 66 ] Data types Before we talk about the layer properties, let's quickly go over the notation for the data types used. The curly brackets { } indicate the JavaScript data type the parameter expects. The data types are as follows: ‹‹ {Array}: An array of elements separated by commas and enclosed in brackets. For e.g.: [1,2,3] ‹‹ {Boolean}: Possible values are true or false. ‹‹ {Float}: Possible values are numbers that can contain a decimal point. For e.g.: 42.5 ‹‹ {Integer}: Possible values are whole numbers (no decimals). For e.g.: 42 ‹‹ {Object}: An anonymous object, key:value pairs separated by commas, enclosed in curly brackets. For e.g.: {'answer': 42, 'question': null} ‹‹ {OpenLayers.______}: An object instantiated from an OpenLayers class. The blank could be any OpenLayers class. For e.g.: new OpenLayers.Control. LayerSwitcher({}); ‹‹ {String}: Possible value is any string, indicated by enclosed quotes (either single quote, ', or double quote, "). For e.g.: 'This is a string' Now that we're familiar with some of the data types, let's take a look at the Layer properties available to us. OpenLayers.Layer class properties So far we've only been working with the WMS Layer class, but OpenLayers has many more Layer classes. The following properties apply to all layer classes. We can set these properties via the options argument when we create the layer, and we can access them in Firebug by simply calling layer_object.property. The following is a table of layer properties available as of OpenLayers version 2.10. We will be using them throughout the book, and coming back to them often, so don't feel as if you need to memorize them right now. The latest properties can always be found in the OpenLayers Layer docs at http://dev.openlayers.org/docs/files/OpenLayers/ Layer-js.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 67 ] Property Property Data Type Description Default Value events {OpenLayers. Event} An OpenLayers event object. We can pass in an event object here that will call a function when an event, such as zooming in, occurs. We will talk more about events in a later chapter. - map {OpenLayers. Map} The map object that this layer belongs to. This is set automatically when the layer is added to the map via the setMap() function. isBaseLayer {Boolean} Determines if a layer is to act as a base layer. false alpha {Boolean} Specifies if the layer's images contain an alpha channel. This was originally designed to fix transparency issues in IE, but incurs a large performance hit. It is recommended that you do not use this. false displayIn LayerSwitcher {Boolean} Determines if the layer should be displayed in the layer switcher. true visibility {Boolean} Determines if the layer is visible on the map and enabled or disabled in the Layer Switcher control. true attribution {String} Text that is displayed when the Attribution control has been added to the map. By default, the attribution text appears in the bottom right and each layer's attribution is separated by a comma. - inRange {Boolean} Is either True or False, depending if the current map's resolution is within the layer's minimum and maximum range. This is set when the zoom level changes. - imageOffset {OpenLayers. Pixel} The displacement of the image tiles for layers with a gutter. - This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 68 ] Property Property Data Type Description Default Value options {Object} Optional object whose properties will be set on the layer. Any of these layer properties can be defined in this options object. This is the same options object we've spent the past couple of pages discussing. - eventListeners {Object} Event listeners will be registered if this is set during the layer object's creation. We will discuss this in detail in Chapter 6. - gutter {Integer} Sometimes you may notice artifacts around the edges of tiles that the map requests. When you set the gutter value, OpenLayers will request tiles that are bigger than the normal tile size by two times the gutter value. So, if your default tile size was 256 x 256, and if you had a gutter value of 10 then OpenLayers would request tiles with a size of 276 x 276. Anything outside the normal tile though (256 x 256 in this case) is not shown, and to OpenLayers the tile size is still 256 x 256. This really only needs to be used when you encounter problems with artifacts near the edge of your tiles. Non-tiled layers always have a gutter value of zero. 0 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 69 ] Property Property Data Type Description Default Value projection {OpenLayers. Projection} or {String} This will override the default projection of the map if specified. You may also need to set the maxExtent, maxResolution, and units properties. If you pass in a string instead of a projection object, it will be converted to a projection object. Projections are used to display a three dimensional object (the earth) in two dimensions (on our map). Different projections use different coordinates and measurement units. We will cover projections in more detail in Chapter 4. {EPSG:4326} units {String} The units the map's layer is in. Possible values are 'degrees' (or 'dd'), 'm', 'ft', 'km', 'mi', or 'inches'. 'degrees' scales {Array} Contains the map scales, from highest to lowest values. The units property must also be set when using this. It is recommended that you use the resolutions property instead. - resolutions {Array} Contains an array of map resolutions (map units per pixel) from highest to lowest values. If this is not set, it will be calculated automatically based on other properties, such as maxExtent, maxResolution, etc.). - This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 70 ] Property Property Data Type Description Default Value maxExtent {OpenLayers. Bounds} An OpenLayers.Bounds object consisting of min x, min y, max x, and max y values that specify the extent of the layer. Any coordinates outside this bounding box will not be displayed. If the displayOutsideMaxExtent property is set to false, tiles that fall outside these coordinates will simply not be requested. - maxResolution {Float} Sets the maximum resolution (the width or height, in map units, per pixel). Default max is 360 degrees/256 pixels. If you are not using a geographic projection, specify a different value. - numZoomLevels {Float} Specifies the number of zoom levels a layer has. The layer will not be displayed if the zoom level of the map is greater than this value. 16 minScale {Float} Specifies the minimum scale at which the layer will turn on. - MaxScale {Float} Specifies the maximum scale at which the layer will be shown. If the map scale is greater than this value, the layer will not be displayed. - displayOut sideMaxExtent {Boolean} Specifies if the map should request tiles that are completely outside of the layer's maxExtent property. false wrapDateLine {Boolean} This will cause the map to wrap around the date line. This allows you to continue dragging the map left/right forever, as the map is wrapped around onto itself. - This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 71 ] Property Property Data Type Description Default Value transitionEffect {String} Specifies the transition effect to use when the map is panned or zoomed. Possible values at the time of writing are null (no effect) and 'resize.' null SUPPORTED_ TRANSITIONS {Array} This is a constant—it does not change. It contains a list of support transition effects to use with the transitionEffect property. - metadata {Object} Allows you to store additional information about the layer. This does not have an effect on the layer itself. - Modifying layer properties Now that you're familiar with a few more layer properties, try to modify the second example's code, passing in additional properties to the options object. Open up the previous example and use Firebug to access individual layer properties, for example, map. layers[1].propertyName. Play around by passing in different properties to the options object when creating the layer objects, and don't be afraid to break your code! The OpenLayers.Layer class So far, we've only been using the OpenLayers.Layer.WMS class in the examples. This has been the beginning of all our layer object instantiation code so far: var wms = new OpenLayers.Layer.WMS( Notice the period between Layer and WMS: Layer.WMS. This means that we're accessing the WMS subclass of the Layer superclass. What do I mean when I say this? Well, the WMS class is a subclass of the Layer class, so let's briefly refresh our knowledge about subclasses. Subclasses In Chapter 1, we mentioned Subclasses. Subclasses are, basically, classes that derive from some base class. This base class is called a Superclass. The Layer class is thus the Superclass of all Layer subclasses (Superclasses and Subclasses are still classes). Subclasses inherit all the properties of their superclass. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 72 ] Layer Class—Sub and super classes The Layer class is therefore the superclass from which all other Layer classes derive. The Layer.WMS layer class is different from the Layer.Image class, and each of those two classes has their own unique properties and functions. But both layers also share all the properties of their Layer superclass—so all those properties we discussed earlier apply to both classes. Some Layer classes also inherit from multiple Layer class in a hierarchical way. For example, the Layer.WMS class inherits from the Layer.Grid class, which inherits from the Layer class. Other layer types OpenLayers supports a multitude of different Layer classes. As you may recall from Chapter 1, each Layer class is associated with a different map server back end. The Layer.WMS class is used to connect to a WMS map server, and the Layer.Google class is used to connect to the Google Maps service. Because OpenLayers is such an actively developed framework, there are a few layer classes that are deprecated (not recommended to use anymore). Later versions of OpenLayers will deprecate more Layer classes, so we'll try to focus on the classes that should be around for a while. We won't be able to cover every Layer class, but we will discuss the common ones. The Layer classes which we don't mention below will not be covered in this book, either because they are deprecated or outside the scope of this book. Let's take a look. Layer.ArcGIS93Rest This is the class that allows us to interact with ArcGIS Server 9.3 via its REST interface. Interaction with the REST interface is handled by constructing requests with URLs, and is similar in concept to how GET requests work. The structure to instantiate objects from this class is similar to Layer.WMS, as both inherit from the Layer.Grid class. ArcGIS Server is a third party, proprietary piece of software from ESRI, but OpenLayers provides excellent support for it. The OpenLayers documentation for the ArcGIS layer can be found at http://dev.openlayers.org/docs/files/ OpenLayers/Layer/ArcGIS93Rest-js.html. The documentation for ArcGIS Server's REST API can be found at http:// resources.esri.com/help/9.3/arcgisserver/apis/rest/ index.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 73 ] Layer.ArcIMS This layer class is similar to the ArcGIS93Rest class, and allows us to display data from ArcIMS Mapping Services. ArcIMS is another proprietary software product from ESRI. The documentation for this Layer class is at http://dev.openlayers.org/docs/files/ OpenLayers/Layer/ArcIMS-js.html. More information about ArcIMS can be found at http://www.esri.com/software/arcgis/arcims/index.html. Layer.Google This layer allows us to interact with Google Maps via the Google Maps API. The documentation for this layer class is located at http://dev.openlayers.org/docs/ files/OpenLayers/Layer/Google-js.html. Time for Action – creating a Google Maps layer Let's take a look at how to create a Google Maps layer. 1. From this point onward, I'm going to assume that when I say "create a new map" you will use the template from http://vasir.net/openlayers_book/, or create a copy of the example from Chapter 1 and remove the WMS layer calls (they both result in the same code). So, create a new map. 2. The first thing we'll need to do is add in a script tag to the <head> section that references the Google API library. This allows us to actually use the Google Maps library: <script src="http://maps.google.com/maps/api/js?sensor=false"></ script> 3. Now, we'll need to create a Google Maps layer object. We won't pass in any additional settings, so the default settings will be used. By default, we'll see the Google 'Streets' Map (we could ask for the aerial imagery layer, the physical topology layer, etc—we'll cover this soon). Add the following in your init function: var google_map_layer = new OpenLayers.Layer.Google( 'Google Map Layer', {} ); 4. All we have to do now is add the layer to the map. Before you do this, make sure you've removed the existing line of code that adds the WMS layer to the map. We only want to have our map contain the google_map_layer. map.addLayer(google_map_layer); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 74 ] You should see something like this: What Just Happened? We just created a map with a single Google Maps layer object using the Google Maps API (version 3). The layer that Google is showing us is the default layer named ROADMAP (referred to also as the 'streets' map). We can change the type of layer we see by changing the type value in the layer's options object. The possible values we can set for the type key are: ‹‹ google.maps.MapTypeId.ROADMAP (the default) ‹‹ google.maps.MapTypeId.TERRAIN ‹‹ google.maps.MapTypeId.HYBRID ‹‹ google.maps.MapTypeId.SATELLITE This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 75 ] You'll notice that each type is preceded by google.maps.MapTypeID., which must be present. For example, to specify that you wish to use the hybrid layer (satellite imagery combined with the ROADMAP layer), you could use: var google_hybrid_layer = new OpenLayers.Layer.Google( "Google Hybrid", {type: google.maps.MapTypeId.HYBRID} ); Because of the projections Google Maps and the other third party APIs use, it can be little tricky to get other layers to work with them—we'll talk about this more in Chapter 4 and Chapter 5. Briefly though, it has to do with the fact that these third party APIs use a projection called EPSG:900913, which uses a different coordinate system than EPSG:4326 (the projection our WMS layers have been in so far). Because of this, the coordinates don't match up. So, we can't just place our WMS layer (which is in the EPSG:4326 projection) on top of the Google Layer (which is in the EPSG:900913 projection), as the coordinates are very different. In the next chapters, we'll address how to solve this issue. The Google Maps Version 3 API can be found at http://code.google. com/apis/maps/documentation/javascript/ and more information on possible Google Maps Layer types can be found at http:// code.google.com/apis/maps/documentation/javascript/ overlays.html. Layer.Grid This is a base class that many other layers, such as WMS, inherit from. We won't be using it explicitly, but it is important to know that it exists. It works, basically, by constructing a grid and placing tiles (map images) inside the grid. It uses the HTTPRequest class to communicate with a map server to get the tiles it needs to build the map. Classes that inherit from it, such as WMS, work in a similar way. So, if you're familiar with how to use the WMS layer, you'll be familiar with how to use nearly any class that inherits from the Layer.Grid class. The documentation for this class is at http://dev. openlayers.org/docs/files/OpenLayers/Layer/Grid-js.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 76 ] Layer.Image This class allows us to use an image as a map layer. It's bit different than the other layer classes OpenLayers offers, because it doesn't strictly follow a client/server model. Once the initial request for the image is made, the client has the image and OpenLayers handles all further map interaction—no further requests are made to the server after the first request to get the image. There are no tiles, like with other raster (non vector, which is nearly every other layer class) layers. Instead, just a single image is used. The original purpose (and one of the best uses) of this class is to use an image for the overview map control. But it can be used for many more things than just that—this class, a great example of OpenLayers can be used for other purposes than just mapping. Time for Action – using the image layer Let's use the image layer to create a sort of image viewer. 1. First we'll need to add an image layer. The image layer expects the name as the first parameter (like other layers), the URL of the image next, then an {OpenLayers. Bounds} object specifying bounds of the image, then an {OpenLayers.Size} object which contains Width, Height pixel dimensions, and finally an optional options object. var image_layer = new OpenLayers.Layer.Image( 'Wallpaper', 'http://us.starcraft2.com/images/wallpapers/wall3/ wall3-1920x1200.jpg', new OpenLayers.Bounds(-180,-112.5,180,112.5), new OpenLayers.Size(1920,1200), {numZoomLevels:7, maxResolution:.625} ); 2. Let's create another image layer now. We'll set the opacity to 20 percent and make sure it's not a base layer by setting isBaseLayer: false in the options object. We'll also be using a different URL: var image_layer_2 = new OpenLayers.Layer.Image( 'Wallpaper 2', 'http://us.starcraft2.com/images/wallpapers/wall6/ wall6-1920x1200.jpg', new OpenLayers.Bounds(-180,-112.5,180,112.5), new OpenLayers.Size(1920,1200), {numZoomLevels:7, maxResolution:.625, isBaseLayer:false, opacity:0.2} ); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 77 ] 3. Finally, add the image layers to the map: map.addLayers([image_layer, image_layer_2]); You should see the following: What Just Happened? We just created an OpenLayers map that wasn't a map at all, but more of an image viewer. There are plenty of other uses for this though—you could overlay an image on top of a WMS layer, create a map from image layers consisting of high resolution scans of older printed maps, etc. The parameters required to create an image layer are: Image layer parameters ‹‹ name: {String} Name of the layer. ‹‹ url: {String} URL of the image. Can be on your own computer/server, or an external server. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 78 ] ‹‹ extent: {OpenLayers.Bounds} The extent of the image. ‹‹ size: {OpenLayers.Size} The size of the image, in pixels. ‹‹ options: {Object} An anonymous object containing the layer property settings. Let's take a look at our image_layer object's extent, size, and options arguments: new OpenLayers.Bounds(-180,-112.5,180,112.5), new OpenLayers.Size(1920,1200), {numZoomLevels:7, maxResolution:.625} The extent argument is an {OpenLayers.Bounds} object, which is created via OpenLayers.Bounds(-180,-112.5,180,112.5). We pass in the min x, min y, max x, and max y values to create the bounding box. For images, you may have to play around with the values depending on your image's dimensions. The ratio of our image (1920/1200) is 1.6, so I've set the bounds to have a similar ratio (180 / 112.5 = 1.6). The size argument, new OpenLayers.Size(1920,1200), is an {OpenLayers.Size} object, and we pass in the width and height of the image. For the next argument, the options, we pass in {numZoomLevels:7, maxResolution:.625}. The numZoomLevels property can be anything you'd like, but higher values may allow you to zoom in too far to the image. The value of maxResolution property is set to .625, which is a value I arrived at by dividing 1200/1920 (the width divided by the height)—just a rough calculation that, in case, makes the map look just about right. Documentation for this layer class is at http://dev.openlayers.org/docs/files/ OpenLayers/Layer/Image-js.html. Have a Go Hero – make your own image based maps Using the previous example's code, find other high resolution images and create a similar 'image viewer' with them. Add as many layers as you'd like, and play around with the opacity setting to see how the image layers interact with each other. Layer.MapGuide This is the layer for the open source MapGuide platform. We won't be covering MapGuide in this book, but it is a popular project and, since it is free and open source, you're welcome to install it on your own computer. It behaves similar to the Layer.WMS class and has the same parameters. It also inherits from Layer.Grid. The documentation for this layer class can be found at http://dev.openlayers.org/docs/files/OpenLayers/Layer/ MapGuide-js.html. More information on MapGuide can be found at http://mapguide. osgeo.org/. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 79 ] Layer.TileCache The TileCache layer allows you to interact with MetaCarta's TileCache, which is free open source software that caches WMS requests. TileCache provides enormous performance boosts because it caches requests to a WMS server. This means that once a request has been made, the response (the map image) is stored either on the server's hard drive or in memory and is immediately returned to the client. The normal process of requesting a map image from a WMS server goes like this: 1. Client sends request to WMS server, asking for a specific part of the map. 2. WMS Server receives the request and generates an image based on the requested map extent. 3. WMS Server returns the rendered map image to the client. The bottleneck is Step 2, the WMS server rendering the image based on the request. With caching (either through TileCache or by other means), the second step becomes a lot easier. With caching, the server only has to generate an image once, and then it saves it (in the hard drive or memory). So, instead of having to generate an image for every request, it simply grabs an image which it has already generated. This makes the request seem almost instantaneous, and most popular web maps use some form of caching—including Google Maps. We will be discussing TileCache a little more in the last chapter, where we talk about deploying and using OpenLayers in a production environment. The documentation for this layer class is at http://dev.openlayers.org/docs/files/OpenLayers/Layer/TileCache-js. html. For more information on TileCache, head to http://tilecache.org/. Layer.Vector The vector layer is one of the more powerful features of OpenLayers. It allows us to draw points and polygons on the map, style them however we want, retrieve and use KML and other geo data formats, etc. The vector layer makes uses of Protocols (such as HTTP), Formats (such as KML), and Strategies (such as Clustering) to display and provide an interactive layer. The vector layer also allows us to create and edit vector features. In Chapter 9, our chapter on the Vector Layer class, we'll go into far more detail. Layer.VirtualEarth This layer allows us to interact with Microsoft's VirtualEarth API. It works in a similar way to the Google Layer, so we won't spend more time discussing it here. Chapter 6 will fully cover this layer class. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 80 ] Layer.WFS This is a class that is deprecated, so try to avoid using it, as it will soon no longer be supported. I wanted to explicitly mention it because it is on its way out, but I still see lots of code that uses it. WFS (Web Feature Service), allow us to interact with data that is stored on the server. If you would like to use WFS, the general procedure is to use the Layer.Vector class and use WFS as the format. We'll be covering this in detail in Chapter 9, our chapter on the Vector Layer class. Layer.WMS This is the Layer class we've been using throughout the book so far. It inherits from Layer. Grid, and is similar to other classes we've mentioned. WMS is a popular standard and the way we interact with it is similar to how we interact with other Layer classes. We will be using this class throughout the book, so you haven't seen the last of it. Layer class documentation can be found at http://dev.openlayers.org/docs/files/ OpenLayers/Layer/WMS-js.html. More information and specifications of the WMS protocol can be found at http://www.opengeospatial.org/standards/wms. Layer.Yahoo This Layer class allows us to interact with the Yahoo! API. It works in a similar way to Layer.Google and Layer.VirtualEarth, and we'll be covering it in more detail in Chapter 6. Documentation for this layer class is at http://dev.openlayers.org/docs/files/ OpenLayers/Layer/Yahoo-js.html. The Yahoo! Maps API can be found at http:// developer.yahoo.com/maps/. Accessing layer objects The last topic to cover concerns the functions of the base Layer class. Before we do that though, let's go over how to access layer objects in Firebug. We'll be doing this often throughout the book, so it is quite important. Time for Action – accessing map.layers Let's jump in to using some of the Layer class functions. 1. Open up the second WMS example, chapter3_ex2_options_config.html in Firefox. We won't be editing the code yet. 2. Enable Firebug and go to the Console panel. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 81 ] 3. In the console, input and run this line of code: map.layers; 4. You should then see a list of layers. What Just Happened? You just accessed the layers property of the global map object. This is a property of the map object that contains an array of all the layers in the map. The list is ordered by the way you add layers to the map, so the first item in the list corresponds to the first layer we added to the map, the wms_layer_map layer. Not only will Firebug list the layer objects, you can also access any one of them by clicking on them. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 82 ] Time for Action – accessing layer objects in Firebug Let's take a look at how to access an object in Firebug now. Click on one of the items in the array of objects that was outputted from the previous Time for Action section. You should see something like this: What Just Happened? We just accessed our map's wms_layer_map layer object. What you see in the DOM panel is a listing of all the properties and functions that layer object contains. The DOM panel is one way we can access a layer object's properties. The DOM panel is a great way to get a quick look at all the properties and values of any object we wish to know more about. You'll notice some of the properties have not yet been mentioned; this is because some properties are specific to the Layer.WMS class, and is not included in the base Layer class. Accessing layer properties There are two common ways to access a layer object's properties. map.layers The first way is by accessing the layer object through the map.layers array, which we did in the previous example. Here, we are accessing the global map object, which was defined outside the init() function with var map;. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 83 ] If we know the index of a layer object we wish to view, we can use map.layers[n] to access the layer object (where n is the index). For example, if we wanted to access the wms_layer_map layer object, we know it is the first layer added to the map so its index is 0 (because array start its index at 0), so we would type map.layers[0]. The next layer, wms_layer_labels, has an index of 1, and so on. Accessing layers this way, however, is not advised. Adding a layer will change the array, as will deleting a layer. The best way to access layer objects is to store a reference to them, like we've been doing in all the examples (e.g., var my_layer = new OpenLayers. Layer()), as we'll go over now. Storing references to layer objects The second way is to define our layer objects as global variables, like how we did with our map object. So far, all our layer objects are defined inside the init() function, which means we can't access them or refer to them outside of the init() function. If we try to access the wms_base_ground layer object in Firebug, we will get an error because the wms_layer_ map layer object is not a global variable as we are not 'inside' of the init() function in Firebug. This concept is referred to as scope. Let's look at our code to see how the map object is defined as a global variable. var map; function init() { //Setup our map object map = new OpenLayers.Map('map_element', {}); // Setup our two layer objects var wms_layer_map = new OpenLayers.Layer.WMS( ... When you specify var, you are basically saying 'create a new variable here'. Notice how we define var map; outside the init() function. Inside the init() function, we do not use var when we set map equal to the map object, because the map variable has already been created. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 84 ] So, the process for making an object global is to: 1. Define the object/variable outside of the init() (or any function) with var variable_name;. 2. When you create the object inside the init() function, do not specify var when referring to variable_name. Notice our code to create the wms_layer_map layer object: var wms_layer_map = new OpenLayers.Layer.WMS(.…) Since we are using var inside the init function, the object is only now being created, and can only be referenced inside the init function. The wms_ layer_map object will only be accessible inside the same scope that it was defined. In this case, since it was defined inside init(), we can only refer to it with the code that is inside the init() function. To define it globally to access it outside the function, we could do: var map; var wms_layer_map function init(){ map = new OpenLayers.Map ... wms_layer_map = new OpenLayers.Layer.WMS( ... Using global variables can make things easier, but try to avoid using them if possible. If you do use them, keep track of them and take care when naming them. Pop Quiz – working with Variable Scope Take a look at the following code. What will be the value of the variable final? var a = 3; //Create a function that will set a variable called 'b' equal to 5 function set_variable(){ var b = 5; } //Call the function set_variable() final = a + b; 1. 8 2. 3 3. 5 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 85 ] 4. There will be an error because the variable b cannot be accessed outside the test function. Layer class methods We've covered the properties of the base layer class and discussed most of the layer subclasses so far. The remaining topic to cover is the functions or methods of the base layer class. Since we'll be talking about the functions of the layer class, all layer subclasses inherit and use these functions. Before we get to the functions though, let's cover how to access layer objects in Firebug. What's the difference between a function and a method? A method is just a term for a function that is owned by an object or a class. Time for Action – defining a global layer object variable Let's make the layer objects global variables access some properties of a layer, and call one of the layer's methods. 1. Make a copy of chapter3_ex2_options_config.html. Add the following lines right above the init() function: var wms_layer_map, wms_state_lines, wms_labels, wms_water_depth, wms_roads; 2. Inside your init() function, remove the var declaration before each of the layer object names. For example, the first line of your wms_layer_map definition should now look like this: wms_layer_map = new OpenLayers.Layer.WMS( 3. Save the file (we'll refer to it as chapter3_ex5_global_variables.html). 4. Open it up in Firefox and enable the Firebug console. 5. In the console panel, input and run: wms_layer_map;. You should see some output like this: Object { options=Object, more... } 6. Input and run the following command in Firebug: wms_layer_map.name This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 86 ] 7. Here we are directly accessing a property of the layer. You should see the layer's name property output: Base layer 8. Input and run the following command in Firebug: wms_layer_map.setVisibility(false); 9. Assuming you are at the full extent, you should no longer see the layer now, as you've just turned off the base layer's visibility. What Just Happened? We just made wms_base_ground a global variable, accessed a property of it, and called one of its methods. Any of the Layer class properties we talked about before can be accessed via layer_object.property;. So, now that we're a bit more familiar with how to access layer properties and functions on the fly, let's go over the methods of the base layer class. Now, just like properties, most layer subclasses (e.g., Layer.WMS) have their own set of functions, but also inherit all the functions from the base layer class. Layer class method definitions All of the following functions can be called the same way we called the setVisibility() function; by calling layer_object.function_name();. Some methods require that you pass arguments into them. We won't cover all the functions of the base layer class, as some are either deprecated, found in the Map class function list, or outside the scope of this book. The required arguments (if any) are listed under the parameters column. Function Description/Action Parameters Return Value setName( newName ) Set a new name for the layer based on the passed in string. This will also update the display name for the layer in the layer switcher control, and may trigger a changelayer event. newName: {String} The new name of the layer. - This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 3 [ 87 ] Function Description/Action Parameters Return Value addOptions( options ) Add additional options to the layer. The parameter behaves like the options parameter, and allows you to set properties on the layer. options: {Object} An anonymous object containing key:value pairs of layer properties and values. - redraw() Redraws the layer. Returns true if the layer was successfully redrawn or false if the layer could not be redrawn. You will need to call this while adding features to a vector layer, as we'll cover in Chapter 9. - {Boolean} getVisibility() Checks if the layer is visible or not. Returns true if the layer is visible, false if not. - {Boolean} SetVisibility( visibility ) Sets the visibility to true or false, hide/ show the layer, and redraw it. The visibility determines if a layer should be shown or not. visibility: {Boolean} true to show the layer, false to hide the layer. - calculateInRange() Returns true or false if the layer can be displayed at the map's current resolution. If the alwaysInRange property is true, this will always return true. - {Boolean} setIsBaseLayer( is_base ) Will turn the layer into a base layer if true is passed in and if false is passed in it will no longer make it a base layer. is_base: {Boolean} Set to true to make layer a base layer, false to make it an overlay layer. - This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 The 'Layers' in OpenLayers [ 88 ] Function Description/Action Parameters Return Value setOpacity( opacity ) Sets the opacity of a layer based on the value passed in. The passed in opacity value is a float with valid values from 0 to 1, with 0 being completely transparent and 1 being fully opaque. 0.5 would be 50 percent transparent. opacity: {Float} Value of desired opacity. - Have a Go Hero – call some functions Now that we've gone over some of the Layer class functions that we'll be making use of throughout this book, try calling them yourself. Open up Firefox, enable Firebug, and try calling some of the above functions on the layer objects (accessing the layer objects through either the map.layers array or the global layer objects themselves). Again, don't be afraid to experiment or break things! Summary In this chapter we talked about the Layer class. We covered the concept of layers and the differences between base layers and overlay layers. We went in depth with the WMS Layer class. We discussed the Layer class properties and methods and were introduced to various layer subclasses. Whew! This was all likely a lot to take in. We covered a lot of Layer classes individually, but we have not yet made a map with different types of Layer classes. This is one of the things that OpenLayers excels at, and is what drives map 'mash ups.' To understand how to go about putting different layer classes together though, we'll need to have a basic understanding of projections—the topic of our next chapter. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 4Wrapping Our Heads Around Projections When you look at a map, you are looking at a two dimensional representation of a three dimensional object (the earth). Because we are, essentially, 'losing' a dimension when we create a map, no map is a perfect representation of the earth. All maps have some distortion. The distortion depends on what projection (a method of representing the earth's surface on a two dimensional plane) you use. In this chapter, we'll talk more about what projections are, why they're important, and how we use them in OpenLayers. We'll also cover some other fundamental geographic principles that will help make it easier to better understand OpenLayers. In this chapter, we will cover: ‹‹ Concept of map projections ‹‹ Types of projections ‹‹ Longitude, latitude, and other geographic concepts ‹‹ OpenLayers projection class ‹‹ Transforming coordinates Let's get started! This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Wrapping Our Heads Around Projections [ 90 ] Map projections No maps of the earth are truly perfect representations; all maps have some distortion. The reason for this is because they are attempting to represent a three dimensional object (an ellipsoid: the earth) in two dimensions (a plane: the map itself). A projection is a representation of the entire, or parts of a, surface of a three dimensional sphere on a two dimensional plane (or other type of geometry). Why on earth are Projections used? Every map has some sort of projection—it is an inherent attribute of maps. Imagine unpeeling an orange and then flattening the peel out. Some kind of distortion will occur, and if you try to fully fit the peel into a square or rectangle (like a flat, two dimensional map), you'd have a very hard time. To get the peel to fit perfectly onto a flat square or rectangle, you could try to stretch out certain parts of the peel or cut some pieces of the peel off and rearrange them. The same sort of idea applies while trying to create a map. There are literately an infinite amount of possible map projections; an unlimited number of ways to represent a three dimensional surface in two dimensions, but none of them are totally distortion free. So, if there are so many different map projections, how do we decide on what one to use? Is there a best one? The answer is no. The 'best' projection to use depends on the context in which you use your map, what you're looking at, and what characteristics you wish to preserve. Projection characteristics As a two dimensional representation is not without distortion, each projection makes a tradeoff between some characteristics. And as we lose a dimension when projecting the earth onto a map, we must make some sort of tradeoff between the characteristics we want to preserve. There are numerous characteristics, but for now let's focus on three of them. Area Area refers to the size of features on the map. Projections that preserve area are known as equal area projections (also known as equiareal, equivalent, or homolographic). A projection preserves area if, for example, an inch measured at different places on the map covers the same area. Because area remains the same, angles, scales, and shapes are distorted. This is what an equal area projected map may look like: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 4 [ 91 ] Scale Scale is the ratio of the map's distance to the actual distance (e.g., one centimeter on the map may be equal to one hundred actual meters). All map projections show scale incorrectly at some areas throughout the map; no map can show the same scale throughout the map. There are parts of the map, however, where scale remains correct—the placement of these locations mitigates scale errors elsewhere. The deformation of scale also depends on the area being mapped. Projections are referred to as equidistant if they contain true scale between a point and every other point on the map. Shape Maps that preserve shape are known as conformal or orthomorphic. Shape means that relative angles to all points on a map are correct. Most maps that show the entire earth are conformal, such as the Mercator projection (used by Google Earth and other common web maps). Depending on the specific projection, areas throughout the map are generally distorted but may be correct in certain places. A map that is conformal cannot also be equal-area. The maps we've been using so far have been conformal. Other characteristics Projections have numerous other characteristics, such as bearing, distance, and direction. The key concept to take away here is that all projections preserve some characteristics at the expense of others. For instance, a map that preserves shape cannot completely preserve area. There is no 'perfect' map projection. The usefulness of a projection depends on the context the map is being used in. A particular projection may excel for a certain task, e.g. navigation, but can be a poor choice for other purposes. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Wrapping Our Heads Around Projections [ 92 ] Types of projections Projections are a way to represent three dimensions with two dimension surface. Projections are projected onto some geometric surface, three of the most common ones being a plane, cone, or cylinder. Imagine a cylinder being wrapped around the earth, with the center of the cylinder's circumference touching the equator. Now, the earth is projected onto the surface of this cylinder, and if you cut the cylinder from top to bottom vertically and unwrap it, and lay it flat, you'd have a regular cylindrical projection: The Mercator projection is one type of projection. If you've never worked with projections before, there is a good chance that most of the maps you've seen were in this projection. Because of its nature, there is heavy distortion near the ends of the poles. Looking at the previous screenshot, you can see that the cells get progressively larger, the closer you get to the North and South Poles. For example, Greenland looks larger than South America, but in reality it is about the size of Mexico. If area distortion is important in your map, you might consider using an equal area projection as we previously mentioned. More information about projections can be found at the USGS (US Geological Survey) website at http://egsc.usgs.gov/isb/ pubs/MapProjections/projections.html. EPSG codes As we mentioned, there are literally an infinite number of possible projections. So, it makes sense that there should be some universally agreed upon classification system that keeps track of projection information. There are many different classification systems, but OpenLayers uses EPSG codes. EPSG refers to the European Petroleum Survey Group, a scientific organization involved in oil exploration, which in 2005 was taken over by the OGP (International Association of Oil and Gas Producers). This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 4 [ 93 ] For the purpose of OpenLayers, EPSG codes are referred to as: EPSG:4326 The numbers (4326 in this case) after EPSG: refer to the projection identification number. This projection, EPSG:4326, is the default projection which OpenLayers uses. It has been the projection used in all our examples so far, and uses the familiar Longitude/Latitude coordinate system, with coordinates that range from -180° to 180° (longitude) and -90° to 90° (latitude). Time for Action – using different projection codes Let's create a basic map using a different projection. 1. Using the template code, recreate your map object the following way. We'll be specifying the projection property, along with the maxExtent, maxResolution, and units properties. If we use a projection other than the default projection, we need to tell OpenLayers the type of coordinates to use, and setting the maxExtent is one way to do this. The projection we're going to use is EPSG:900913, a projection used by Google Maps and other third party APIs. map = new OpenLayers.Map('map_element', { projection: 'EPSG:900913', maxExtent: new OpenLayers.Bounds(-20037508, -20037508, 20037508, 20037508.34), maxResolution: 156543.0339, units: 'm' }); 2. Save the file, we'll refer to it as chapter4_ex1.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Wrapping Our Heads Around Projections [ 94 ] 3. You should see something like the following: What Just Happened? We just created a map with the projection EPSG:900913. You'll notice that it looks quite a bit different than the maps we've made so far. This is because it is in a different projection. Specifying a different projection OpenLayers supports any projection, but if you want to use a projection other than EPSG:4326, you must specify the following three options: ‹‹ maxExtent: Default value is -180,-90,180,90 ‹‹ maxResolution: Default value is 1.40625 ‹‹ projection: Default value is EPSG:4326 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 4 [ 95 ] If you do not specify those options, the default values are used (all the other maps so far have been using the default values). You should also specify the units property, as we did with units: 'm', depending on the units your projection uses. The reason you must specify these properties is because different projections use different coordinates. In the above example, we set the maxExtent to: maxExtent: new OpenLayers.Bounds(-20037508, -20037508, 20037508, 20037508.34) These values are much different than the default values—they are not longitude and latitude values. Instead, they use an x/y coordinate system, and to OpenLayers the longitude is the x value and latitude is the y value. Pop Quiz – projections Give some reasons why you might want to use a projection other than EPSG:4326. What areas would not be best suited for displaying the EPSG:4326 projection? Longitude/Latitude Longitude and latitude are two terms most people are familiar with, even if they have limited geographic knowledge or get confused by the two. Let's take a look at the following screenshot and then go over these two terms. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Wrapping Our Heads Around Projections [ 96 ] Latitude Latitude lines are imaginary lines parallel to the equator, aptly known also as 'Parallels of Latitude'. Latitude is divided into 90 'degrees', or 90 spaces (or cells), above and below the equator. -90° is the South Pole, 0° would be the Equator, and 90° is the North Pole. Each space, or cell, (from 42° to 43°, for example) is further divided into 60 minutes and each minute is divided into 60 seconds. The minutes and seconds terminology has little to do with time. In the context of mapping, they are just terms used for precision. The size of a degree of latitude is constant. Because they measure 'north to south', OpenLayers considers the y coordinate to be the latitude. Longitude Longitude lines are perpendicular to the lines of latitude. All lines of longitude, also known as meridians of longitude, intersect at the North Pole and South Pole, and unlike latitude, the length of each longitude line is the same. Longitude is divided into 360 'degrees', or spaces. Similar to latitude, each space is also divided into 60 minutes, and each minute is divided into 60 seconds. As the space between longitude lines gets smaller, the closer you get to the poles the size of a degree of longitude changes. The closer you are to the poles, the shorter amount of time it would take you to walk around the earth. With latitude, it makes sense to use the equator as 0°, but with longitude there is no spot better than another to start the 0° mark at. So, while this spot is really arbitrary, the Observatory of Greenwich, England is today universally considered to be 0° longitude. Because longitude measures east and west, OpenLayers considers the x coordinate to be longitude. Time for Action – determining LonLat coordinates Let's take a look at a couple of examples on coordinates from our previous maps. 1. Open up the final example from Chapter 1. 2. Pan around the map in any direction. Then, in Firebug, type: map.getCenter(); 3. Depending on where you have panned, your output should read something like this: lon=-72.8125, lat=19.6875 4. Now, open up the example from the beginning of this chapter. 5. Pan around, and then in Firebug type: map.getCenter(); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 4 [ 97 ] 6. You should see something like: lon=-9397474.0038099,lat=3595597.9798909 What Just Happened? We just took a look at the longitude and latitude values for the center of the map in two different maps with different projections. When we call map.getCenter(), we get back an OpenLayers LonLat object. In the first map, the max extent of the map was between -180° and 180° for longitude, and between -90° and 90° for latitude. These are the values used by the EPSG: 4326, and it is a longitude/latitude type of coordinate system. The values for longitude and latitude change in the second map because they are not in the same projection (they are in EPSG:900913). OpenLayers projection class So far, we've been talking about the abstract idea of a projection. Let's dive into OpenLayer's Projection class, OpenLayers.Projection, which is what we use to actually handle projections. The Projection class relies on an external library called Proj4js, which can be found at http://proj4js.org. First, we'll talk about what we can do without the proj4js library, and then talk about what we can do with it. Creating a projection object To instantiate an object from the Projection class, the code would look like the following: my_proj = new OpenLayers.Projection('EPSG:4326', {}); Parameters Let's take a look at the parameters for the Projection class. ‹‹ projectionCode: {String}: A string of identifying the Well Known Identifier (WKID) for the projection, such as an EPSG code in the form of EPSG:4326. ‹‹ options:{Object}: An optional object. For instantiating projection objects, it is very common to leave this out. When creating a map and specifying the projection property, you can either pass in a projection object (like the one created above), or pass a string containing the projection code. This string, such as EPSG:4326, is also known as an SRS code. When passing in a code, like we've done with all our examples so far, OpenLayers automatically turns it into a projection object for you. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Wrapping Our Heads Around Projections [ 98 ] Functions The Projection class has a number of methods, including: Function Description Parameters getCode() Returns a {String} containing the projection code, e.g., 'EPSG:4326' - getUnits() Returns a {String} the units string for the projection, e.g., 'degrees'. If the Proj4js library is not included on your page, this will return null. - addTransform(from, to, method) Define a custom transformation function between two projections. This usually will not be necessary, especially if you are using Proj4js, unless you need to define a custom projection transformation. from: {String} Source projection code to: {String} Destination projection code method:{Function} A function that transforms the source point to the destination point, leaving the original point unmodified. transform(point, source, destination) Calling this function will transform the passed in point from the passed in source projection to the passed in destination projection. You can also pass in an {Object} as long as it contains x and y properties. This function will transform the point in place, meaning that the point you passed in will be transformed. If you need a copy of the point, you should first make a clone of it before calling transform() by calling the point's clone() method. point: {Geometry.Point} An object from the OpenLayers. Geometry.Point class, containing an x and y coordinate source:{OpenLayers. Projection} Projection object of the source projection destination:{OpenLayers. Projection} Projection object of the destination map projection. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 4 [ 99 ] Transforming projections Transforming a point means you take a coordinate in one projection and turn it into a coordinate of another projection. Apart from transforming EPSG:4326 to EPSG:900913 and vice-versa, OpenLayers does not provide support for transforming other projections out of the box—to do transforms of other projections, you'll need to include Proj4js (which can be found at http://proj4js.org/). In most scenarios, it is the job of the backend map server to handle projection transformations, but often it's useful or faster to do it on the client side (such as in the case of vector layer coordinate transformations). Let's take a look at how to transform EPSG:4326 to EPSG:900913 with OpenLayers. Time for Action – coordinate transforms Proj4js is not necessary for this example, as transforming between these two projections is possible without Proj4js. 1. Open up the previous example in Firefox. We won't be modifying any code, so any page which includes the OpenLayers library will be fine. 2. Open Firebug. In the console, create two projection objects: var proj_4326 = new OpenLayers.Projection('EPSG:4326'); var proj_900913 = new OpenLayers.Projection('EPSG:900913'); 3. Now let's create a LonLat object which will contain a point in EPSG:4326 coordinates. var point_to_transform = new OpenLayers.LonLat(-79, 42); 4. And now let's transform it. We'll take it from EPSG:4326 (our source proj_4326 projection object) to EPSG:900913 (our destination proj_900913 projection object): point_to_transform.transform(proj_4326, proj_900913); 5. Finally, we'll print the new value: console.log(point_to_transform); console.log(point_to_transform.lon, point_to_transform.lat) 6. Your output should read something like: lon=-8794239.7714444,lat=5160979.4433314 { lon=-8794239.7714444, more...} -8794239.7714444 5160979.4433314 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Wrapping Our Heads Around Projections [ 100 ] What Just Happened? We just transformed a point in the EPSG:4326 projection to a point in the EPSG:900913 projection. Let's take a closer look at the transform method we called on the point_to_ transform object. point_to_transform.transform(proj_4326, proj_900913); This will transform the original point from the proj_4326 projection to the proj_900913 projection. Notice, we aren't calling the transform() function of a projection object, but of a LonLat object. The transform() function's definition for an OpenLayers.LonLat object is as follows: Function Description Parameters transform(source, dest) This function transforms a point in place, meaning that the original point will be transformed; hence the original value will be lost. If you need a copy of it, use the .clone() method first. It returns itself. source: Source projection dest: Destination projection. In this case, our source projection is in EPSG:4326, and our destination projection is in EPSG:900913. Keep in mind however, that EPSG:4326 and EPSG:900913 are the only two projections you can do transforms on with OpenLayers if you do not include the Proj4js library. When creating your map, all your raster layers (image-based layers; nearly every layer except the vector and image layer) must be in the same projection as your map. You can do projection transformations with coordinates and the vector layer, but once OpenLayers gets back an image from a map server it cannot reproject the image itself (that's something the map server has to do). The Proj4js library The Proj4js library allows you to transform the coordinates from one coordinate system into another coordinate system. The Proj4js website is located at http://proj4js.org. By just including the Proj4js library on your page (like you do with OpenLayers), you can do more transforms within OpenLayers. Not all EPSG codes are supported, but there are many and if your desired code is not supported you can add a projection definition for it. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 4 [ 101 ] The site http://spatialreference.org contains Proj4js definitions for most of the EPSG codes. Ideally, you should be using the same projection throughout your map, but there are times when you may want to display the coordinates in a different projection—such as with a vector layer. Let's take a look at how to setup the Proj4js library. Time for Action – setting up Proj4js.org This step is similar to the way we set up OpenLayers. 1. Download Proj4js from http://trac.osgeo.org/proj4js/wiki/Download. At the time of writing, the latest version was proj4js-1.0.1.zip, so go ahead and download it (or whichever the latest version is). 2. Extract it and copy the proj4js folder to your root code directory (the folder where your OpenLayers.js file is located). 3. Add the following line in the <head> section of your code after the OpenLayers library inclusion code. <script type='text/javascript' src="proj4js/lib/ proj4js-combined.js"></script> 4. Now, open up the page and start Firebug. Type and run the following: var test_proj = new Proj4js.Proj('EPSG:4325'); console.log(test_proj); 5. You should see output that looks like the following: Object { srsCodeInput="EPSG:4325", more...} What Just Happened? We just included the Proj4js library and tested to see if it worked. If you received an error when you attempted to call new Proj4js.Proj() it means that the location of the proj4js-combined.js file was wrong. Ensure that the directories are set up properly (your example file should be in the same directory as the proj4js folder, and inside the proj4js folder should be a bunch of files and folders, including a lib folder that contains the proj4js-combined.js file). This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Wrapping Our Heads Around Projections [ 102 ] Defining custom projections Now that the Proj4js library is included, you can do transforms with more projections the same way we did in the previous example. Not all projections are defined; however, you are able to define them yourself, for example: Proj4js.defs["EPSG:27563"] = "+title=LAMB sud france +proj=lcc +lat_1=44.1 +lat_0=44.1 +lon_0=0 +k_0=0.999877499 +x_0=600000 +y_0=200000 +a=6378249.2 +b=6356515 +towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m"; After that, you'd be able to use EPSG:27563 for projection transformations just like you were able to use EPSG:4326 and EPSG:900913 from the earlier examples. There are a number of already defined projections, and you can view them more extensively at http://proj4js.org. A more complete list (containing Proj4js definitions for nearly any EPSG code) can be found at http://spatialreference.org/. Summary In this chapter, we talked about projections. We covered what they are and the various different types of projections. Longitude, latitude, and other geographic concepts were also discussed. While we just scratched the surface of these pretty complex topics, you should have enough fundamental information to understand how to use projections. We also talked about the Projection class, along with how to transform coordinates and use the Proj4js library. You'll often work with data in coordinate systems other than EPSG:4326, and knowing how to work with and transform data in other projections is important. In the next chapter, we'll take a look at Google Maps and other third party APIs, and put some of our recently gained knowledge of projections to use. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 5Interacting with Third Party APIs Web maps are very popular today, and are growing in popularity. After Google Maps was introduced, there was an explosion of interactive web maps. Google provides an API to interface with its mapping service, as do others now, and OpenLayers work well with most of them. Not only can we use these third party APIs with OpenLayers, we can also 'mashup' other layers on top of them. In this chapter, we're going to learn: ‹‹ Concept of third party mapping APIs ‹‹ Using the layer classes for third party APIs: Google, Microsoft, and Yahoo! ‹‹ How to use OpenStreetMap ‹‹ Working with Spherical Mercator ‹‹ Combining different layer classes Third party mapping APIs Web based maps are commonplace today. The catalyst for the explosive growth of web maps was the introduction of Google Maps. Web maps existed before, but they were not quick or developer friendly. In June 2005, Google released an API for Google Maps which provided a front end client (the role OpenLayers plays) along with an access to the backend map server. This allowed anyone to insert not just a Google Map on their site, but also allowed them to add in their own point data and manipulate the map in other ways. Google Maps grew in popularity, and other companies, such as Microsoft and Yahoo!, followed in their footsteps, creating their own web-mapping APIs. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 104 ] Map mashups The term mashup refers to an application that combines various different data sources and functionality together. A map mashup is a map that combines different layers and data. Third party mapping APIs, like Google and Yahoo! Maps, allow people to more easily create these 'map mashups'. For example, a map with a Google Maps base layer overlaid with markers that track places you've traveled to could be considered a map mashup. OpenLayers did not introduce map mashups, but it allows us to create very powerful ones with ease. Combining a Google's Map layer, a WMS layer, and a Vector layer is pretty simple with OpenLayers. OpenLayers and third party APIs OpenLayers allows you to use third party mapping APIs inside your map application, letting you use their maps inside of yours. The main caveat is that, at the time of writing, the third party API map must be a base layer (but, as we discussed before, you can use as many base layers as you wish). The three large commercial mapping APIs that OpenLayers can communicate with are Google Maps, Yahoo! Maps, and Bing (Microsoft) Maps. There is another free and open source API that OpenLayers works well with (and works in a similar manner to the previous three) called OpenStreetMap. We'll first take a look at each of these four Layer classes, and then we'll go over how to combine them with other Layer classes. Google Maps The software behind Google Maps consists of a client and server. The client is what you use when you visit http://maps.google.com, and it communicates with the Google Maps backend servers. Google provides an API that lets you use their own client and backend server, but since OpenLayers is used as our client, we're only interested in interacting with Google's backend map server. There may be legal restrictions depending on how you plan to use the Google Maps. Full restrictions can be found at http://code.google.com/apis/ maps/terms.html and http://www.google.com/intl/en_ALL/ help/legalnotices_maps.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 105 ] In the previous chapters, we've set up the Google Layer using Version 3 (V3) of the Google Map API. Because Google Maps updates their API, OpenLayers must also update to accommodate any changes that are made. OpenLayers versions prior to 2.10 (the one this book is based on) use Version 2 (V2) of the Google Maps API. Google Maps V2 usage is deprecated however, so we will focus on using V3. From our perspective, our code doesn't change much—OpenLayers handles all the version specific functionality behind the scenes for us. However, you are likely to encounter older OpenLayers maps made using Google Maps Version 2, so we will cover how to use both V2 and V3. Again, please use only V3 when making new maps, as V2 will eventually no longer be supported. Differences between Google Maps version 2 and version 3 There are really only three things that you must do differently when going from Google Maps V3 to V2 (or vice versa): 1. The script tag you add in to reference the Google Maps library is different. You do not need to provide an API key with V3 of the Google Maps API. 2. The layer names used in the type property are different. For example, the 'terrain' layer in V3 is defined as type: google.maps.MapTypeId.TERRAIN, but in V2 it is type: G_PHYSICAL_MAP. 3. The layer is configured with spherical Mercator (we'll cover what this means shortly). It is recommended you use V3 of the Google Maps API. The official Google Maps V3 documentation can be found at http://code.google.com/apis/ maps/documentation/javascript/. Official Google Maps API docs for V2 can be found at http://code. google.com/apis/maps/documentation/javascript/v2/ reference.html. Time for Action – using Goole Maps V3 (standard way) Let's create a map using V3 of the Google Maps API. We've already done this in Chapter 3, so you should be somewhat familiar with it. This is the standard way to use the Google Maps Layer, and you should use this method unless you specifically need a layer type in V2 that V3 does not yet support. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 106 ] With versions of OpenLayers prior to 2.10, accessing Google Maps V3 may not work. 1. Version 3 of the Google Maps API does not require an API key. However, you still must include the following in your <head> section, so OpenLayers knows where to look for the Google Maps API library. We'll be asking the map's API and specify that we want v3.2 of the Google Maps API (you can leave the &v=3.2 parameter if you'd like to let Google Maps provide the latest version for you automatically). Add this before your OpenLayers inclusion script: <script src="http://maps.google.com/maps/api/js?sensor=false&v=3.2"></ script> 2. Let's start by adding a hybrid layer to our map. The type property is in the form of google.maps.MapTypeId.TYPE, where TYPE in this case is HYBRID: var google_hybrid = new OpenLayers.Layer.Google( "Google Hybrid", {type: google.maps.MapTypeId.HYBRID} ); 3. Now we'll add a physical (topographic type) layer: var google_physical = new OpenLayers.Layer.Google( "Google Physical", {type: google.maps.MapTypeId.TERRAIN} ); 4. To add a satellite layer type: var google_satellite = new OpenLayers.Layer.Google( "Google Satellite", {type: google.maps.MapTypeId.SATELLITE} ); 5. Now, we'll create a streets layer. If we do not pass in a layer type, the streets layer is used by default. If you wish to manually specify the type, the streets map type is google.maps.MapTypeId.ROADMAP. var google_streets = new OpenLayers.Layer.Google( "Google Streets", { ); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 107 ] 6. We'll add the layers to the map: map.addLayers([google_hybrid,google_physical,google_satellite, google_streets]); 7. Finally, add a layer switcher control. You'll also notice that all the layers on the map that we've added are all base layers. By default, third party map API layers act as base layers. map.addControl(new OpenLayers.Control.LayerSwitcher()); 8. Open up the page. You should see something similar to this: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 108 ] What Just Happened? We just created a map using Google Maps API V3. We only needed to do two things: 1. Include the Google Maps V3 API with <script src="http://maps.google. com/maps/api/js?sensor=false&v=3.2"></script>. This lets OpenLayers access the Google Maps API. 2. Set the type property, such as type: google.maps.MapTypeId.HYBRID. Including the Google Maps API enables OpenLayers to communicate with the Google Maps backend. Because we included the Google Maps API, we can use variables that are part of it. Specifically, the possible values for the type property come directly from the Google Maps API—for example, if we didn't include the API, we could not use google.maps. MapTypeId.HYBRID. Notice that if you run map.getCenter(); in Firebug, the center point we get back has very different coordinates than what we're used to—they aren't longitude/latitude values. This is because sphericalMercator is set for us automatically, so we can easily lay other layers, such as vector layers, on top of the third party map layer. Before we cover the reason for this (which is related to the other third party mapping APIs) let's go over the Google Map Layer class in more detail. Creating a Google Map layer object The format to create a Google Map layer object is the same for both V2 and V3 of the Google Maps API. The basic format for instantiating a Google Maps layer is: var google_layer_object = new OpenLayers.Layer.Google( 'Layer Name', {Properties} ); The first argument, as with most Layer classes, is the layer's name. The second argument is a properties object. There are just a few Google Layer specific properties, so let's go over them. Google layer properties Because the Google Layer inherits from the base Layer class, you can use nearly any property that the Layer class provides—such as numZoomLevels or maxExtent. We'll cover just the properties that are specific to the Google Maps layer class. The possible values for the type attribute depend on the version of the Google Maps API used. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 109 ] sphericalMercator {Boolean} This property determines if the map should behave as a Mercator-projected map. Setting to true will allow us to use other layers, such as the Vector layer, with the actual map projection. Spherical Mercator is covered in more depth later in this chapter. When using V3 of the Google Map API, this property is automatically set to true. With V2, you will have manually set it to true. Example: sphericalMercator: true. type {GmapType} The type property specifies the Google Map layer type—what layer Google should give us. V2 and V3 of the Google Maps API have different possible values. Let's go over Version 3's values, and then take a look at Version 2's values. V3 GMapType values The standard, V3 way to refer to layer types is in the form of: var google_layer_V3 = new OpenLayers.Layer.Google( 'Google Maps V3 Layer', {type: google.maps.MapTypeId.TYPE} ); To specify the layer type, we can simply change the TYPE property. The possible TYPE values can be one of the following: Type Description google.maps.MapTypeId.ROADMAP The default value, used if nothing is passed in. Shows the default street map (same as G_ NORMAL_MAP). google.maps.MapTypeId.HYBRID Displays a map with a semi-transparent street layer overlaid on satellite imagery (same as G_HYBRID_ MAP). google.maps.MapTypeId. SATELLITE Displays satellite imagery (same as G_ SATELLITE_MAP). google.maps.MapTypeId.TERRAIN Displays a map with features such as terrain (same as G_PHYSICAL_MAP). An up-to-date list of possible layer types can be found in the Google Maps API documentation at http://code.google.com/apis/maps/ documentation/javascript/maptypes. html#MapTypes. At the time of writing, the non-earth (moon, Mars, and sky) layers were not yet available from Google Maps. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 110 ] V2 GMapType values Version 2 of the Google Maps API provides various layers as well. Some of the layer types are even maps of other planets, but no extra work is required to view them inside the OpenLayers. To specify the map type for V2, use the following: var google_layer_V2 = new OpenLayers.Layer.Google( 'Google Maps V2 Layer', {type: TYPE} ); The possible TYPE values are the following: Type Description G_NORMAL_MAP The default value. This is used if no GMapType is specified. This map type will display the normal street map. G_SATTELITE_MAP Displays satellite imagery. G_AERIAL_MAP Displays aerial photography. G_HYBRID_MAP Displays a map with a semi-transparent street layer overlaid on satellite imagery. G_AERIAL_HYBRID_ MAP Displays a map with a semi-transparent street layer overlaid on aerial imagery. G_PHYSICAL_MAP Displays a map with features such as terrain. G_MOON_ELEVATION_ MAP Displays a terrain map of the moon with altitude color coded. G_MOON_VISIBLE_MAP Displays photography taken from orbit around the moon. G_MARS_ELEVATION_ MAP Displays a terrain map of Mars with altitude color coded. G_MARS_VISIBLE_MAP Displays photography taken from orbit around Mars. G_MARS_INFRARED_ MAP Displays infrared imagery of Mars. Warm areas are bright and cooler areas are dark. G_SKY_VISIBLE Displays a map of the full celestial sphere. To use any of these layer types, simply pass in the type name when creating the layer. Example: type: G_NORMAL_MAP. The previous list was the supported layer types at time of writing, but current and up-to-date layer values can be found on the Google Maps V2 API docs at http://code.google.com/apis/maps/documentation/ javascript/v2/reference.html#GMapType. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 111 ] Time for Action – creating a Google Map layer with V2 (Deprecated) Let's create a map with V2 of the Google Maps API. This is not the proper way to use the Google Maps layer, but I am including it because you are likely to come across a code that uses V2 of the API. Google Maps V2 also provides a few different layer types (like a moon layer) that V3 does not provide at the time of writing. 1. V2 of the Google Maps API requires you to register an API key, so grab one (for free) at http://code.google.com/apis/maps/signup.html. If you don't have a domain name, use http://localhost. 2. In the <head> section, before the OpenLayers script tag, include the following. Replace YOUR_KEY with the key generated from Step 1: <script src='http://maps.google.com/maps?file=api&v=2&key=YOUR_ KEY'></script> 3. Let's create some layers now. The format of the type attribute is slightly different with V2. We'll go over the possible layer type values right after this example. First, let's create some layers, starting with a 'hybrid' layer. The type value for the hybrid layer is G_HYBRID_MAP. We'll also specify 20 zoom levels with the numZoomLevels property. Different base layers can support different numbers of zoom levels: //Create Google Map Layer objects var google_hybrid = new OpenLayers.Layer.Google( "Google Hybrid", {type: G_HYBRID_MAP, numZoomLevels: 20} ); 4. Now let's create a 'physical' layer, which is a terrain/topology type of layer. The type is G_PHYSICAL_MAP, and we'll specify a different numZoomLevels property: var google_physical = new OpenLayers.Layer.Google( "Google Physical", {type: G_PHYSICAL_MAP, numZoomLevels: 22} ); 5. We'll use the G_SATELLITE_MAP to create a satellite layer: var google_satellite = new OpenLayers.Layer.Google( "Google Satellite", {type: G_SATELLITE_MAP} ); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 112 ] 6. If we don't pass in a type, the default 'streets map' layer will be used. This is the layer you'll see by default in Google Maps. If you wish to manually specify this layer type, it is called G_NORMAL_MAP. var google_streets = new OpenLayers.Layer.Google( "Google Streets", {} ); 7. Now let's add a layer type that is not (at the time of writing) supported by V3 of the Google Maps API—a layer of Mars: var google_mars = new OpenLayers.Layer.Google( "Google Mars", {type: G_MARS_VISIBLE_MAP} ); 8. Finally we just add the layers to the map: map.addLayers([google_hybrid,google_physical,google_satellite, google_streets,google_mars]); 9. Take a look at your map. You should see something like this: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 113 ] What Just Happened? We just created a map using Google Maps V2 with a few of the possible Google Maps layer types. Because we're using a different version of the API here, you'll notice that our code is slightly different than it was when using the standard (V3) of the Google Maps API. Specifically, the type property is different because V2 of the API supports different layer types than Version 3 does. Yahoo! Maps API OpenLayers provides us with an easy way to interface with the Yahoo! mapping API as well. We work with it similar to how we work with the Google Maps Layer class. The official Yahoo! Maps API documentation can be found at http://developer.yahoo.com/maps/. Let's take a look! Time for Action – using the Yahoo! Maps Layer Let's create a map with the Yahoo! Maps Layer class. 1. In the <head> section, we need to reference the location of the Yahoo! Maps API. Add the following before the OpenLayers inclusion script tag: <script src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=euzuro- openlayers"></script> 2. Let's set up some Yahoo! layer objects now. Like the Google Maps Layer, we specify the type of the layer we want by setting the type property. Let's create a hybrid layer. The type property for Yahoo! layers start with YAHOO_MAP_, followed by a three character code: var yahoo_hybrid = new OpenLayers.Layer.Yahoo( "Hybrid", {type: YAHOO_MAP_HYB, numZoomLevels: 24} ); 3. Now let's create a satellite layer type. The three character code for satellite is SAT. We'll also set a different amount of zoom levels to further illustrate how each base layer can have its own amount of zoom levels: var yahoo_satellite = new OpenLayers.Layer.Yahoo( "Satellite", {type: YAHOO_MAP_SAT, numZoomLevels: 20} ); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 114 ] 4. Let's add a default layer next. If we don't pass a type in, the default value will be used, which is YAHOO_MAP_REG—a street-like map. var yahoo_street = new OpenLayers.Layer.Yahoo( "Street", {} ); 5. Finally we just add the layers to the map: map.addLayers([yahoo_hybrid, yahoo_satellite, yahoo_street]); 6. Open the page. You should see something like the following: What Just Happened? We just created a map with a Yahoo! Maps layer. Like the Google Maps layer, we need to include a link to the Yahoo! API—but we do not need an API key. We included the API with: <script src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=euzuro- openlayers"></script> Like the Google Maps API script, this script allows OpenLayers to communicate with the Yahoo! Maps API. It also provides us values to use for the type property. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 115 ] Yahoo! Maps Layer class properties Like the Google Maps, our main focus on this layer class will be on the type property. Let's go over the available type values. Yahoo! Maps Layer types At the time of writing there were three layer types the Yahoo! Maps API gives us access to: Type Description YAHOO_MAP_HYB Displays a hybrid map consisting of the satellite map overlaid with the regular street map. YAHOO_MAP_SAT Displays satellite photography. YAHOO_MAP_REG Displays a street map. This is the default type value, so if type is not passed in this will be used. Microsoft's mapping API Microsoft provides an interface to their mapping services as well. Their mapping service previously was referred to as Virtual Earth, but they have since re-branded it as Bing Maps. Hence, in OpenLayers, the Layer Class is called VirtualEarth, and we use it the same way we've created Google and Yahoo! layers. The official Microsoft documentation can be found at http://msdn.microsoft.com/en-us/library/dd877180.aspx. Time for Action – creating a Bing/Virtual Earth Layer 1. Include a reference to the Microsoft Mapping API in your <head> section, before the OpenLayers inclusion script: <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol. ashx?v=6.2&mkt=en-us"></script> 2. Let's set up some layer objects now. We specify the type property like in the previous examples. For this layer, the type is prefixed by VEMapStyle., which stands for 'Virtual Earth Map Style'. Let's create a 'shaded' layer, which in this case is the street/road layer with shaded elevation: var ve_shaded = new OpenLayers.Layer.VirtualEarth( "Shaded", {type: VEMapStyle.Shaded} ); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 116 ] 3. We'll add an aerial (satellite imagery) layer now: var ve_aerial = new OpenLayers.Layer.VirtualEarth( "Aerial", {type: VEMapStyle.Aerial} ); 4. Now, let's create a default layer. If we don't pass in a type, the VEMapStyle.Road layer will be used. It is similar to the shaded layer, but without the shading. We'll also use another property called animationEnabled, which we'll set to false. By default, this property is set to true and controls whether or not a zooming animation will be applied to the layer (this animation is a Virtual Earth layer specific property): var ve_road = new OpenLayers.Layer.VirtualEarth( "Road", {animationEnabled: false} ); 5. Let's add a hybrid layer: var ve_hybrid = new OpenLayers.Layer.VirtualEarth( "Hybrid", {type: VEMapStyle.Hybrid} ); 6. Finally, add the layer objects to the map: map.addLayers([ve_shaded, ve_aerial, ve_road, ve_hybrid]); 7. You should see something like the following: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 117 ] What Just Happened? We just made a map using the Microsoft Virtual Earth/Bing API. It works similar to how the Google and Yahoo! Maps API work—by providing a script tag to the Microsoft mapping API. We can communicate with Microsoft's map server and use different type values. Let's go over the properties, as there is a new one the other third party layer classes do not provide. VirtualEarth layer class properties Like the previous layers, the type property controls what layers we get back from the map server. However, there is a unique animationEnabled property that we can use with the VirtualEarth layer. Let's take a look at it: Property Data Type Description animationEnabled {Boolean} This property determines if panning/zooming animations should be enabled. By default, the value is true. If it is false, the animations will be the same as the other layers' animations. If you look at the Road layer from the previous example, you can see the difference with this property set to false. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 118 ] Possible type values The possible values for the type property, at the time of writing, are the following. Type Description VEMapStyle.Shaded Displays a shaded street/road map, showing elevations. VEMapStyle.Aerial Displays aerial (satellite) imagery. VEMapStyle.Road Displays a road map. VEMapStyle.Hybrid Displays an aerial layer overlaid with a road map. Let's cover one more layer type before we start combining different layers together—the OpenStreetMap Layer. OpenStreetMap OpenStreetMap (or OSM) is a free, wiki-style, map of the world driven by user contributed content. You are able to use your own OSM tiles or ones provided through the OpenStreetMap servers. Unlike the previous third party APIs, there is no type property to specify for this layer. Setting up an OpenStreetMap service and tiles yourself is not too difficult, but it is outside the scope of this book (visit http://wiki.openstreetmap.org/wiki/OpenLayers_ Simple_Example for more information on this). Accessing OSM with OpenLayers, however, isn't. More information about OpenStreetMap can be found at http://www.openstreetmap.org/. Time for Action – creating an OpenStreetMap Layer 1. For the OpenStreetMap layer, we do not need to include a script; we can access it outside of the box. 2. This will be pretty simple. We just need to create an OSM layer object: var osm_layer = new OpenLayers.Layer.OSM( 'OpenStreetMap Layer' ); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 119 ] 3. Then, we just add the layer object to the map. Even though we're only passing in one layer object, we'll use the addLayers function to keep our code consistent: map.addLayers([osm_layer]); 4. You should see something like this: What Just Happened? The map we just created is showing an OpenStreetMap layer. Unlike the previous layers, we did not have to provide an API key or link to OSM specific files—OpenLayers knows about it already. Another thing that separates the OpenStreetMap layer from the other third party map layers is that the OSM layer has no type property. Accessing your own OSM tiles The above code uses the publicly available OSM tiles, but it is easy to point it at your own tiles. To do so, create the layer in this format: var osm_layer = new OpenLayers.Layer.OSM( 'My OSM Layer', 'http://URL_TO_TILES/${z}/${x}/${y}.png', This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 120 ] {} ); To use this, you would replace URL_TO_TILES with the server hosting your OSM tiles. The ${z}, ${x}, ${y} are variables which OpenLayers will replace with the appropriate values to reference specific map tiles. Spherical Mercator Let's talk about Spherical Mercator. I've made a couple of references to it throughout this chapter, but what is it? It is a term used to refer to the projection that many commercial, third party mapping APIs use. We need to use Spherical Mercator to properly overlay data and layers on top of third party map layers. All the APIs we've used in this chapter so far (Google, Yahoo!, VirtualEarth/Bing, and OSM) are in the Spherical Mercator projection—a projection that treats the earth as a sphere (as opposed to an ellipsoid). Spherical Mercator—EPSG code The official EPSG code for the projection is currently EPSG:3857, but when the code was established it was referred to as EPSG:900913. This EPSG:900913 code can still be used in OpenLayers, as the EPSG:3857 code is identical to it. Google was one of the (if not the) first to publish maps with this projection—notice how 900913 resembles 'gOOglE' (with a backwards 'E' for 3). You may also see other EPSG codes that represent the same Spherical Mercator projection. For example, ArcGIS version 9.3 uses the code EPSG:102113, and you may also encounter the standard EPSG:3785. These all refer to the same 'Spherical Mercator' projection and can be used interchangeably. So, what does this mean for us? Well, when we use these third party APIs, we use coordinates that don't look like Lon/Lat coordinates (they are x, y in meters). Because the earth is treated as a sphere in the Spherical Mercator projection, calculations are affected and data from other sources may not match up perfectly with it if we don't re-project our data. We need to set up Spherical Mercator before we can add in other layer classes and have them line up correctly, so let's take a look. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 121 ] Time for Action – using Spherical Mercator Let's set up a map using Spherical Mercator with a Yahoo! Maps Layer. When using the Google Maps Layer (V3), the sphericalMercator property is set to true automatically, so you do not need to do anything extra. However, explicitly specifying it is a good idea when working with other layers that are not in the Spherical Mercator projection. 1. Since we'll use the Virtual Earth layer type, we need to make sure to include the Microsoft Mapping API: <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol. ashx?v=6.2&mkt=en-us"></script> 2. The first step is to specify the maxExtent, maxResolution, units, and projection properties when creating our map object. If you'll remember from the previous chapter, these are the properties we must set when we specify a map projection other than the default EPSG:4326 projection. We'll also set the displayProjection property to an EPSG:4326 projection object. This displayProjection property specifies what projection various controls, such as the MousePosition control, display coordinates in. For the EPSG: 900913 projection, (-)128 * 156543.0339 is the max extent. map = new OpenLayers.Map('map_element',{ maxExtent: new OpenLayers.Bounds( -128 * 156543.0339, -128 * 156543.0339, 128 * 156543.0339, 128 * 156543.0339), maxResolution: 156543.0339, units: 'm', projection: new OpenLayers.Projection('EPSG:900913'), displayProjection: new OpenLayers.Projection("EPSG:4326"), }); 3. Now, we'll create a Microsoft Virtual Earth (Bing) map layer object. This time, we'll specify sphericalMercator: true so the layer is properly projected to the projection: var ve_road = new OpenLayers.Layer.VirtualEarth( "Road", {sphericalMercator:true} ); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 122 ] 4. Open the map, and in Firebug run the command: map.getProjection(); 5. You should see the following output: "EPSG:900913" What Just Happened? We just set up a map using a Yahoo! Maps Layer with Spherical Mercator enabled on it. When using a different projection, we have to specify some properties (as we talked about in the previous chapter). Let's briefly go over the properties relevant to the EPSG:900913, the Spherical Mercator, projection. Map properties with Spherical Mercator layers Because we're working in a different projection, we have to tell OpenLayers some things about the map so it knows how to set it up. Let's take a look at the map properties which we set in the code above. maxExtent The first property we pass in to the map properties is: maxExtent: new OpenLayers.Bounds( -128 * 156543.0339, -128 * 156543.0339, 128 * 156543.0339, 128 * 156543.0339), The maxExtent property tells OpenLayers the maximum boundaries of the map. The four numbers represent the minimum x, the minimum y, the maximum x, and the maximum y coordinates. The numbers we use here are the coordinates for the extent of the world in EPSP:900913—quite different from Longitude/Latitude coordinates. Without specifying the maxExtent, our map would not display properly since OpenLayers would not know what the boundaries of the world are. maxResolution This property sets the maxResolution of the map, which is based on fitting the map's extent into 256 pixels. This is a {Float} data type. For example, maxResolution: 156543.0339, More information about this property can be found in Chapter 8, the chapter about the Map Class. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 123 ] units This specifies that the map is in meters. It is a {String} data type. units: 'm', By default, the units property is set to degrees. Since Spherical Mercator is a projection that uses meters, we need to specify it here. projection Here we set the map's projection to be ESPG:900913, i.e., the Spherical Mercator projection: projection: new OpenLayers.Projection('EPSG:900913'), If we wish to work with Longitude/Latitude, or other layer types in a different projection, we need to transform the coordinates from EPSG:900913 to the appropriate other projection. Once we start to use a third party map API, we're stuck using the Spherical Mercator projection. Using Google Maps and other layers Getting other layers to play nicely with these third party layers involve three things, two of which we've already done: 1. Set up the correct map projection properties. 2. Make sure sphericalMercator is set to true on the third party map layer. 3. Ensure all raster layers (any non-Vector or Image layer), such as WMS, are in the map's projection. In this case, we'll need to make sure we ask our WMS server for map tiles in the EPSG:900913 projection. Using what we learned so far, let's make a mashup. We'll use a Google Maps layer as the base layer, and WMS and Vector layers as overlay layers. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 124 ] Time For Action – creating your first mashup Let's make a map 'mashup' that consists of a Google Map layer, a WMS layer, and a Vector layer. 1. First, we need to add the Google Maps V3 script API tag in the <head> section: <script src="http://maps.google.com/maps/api/ js?sensor=false&v=3.2"></script > 2. Now, set up your map object with the proper projection info, like in the previous example: map = new OpenLayers.Map('map_element',{ maxExtent: new OpenLayers.Bounds( -128 * 156543.0339, -128 * 156543.0339, 128 * 156543.0339, 128 * 156543.0339), maxResolution: 156543.0339, units: 'm', projection: new OpenLayers.Projection('EPSG:900913'), displayProjection: new OpenLayers.Projection("EPSG:4326"), }); 3. Now let's create a Google Maps layer. Because we are using V3 of the API, we do not need to specify sphericalMercator: true because it is set to true automatically. var google_streets = new OpenLayers.Layer.Google( "Google Streets", {numZoomLevels: 20} ); 4. We'll create our WMS layer now and specify that we want the basic and various label layers back from the WMS server. We'll also set the opacity to .7, or 70 percent opaque, and set isBaseLayer: false so we're sure it will be an overlay layer. Now, we are not specifying a projection property on the layer because our layer will inherit the projection of the map object. All layers (with the exception of the Vector layer) should be in the same projection as your map. The WMS server we're using does support the EPSG:900913 projection, so we'll get back proper map tiles. If the WMS service does not support this projection, we wouldn't be able to use it—we'll talk about this more after the example. Go ahead and create the WMS layer: var wms_layer = new OpenLayers.Layer.WMS( This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 125 ] 'OpenLayers WMS', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic,clabel,ctylabel,statelabel', transparent: true}, {isBaseLayer: false, opacity: .7} ); 5. Now let's add a Vector layer. It will be in the EPSG:900913 projection as well, since it inherits the projection from the map object. We could, however, specify a different projection for the vector layer and re-project vector data on the fly (we can't do this on raster layers, like the WMS layer)—but we'll save this topic for Chapter 9, our Vector layer chapter. Create the vector layer object: var vector_layer = new OpenLayers.Layer.Vector( 'Editable Vectors'); 6. We'll add an EditingToolbar control now, which lets us add points and polygons to a vector layer via a toolbar that will appear in the upper right corner of the map (by default). When creating the control object, we simply pass in the vector layer object we wish the control to use: //Add a vector editing control map.addControl(new OpenLayers.Control.EditingToolbar(vector_layer)); 7. Finally, we just need to add the layers to the map. map.addLayers([google_streets, wms_layer, vector_layer]); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Interacting with Third Party APIs [ 126 ] 8. Open up your map now. You can add points and polygons to the vector layer via the EditingToolbar control in the top right corner: What Just Happened? We just created a map using Google Maps, WMS, and Vector layers. We also have an EditingToolbar control hooked up to the vector layer object, which will let us create points and polygons in the vector layer. Any points or polygons (also called features) we create will be in the map's projection, EPSG:900913, and line up with the Google Map layer below it. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 5 [ 127 ] WMS with Spherical Mercator/third party map layers We also have a WMS layer, set to be an overlay layer. To use a WMS service on top of a Google Maps layer (or any other third party map layer), the WMS service must support the EPSG:900913 projection. The WMS service we used, http://vmap0.tiles.osgeo.org/ wms/vmap0, does support this projection so the map tile images properly lined up with the Google Maps layer. If a WMS service does not support the EPSG:900913 projection, the map tile images would not properly line up. To find out what projections a WMS service supports, you can make a getCapabilities request to the server. To make this request, specify the request, service, and version properties in the URL. For example, http://wmsserver/?request=GetCapabilities&service=WMS& version=1.1.1. Summary In this chapter, we talked about what third party mapping APIs are and how to use them. We learned how to use the Google, Yahoo!, and Microsoft mapping APIs, along with the OpenStreetMaps API. We also discussed Spherical Mercator and demonstrated how to use it. Finally, we created a map 'mashup', mixing various different layer types together. The next chapter will focus on another part of OpenLayers—the Control class. This is another thing that sets OpenLayers apart from other mapping libraries. With it, we're able to interact with our map, add tons of additional functionality, and even create our own custom controls. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 6Taking Control of Controls So far, we've taken for granted that the map will move when we drag it. Or that we can just click a layer in the layer switcher and it will turn on or off. Or that we can zoom in and out. We haven't yet talked much about what actually is behind the map interaction or how it works. Simply put, the OpenLayers Control class is what makes our maps interactive. There are many built in controls, each with their own unique functions. But, not only do you have a large number of pre-made controls at your disposal, you also have the ability to easily create your own controls. In this chapter, we'll cover: ‹‹ What controls are ‹‹ Adding controls to a map ‹‹ The Control class and its subclasses ‹‹ Using panels to add controls outside of the map ‹‹ Creating custom controls This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 130 ] What are controls? Controls allow us to interact with our map. They also allow us to display extra information, such as displaying a scale bar with the ScaleLine control. Some controls do not have a visual appearance, such as the ArgParser control, but others do, such as the OverviewMap control. You can have as many controls on your map as you'd like. There are even some cases where you may not want any controls—such as embedding an unmovable map in a page, or showing a static map for printing. Using controls in OpenLayers Most controls are added directly to the map, such as the Navigation control. Others are added to the map, but can also be placed in a <div> tag outside the map—like the overview map control. Other controls act similar to buttons which can be clicked or toggled on and off. These types of controls can be placed inside of a panel, which can also be placed outside the map. Furthermore, we can even create our own controls and place them directly in the map or inside of a panel. We'll cover all this and more, so let's get started with how to add controls to your map. Adding controls to your map There are two methods for adding controls to a map. 1. Pass in a JavaScript array of OpenLayers Control objects when you instantiate the map object. 2. Add controls to the map object after it has been created by calling either of the two map functions addControl(), passing in a single control object, or addControls(), passing in an array of control objects. When you create your map, four control objects are added automatically. These four controls are: ‹‹ OpenLayers.Control.Navigation: Responsible for handling map interaction from mouse events, such as dragging the mouse, zooming with the scroll bar, and double-clicking. ‹‹ OpenLayers.Control.PanZoom: Adds a Pan/Zoom bar to the top left hand side of the map. Contains up/down/left/right arrows for panning, and zoom in, zoom out, and zoom to maximum map extent buttons for zooming. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 131 ] ‹‹ OpenLayers.Control.ArgParser: Allows the map to zoom to a specific location and turn on/off layers based on the URL that you call the map with. In other words, when you use the OpenLayers.Control.Permalink control to generate a URL, the ArgParser control will parse the URL and manipulate the map based on the parameters embedded in the URL. We'll cover both controls in detail later in this chapter. ‹‹ OpenLayers.Control.Attribution: This control will add attributions from layers to the map if any attributions are passed into the layer object. Since these controls are added without us explicitly adding them, how do we opt to not include them? The simplest way is to pass in an empty array to the controls property when we instantiate a map object. Time for Action – creating a map with no controls Without controls, our map will not be interactive. This is sometimes exactly what you want, such as when displaying a map on a printer friendly page. 1. First, we'll need to create our map object and pass in an empty array to the controls property: map = new OpenLayers.Map('map_element', { controls: [] }); 2. Now, no controls will be added to the map unless you manually call addControl or addControls. Let's create and add a WMS layer to our map: var wms_layer = new OpenLayers.Layer.WMS( 'WMS Layer Title', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {} ); map.addLayer(wms_layer); 3. Make sure you also set the extent—as stated in previous chapters, this step will be implied in all future examples: if(!map.getCenter()){ map.zoomToMaxExtent(); } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 132 ] 4. Open up the page. You should see a map with no controls, and you won't be able to pan around or zoom in since the map has no controls, as shown below: What Just Happened? We just created a map without any controls. Users cannot interact with the map—they can see it, but they cannot navigate or change it. By passing in an empty array when we instantiated our map object via controls: [], we overrode the four controls that are added to the map by default. Time for Action – adding controls to a map There are tons of other controls that OpenLayers provides that aren't passed in by default but are still very useful. Let's take a look at some of them and how to add them to our map. After we cover how to do this, we'll go over each Control class in more detail. 1. What we'll do now is create an array of control objects that we'll pass in when creating our map object. Let's create a NavigationControl object first. This way, we'll be able to reference the control anywhere in our code easily. Next, we'll create a JavaScript array that will contain the navigation_control object we created; along with four other control objects we'll immediately instantiate. So, the first thing we'll need to do inside our init() function is to create an array of control objects: var navigation_control = new OpenLayers.Control.Navigation({}); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 133 ] var controls_array = [ navigation_control, new OpenLayers.Control.PanZoomBar({}), new OpenLayers.Control.LayerSwitcher({}), new OpenLayers.Control.Permalink(), new OpenLayers.Control.MousePosition({}) ]; 2. Now we'll create the map object and pass in the array of controls we just made: map = new OpenLayers.Map('map_element', { controls: controls_array }); 3. Create and add a WMS layer like in the previous example. If I ask you to create a WMS layer in future examples in this chapter, I'll be referring to this block of code. var wms_layer = new OpenLayers.Layer.WMS( 'WMS Layer Title', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {} ); map.addLayer(wms_layer); 4. Passing in an array of control objects when creating our map object is one way to add controls, but as you know we can also call addControl or addControls. Let's call addControl and pass in an object that will be instantiated on the fly—we won't create a variable to reference it. map.addControl(new OpenLayers.Control.ScaleLine()); 5. Let's make an overview map control object. We'll instantiate it and save a reference to it by using a variable, then add it to the map by passing it into an array and calling addControls. We'll also pass in a KeyboardDefaults control which we will instantiate in the addControls itself. Both ways work—passing in a reference to a control object, or instantiating an object in the method call: var overview_map = new OpenLayers.Control.OverviewMap(); map.addControls([ overview_map, new OpenLayers.Control.KeyboardDefaults() ]); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 134 ] 6. Open up the page. You should see something similar to this: What Just Happened? We added various controls to our map by passing in an array of control objects when constructing the map object, and by calling map.addControl and map.addControls. These two functions take in control object(s) and add them to the map, which also causes the map.control array to get updated. Have a go hero – add controls Add and remove various controls and see if you can determine which controls are which. Take a look at the Control class documentation at http://dev.openlayers.org/docs/ files/OpenLayers/Control-js.html to see a list of all available control classes (we'll cover each one soon) and try adding different controls to your map. Use map.removeControl() and pass in a control object you wish to remove. You can either use the variable name, or a less eloquent method of accessing the controls array itself, e.g.: map.removeControls(map.controls[2]); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 135 ] Doing it this way should make it clear why creating variables is a much better way to instantiate control/layer objects, as it makes it much easier to access the right object. Another downside to using the controls array itself is that removing a control will affect the index of all other controls, so there can be a lot of guesswork involved as to what control you're actually removing. So, in short, avoid removing controls this way if you can. Adding controls by passing in an array of controls The first way to specify controls for your map is to pass them in as an array when the map object is instantiated. Passing in an array of control objects when creating your map object is the preferred way to add controls—it keeps all your control objects in one place, making your code a little easier to read and maintain. In the above example, we created an array that held five control objects, four of which were instantiated in the array itself: var navigation_control = new OpenLayers.Control.Navigation({}); var controls_array = [ navigation_control, new OpenLayers.Control.PanZoomBar({}), new OpenLayers.Control.LayerSwitcher({}), new OpenLayers.Control.Permalink(), new OpenLayers.Control.MousePosition({}) ]; As we've mentioned before, it's best to create a reference to an object (be it a control or a layer object) before adding it to your map. We created the navigation_control object outside an array, so we can easily access it just by referring to it. To access the other controls, we'd have to either call controls_array[index] or map. controls[index], where index is the index of the control you wish to access. You should try to avoid this method if possible, as the array may change, throwing off the index. Furthermore, accessing controls by index like this affects your code's readability, as it's not immediately obvious which control object you're trying to access. Adding controls to map with addControl() and addControls() There are cases where you might want to dynamically add controls to your map after it has been created. To do so, we can use either the addControl() method of our map object and pass in a single control object, or we can use addControls() and pass in an array of objects. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 136 ] In the example above, we called addControl and passed in a ScaleLine control object, which we instantiated on the fly: map.addControl(new OpenLayers.Control.ScaleLine({})); We also used addControls by passing in a control object that has already been instantiated and an object which we created in the function call itself: var overview_map = new OpenLayers.Control.OverviewMap(); map.addControls([ overview_map, new OpenLayers.Control.KeyboardDefaults() ]); Whichever method of adding controls you use, you should try to keep all your control related code together. The bulk of the time you spend programming will be spent in reading your code and not writing it. Your code will be much easier for you to read and maintain if you organize it. There is no 'right' way to go about organizing your code, but keeping related things (like code blocks related to controls) grouped together is a good rule of thumb. Removing controls So, we know how to add controls, but how about removing them? There are two common ways to do this: ‹‹ Call map.removeControl(map.controls[x]); where x is the index of the control you wish to remove. As we stated before, removing controls this way is discouraged because it is not clear which control is actually being removed. ‹‹ Another way is to call map.removeControl(control_object); where control_object is a reference to a control object you've already instantiated. Be sure to keep in mind, if you define an object with var inside a function, you won't be able to access it outside the function. Now that we're familiar with how to add and remove controls, let's get into the Control class itself and take a look at various different types of controls. OpenLayers.Control class As we saw in the previous example, there are quite a few controls available to us. The Control class is similar in nature to the Layer class—there is a base Control class which all other control subclasses inherit from. Because most controls provide completely different functionality from one another, the base Control class does not contain a lot of properties. The official, up to date documentation for this class can be found at http://dev. openlayers.org/docs/files/OpenLayers/Control-js.html. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 137 ] OpenLayers.Control properties Like properties of other classes, you can specify these properties when instantiating your control object. There are a few properties of the base OpenLayers.Control class, which are inherited by all other Control classes: Name Type Description autoActivate {Boolean} Set the control to activate when added to the map. Default value is false. div {HTML Element} Many controls support the ability to be placed in a div outside the map. (This does not work for all controls, however.) An example call would look like: {div: document.getElementById('my_control_ div') } id {String} This specifies the ID of the HTML element that will be assigned to the control. eventListeners {Object} This object specifies event listeners to register. Events are covered later in this chapter. displayClass {String} Specifies the CSS class that the control will receive. By default, controls receive a class name made up of OlControl plus the control name, e.g. OlControlNavToolBar. We cover this in more detail in Chapter 7 on styling controls. title {String} The title you set here will be displayed in a tooltip (or other places, when appropriate). type {Number} Specifies the type of control, which is related to how the control functions. Used in various cases, such as when working with buttons or panels. We cover this later in the chapter, on the section on panels. OpenLayers.Control functions There are a few functions that all control subclasses inherit that we should take a look at. Name Description destroy() This will perform any clean up actions (such as removing any events attached to the control) and then remove the control from the map. Since the control is removed from the map, it also will no longer show up when accessing map.controls. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 138 ] Name Description moveTo(location) This function takes in a single parameter, location, an OpenLayers.Pixel object, and will move the control to the passed in pixel coordinate (in the order of x coordinate, y coordinate). This function will only really affect controls which have a visible component. An example call might be: control_object. moveTo(new OpenLayers.Pixel(200,100)); which would move the control to the x, y, position of 200, 100 inside of the map. The origin is the top left of the map, so it would move 200 pixels to the right and 100 pixels down. activate() Calling this function will activate the control and its event handler (if one is set). Returns true if control was successfully activated and false if it was already active. deactivate() Deactivates the control and any associated event handlers. The behavior of this will vary among controls. Returns true if control was successfully deactivated and false if it was already inactive. draw() This function initializes the control. You will rarely have to call this, as it is usually called automatically. There may, however, be cases where you may want to issue the call manually. It returns a {DOMElement} which is a reference to the div containing the control (if one exists). OpenLayers.Control subclasses OpenLayers provides a lot of different types of controls, and an up to date list can always be found at http://dev.openlayers.org/docs/files/OpenLayers/Control-js. html. Some controls are deprecated, and should not be used, so we will not cover controls that will not be supported in OpenLayers version 3.0 or greater. Some properties and functions of some controls are mainly for advanced users and are outside the scope of this book, so we won't cover them all. Some other controls are used in accordance only with the Vector Layer class—so we'll get to those in Chapter 9. However, that still leaves us a lot to cover here now, so let's get started! OpenLayers.Control.ArgParser As we mentioned earlier, this is a control added to the map by default. It is aptly named; it parses the arguments passed in via the URL. For example, when appending ?zoom=0&lat=0&lon=0&layers=B to the URL of your map, the ArgParser control will parse the variables (i.e., zoom=0, lat=0, lon=0, and layers=B). This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 139 ] This control will then manipulate the map based on the parameters—it would zoom the map to level 0, set the lat and lon to 0, and turn on a certain layer. The ArgParser control does not handle the generation of these URL parameters. It is usually used in accordance with the Permalink control. The Permalink control, as we'll see soon, is the control that actually generates the URL. Example Call: var argparser_control = new OpenLayers.Control.ArgParser(); OpenLayers.Control.Permalink The Permalink control will create an HTML link element (an anchor, an <a>, element) at the bottom right of the map. The link has a URL embedded with the current map's longitude, latitude, zoom values, and layer information (e.g., which layers are turned on). When visiting a page from one of these generated URLs, the URL is read in by the ArgParser control we just discussed. Anytime the map is updated (zooming, panning, turning layers on/off, etc.) the permalink URL changes to reflect the map's current state. Example Call: var permalink_control = new OpenLayers.Control.Permalink(); When using the Permalink control, an ArgParser control will be automatically added to the map. OpenLayers.Control.Attribution This is another control added to your map by default. It will display layer attributions on the map (if you specify the attribution property for the layers). A layer attribution is just some string that will be displayed on your map with this control. You can also specify the separator by setting the separator property, which will separate the attributions for each layer. For this control to be useful, you must specify the attribution property of layers that you want to show attributions for. Attribution properties Name Type Description Default Value separator {String} Separator used between layer attribution text. ', ' div {HTML Element} HTML Element to place this control in. Example: document.getElementById('map_ control_div'); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 140 ] Time for Action – using attributions Let's take a look at the attribution control in action. 1. To make the Attribution control useful, we must specify an attribution property on our layer objects. So, let's create a WMS layer with this property set to 'Base WMS Layer': var wms_base = new OpenLayers.Layer.WMS( 'OpenLayers WMS', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {attribution: 'Base WMS layer'} ); 2. Now, let's add another layer and set the attribution text to 'State Boundary'. var wms_state_lines = new OpenLayers.Layer.WMS( 'State Line Layer', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'stateboundary'}, {attribution: 'State Boundary', isBaseLayer: false, opacity: .2} ); 3. Finally, add the layers to the map: map.addLayers([wms_base, wms_state_lines]); 4. You should see something like the following, with the layer attribution text at the bottom right hand of the map: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 141 ] What Just Happened? You just used the attribution control with two layers containing an attribution property. The text on the bottom right of the map shows the current active layers' attribution text. Let's continue going over more of the built in control classes. OpenLayers.Control.EditingToolbar The EditingToolbar control is a panel composed of tools that allow drawing and editing of vector features. It required a vector layer to be present in your map. We will cover this control, and how to use it, in much detail in Chapter 9. OpenLayers.Control.Graticule This will display lines of longitude and latitude (or whichever measurements your projection is) on your map. Unlike some of the other controls, this control has quite a few properties that we can set to change the way it behaves. This is what our map looks like after adding the Graticule control with the default parameters: Let's go over the properties of the Graticule control. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 142 ] Graticule properties Name Type Description Default Value intervals {Float {Array}} An array of floats of possible graticule widths, in degrees. The different widths will be used depending on the current map zoom levels. Example: intervals: [50,30,15,7,4,2,1,.5] [45,30,20, 10,5,2,1,0 .5,0.2,0.1 ,0.05,0.01 ,0.005,0.0 02,0.001] displayInLayerSwitcher {Boolean} If set to true, the graticule control will be displayed in the layer switcher. true visible {Boolean} Determines if the graticule control should be visible or not. true numPoints {Integer} Specifies the number of points to use in each of the graticule lines. 50 targetSize {Integer} Determines the maximum size of the grid in pixels on the map. 200 layerName {String} Specifies the layer name that will be displayed in the LayerSwitcher control. null labeled {Boolean} Determines if the graticule lines should be labeled. true labelFormat {String} The format of the coordinates the labels will display in. Possible values, for example, would be'd', 'dm', or 'dms', which represent 'degree', 'minutes', and 'seconds', which determine how precise the coordinates will be displayed in. 'dm' lineSymbolizer {Symbolizer} Specifies the symbolizer to be used to render the lines. A symbolizer specifies style information. In Chapter 10, we will discuss symbolizers and how to use them in detail. labelSymbolizer {Symbolizer} The symbolizer used to render labels, which controls the label style. Chapter 10 discusses symbolizers (objects that define style) in detail. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 143 ] OpenLayers.Control.KeyboardDefaults This control adds panning and zooming functionality, controlled by the keyboard. By pressing the arrow keys, plus/minus keys, or home keys, the map will be moved or zoomed. There is one relevant property we should take a look at. KeyboardDefaults properties Name Type Description Default Value slideFactor {Integer} Sets the amount of pixels to slide the map by. 75 OpenLayers.Control.LayerSwitcher This control will create a layer list that allows you to turn on/off map layers. By default, it creates a toggle-able window in the map, but you can pass in a div HTML element and the layer list will be created in it. LayerSwitcher properties Name Type Description Default Value ascending {Boolean} Changes the order the layers are displayed. true roundedCorner {Boolean} Specifies if rounded corners should be created for the layer switcher window. true div {HTML Element} If specified, the HTML Element to place this control in. If you use this, you should set the roundedCorner property to false, otherwise, rounded corners will be created inside the target div. Example: document. getElementById('map_ control_div'); null This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 144 ] LayerSwitcher functions There are a couple of functions we can manually call to show or hide the LayerSwitcher control. ‹‹ minimizeControl(): When called, it will minimize the layer switcher control. Especially useful if you want to programmatically hide the layer switcher control. ‹‹ maximizeControl(): Shows the layer switcher control; it essentially does the opposite of mimizeControl(). OpenLayers.Control.MousePosition This control displays the longitude and latitude of where the mouse is currently located. By default, the control displays the position on the bottom right hand side of the map. MousePosition properties Name Type Description Default Value prefix {String} The string that will be added to the beginning of the coordinate text. '' suffix {String} The string that will be added after the coordinate text. '' emptyString {String} Text that will be displayed instead of the coordinates if the mouse is moved outside of the map. By default, when the mouse is moved outside of the map the last known coordinates will remain displayed. null separator {String} The string that will be placed in between the longitude and latitude. ',' numDigits {Integer} Specifies the numbers of digits after the decimal point (i.e., precision), of the coordinate displayed. 5 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 145 ] Name Type Description Default Value displayProjection {OpenLayers. Projection} Controls what projection the coordinates will be displayed in. Any projection other than EPSG:4326 or EPSG:900913 will require the Proj4js library to properly re-project the coordinates. If this property is not set, the map's projection will be used. div {HTML Element} If specified, the HTML Element to place this control in. OpenLayers.Control.Navigation The Navigation control is one of the four controls added by default to your map. This control handles map navigation via mouse input, such as dragging the map and scrolling to zoom in. Let's take a look at a couple of the properties. Navigation properties Name Type Description Default Value documentDrag {Boolean} Determines whether the map should be allowed to be dragged when the mouse exits the map viewport (i.e., the map HTML div element). false handleRightClicks {Boolean} If set to turn, the map will intercept right clicks. On a double right click, the map will zoom out. false zoomWheel {Boolean} Specifies if the mouse scroll wheel will make the map zoom in. true autoActivate {Boolean} Determines whether the control should be activated when it is added to the map. It can be useful when adding this control to a panel. true This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 146 ] OpenLayers.Control.NavigationHistory The NavigationHistory control stores information about, as you could guess, map navigation. It is actually a control that creates two children controls called next and previous. You do not need to access these child controls directly, you should usually only use the methods of the NavigationHistory control object itself. By calling the nextTrigger() or previousTrigger() functions, you can go forwards or backwards (if possible) through your navigation history. NavigationHistory properties Name Type Description Default Value limit {Integer} Specifies the limit of history states to store. 50 nextStack {Array} Contains an array of next history states. Not used when instantiating an object. previousStack {Array} Contains an array of previous history states. Not used when instantiating an object. NavigationHistory functions There are two functions we can call to access the navigation history. ‹‹ nextTrigger(): This function will call the child next control's trigger() function (which can also be called via nav_history_control.next.trigger()). If there is a next position, it will move the map to the next position on the position stack. ‹‹ previousTrigger(): This function will call the child previous control's trigger() function (which can also be called via nav_history_control.previous. trigger()). If there is a previous position, it will move the map to the previous position on the position stack. Time for Action – using the NavigationHistory control Let's use the NavigationHistory control's nextTrigger and previousTrigger methods to go through our navigation history. 1. Open up the previous examples and enable Firebug. We're going to create a NavigationHistory control object and add it to the map via the console. Enter and run the following code: var nav_history_control = new OpenLayers.Control.NavigationHistory(); map.addControl(nav_history_control); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 147 ] 2. Pan and zoom around the map. Each time you update the map, the NavigationHistory control stores information about your actions. Then, in Firebug, issue the following command: nav_history_control.previousTrigger(); 3. You should see the map return to the previous zoom level and coordinate. Now, let's return to where we were by calling: nav_history_control.nextTrigger(); What Just Happened? We just created and added a NavigationHistory control to the map, then called previousTrigger() and nextTigger() methods. OpenLayers.Control.NavToolbar OpenLayer's NavToolbar control creates a toolbar of navigation control buttons. It contains two buttons by default—one for panning, and one for zooming. The panning mode allows you to pan around the map like in the Navigation control; while the zoom control lets you select an extent to zoom to. This control comes in handy especially if you wish to provide additional custom functionality to your map. OpenLayers.Control.OverviewMap The OverviewMap control creates a toggle-able window at the bottom right side of your map that displays a smaller, zoomed out map containing a draggable rectangle that represents your current position on the map (i.e., an overview map). As you zoom in on the map itself, the overview map will update accordingly (although, you can turn this behavior off by setting the numZoomLevels property in the mapOptions property). This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 148 ] It's likely that you're familiar with this tool, and many people expect it to be on a web map as Google, Bing, etc. all have similar controls—so including it may be a good idea, depending on your application. There are a few configuration options we should cover. OverviewMap properties Name Type Description Default Value size {OpenLayers. Size} Sets the size of the overview map, in pixels, in the format of width, height. An example call would be: size: new OpenLayers. Size(300,300). This will increase the size of the overview map itself, but the actual div element the overview map resides in is specified with CSS. new OpenLayers. Size(180, 90) layers {Array {OpenLayers. Layer}} Specifies an array of layers from the map to be used in the overview map. If none are added here, by default the base layer will be used in the overview map. Uses the map base layer by default. minRectSize {Integer} This property determines what the minimum width or height, in pixels, of the extent rectangle can be before it will be replaced with the value of the minRectDisplayClass. The larger the number, the bigger the extent rectangle will be before it gets replaced. This property can be used in accordance with the minRatio and maxRatio properties to provide a more custom behavior of the overview map control. 15 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 149 ] Name Type Description Default Value minRect DisplayClass {String} This specifies the name of the class that the extent rectangle will use when the minRectSize value is reached. We'll talk about control styling in much more detail in the next chapter on Styling Map Controls. 'RectReplacement' minRatio {Float} This property will help determine when to zoom out the overview map. This ratio is calculated by dividing the overview map's resolution with the base map's resolution. The default value is 8. This means that if the resolution of the overview map is less than 8 times the resolution of your main map, the overview map will zoom out. 8 maxRatio {Float} This property helps in determining when to zoom the overview map in. The same principle applies here as above. The default value is 32, meaning, if the resolution of the overview map is greater than 32 times the resolution of the main map, the overview map will zoom in. 32 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 150 ] Name Type Description Default Value mapOptions {Object} This is an object just like the options property of the map object. It is an anonymous object that can contain the same options the map can contain. Setting some of these options can be very useful, especially the numZoomLevels property. For example, mapOptions: {numZoomLevels: 1} will cause the overview map never to zoom in or out, staying at the max extent. {} autoPan {Boolean} This property determines whether to automatically pan the overview map to keep the extent rectangle in the middle. Setting this value to true will mimic the behavior of the overview map in Google Maps. false div {HTML Element} If specified, the HTML Element to place this control in. If not specified, the OverviewMap control goes in the bottom right hand of the map. OverviewMap functions ‹‹ minimizeControl(): When called, it will minimize the layer switcher control. It is especially useful if you want to programmatically hide the layer switcher control. ‹‹ maximizeControl(): It shows the layer switcher control, it essentially does the opposite of minimizeControl(). OpenLayers.Control.PanPanel This control adds a pan panel to the top left hand side of the map. It contains four images: up, left, right, and down arrows. Clicking on an arrow will pan the map in the arrow's direction. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 151 ] PanPanel properties Name Type Description Default Value slideFactor {Integer} Similar to the KeyboardDefault control, it sets the amount of pixels to slide by. 50 OpenLayers.Control.PanZoom This is another one of the four controls added to your map by default. It adds a PanPanel and ZoomPanel control to the top left side of the map. It contains the slideFactor property that the PanPanel has. Other than this property, there are no more properties to cover for this control. OpenLayers.Control.PanZoomBar This control is similar to the PanZoom control; however, it also adds a zoom bar with a slider which you can move up and down to specify the zoom level. There are just a couple properties we'll take a look at. PanZoomBar properties Name Type Description Default Value zoomWorldIcon {Boolean} Specifies whether the zoom to max extent world icon should be displayed. If set to false, the icon will be hidden. true div {HTML Element} If specified, the HTML Element to place this control in. OpenLayers.Control.Scale When using this control, your map will display the current map scale as a ratio (for example Scale = 1:443M) placed in the bottom right hand side of the map by default. There is only one relevant property we should take a look at. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 152 ] Scale properties Name Type Description Default Value geodesic {Boolean} Determines if geodesic measurement should be used. This value should be changed to true if your map's projection is EPSG:900913. When set to true, the scale is calculated from the horizontal size of the pixel to the center of the map. false OpenLayers.Control.ScaleLine The ScaleLine control adds a line representing scale to the bottom left hand side of your map. It will show the scale using both km and mi, or m and ft when zoomed in (by default). By specifying different property values, we can change this behavior; let's take a look. ScaleLine properties Name Type Description Default Value maxWidth {Integer} Controls the maximum width of the scale line, in pixels. 100 topOutUnits {String} Specifies the units to zoom on the top bar of the scale line when the map is zoomed out. km (kilometers) topInUnits {String} Specifies the units to zoom on the top bar of the scale line when the map is zoomed in. m (meters) bottomOutUnits {String} Specifies the units to zoom on the bottom bar of the scale line when the map is zoomed out. mi (miles) bottomInUnits {String} Specifies the units to zoom on the bottom bar of the scale line when the map is zoomed in. ft (feet) geodesic {Boolean} Determines if geodesic measurement should be used. This value should be changed to true if your map's projection is EPSG:900913. false div {HTML Element} If specified, the HTML Element to place this control in. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 153 ] OpenLayers.Control.ZoomPanel The ZoomPanel control will add a zoom panel to the top left of the map. It contains ZoomIn, ZoomOut, and ZoomToMaxExtent controls which are activated by clicking on the corresponding icons. It is part of the PanZoom control that is added to the map by default. That's it for our overview on the available built in controls! Let's move on to another related topic—Panels. Panels So far, we've covered the most common controls, ones that we'll be using throughout the book. There is another type of control that we have not yet discussed—the panel control. It is a container, a control that allows us to add and group together other controls inside of a (you guessed it) panel. We have, in fact, encountered the panel control to some degree with a few of the controls we've discussed. The PanPanel and ZoomPanel controls are actually panels which contain other controls. Control types With a panel, each control is represented by an icon (which can be styled anyway you wish). When the icon is clicked, the activateControl method of that control is called. Remembering to call activateControl is very important—most controls will not activate by themselves (we'll cover this in more detail soon). Some of the controls we've encountered can be placed inside a panel, as we'll see in a moment. Each control in a panel has one of the three 'types': ‹‹ OpenLayers.Control.TYPE_BUTTON: This is a button type control, which acts like a button—when it is clicked, some event gets fired off. An example control of this type is the ZoomIn and ZoomOut buttons we encountered in the ZoomPanel Control. ‹‹ OpenLayers.Control.TYPE_TOGGLE: Controls like this are activated by a click and turned off by another click. Turning on one of these controls will not affect other controls, and you can have as many toggle controls as you would like in the same panel. ‹‹ OpenLayers.Control.TYPE_TOOL: This type of control is like the toggle control above, but only one tool type control can be active at a time. For instance, the ZoomBox control is one of the controls of this type. Let's take a look at how to create a panel and see how we add controls to it. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 154 ] Time for Action – using Panels In this example, we're going to demonstrate how to create and use the Panel class. We'll create a panel control object that will be placed in a div outside the map, and then add control objects to the panel. We're going to place the panel in a div element outside of the map. We'll first create an HTML <div> element to contain our panel. Add a new div after the map div like this: <div id='panel_div'></div> 1. Now, we need to create a panel control object. We're going to first create a Navigation control object, as it will act as the 'default control' for the panel, which means it will be the control activated by default. Then, we'll create our panel object and place it inside of the previously created panel_div HTML element. Add a WMS layer to the init() function and place the following code after the map. addLayer() call. var navigation_control = new OpenLayers.Control.Navigation(); var control_panel = new OpenLayers.Control.Panel({ div: document.getElementById('panel_div'), defaultControl: navigation_control }); 2. Let's add some controls to it. The Panel control has a method called addControls which takes in an array of control objects. Let's add a ZoomBox and ZoomToMaxExtent control. Take note that we pass in navigation_control, an already instantiated object, instead of instantiating a Navigation control object on the fly. This navigation_control object is the same control object that the defaultControl property points to. If we just instantiated a Navigation control object here, the defaultControl property would be pointing to a different control object. Creating the navigation control object outside this array ensures that we use the same object. control_panel.addControls([ navigation_control, new OpenLayers.Control.ZoomBox(), new OpenLayers.Control.ZoomToMaxExtent() ]) This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 155 ] 3. Now that we have created our panel, we just need to add it to the map. The panel control has a property called autoActivate which will activate the panel control when it is added to the map. By default, it is set to true. Panels must be activated to be used, so if you set this property to false you must call the panel's activate() method. Similarly, if you wanted a control other than the defaultControl to be activated by default, you would call the panels' activateControl() method and pass in the desired control. map.addControl(control_panel); 4. There's just one more thing left to do. If you open up the page now, you probably won't see anything in the panel. This is because the controls do not yet have any style associated with them. We'll fully cover the rationale behind this, along with how to customize control styles, in the next chapter—but for now, just put the following CSS <style> tag in the <head> section of your page (outside of the <script> tag): <style type='text/css'> /*Navigation Control*/ #panel_div .olControlNavigationItemActive { background: #226699 url('http://dev.openlayers.org/releases/ OpenLayers-2.9.1/theme/default/img/pan_on.png'); width: 22px; height: 22px; } #panel_div .olControlNavigationItemInactive { background: #996622 url('http://dev.openlayers.org/releases/ OpenLayers-2.9.1/theme/default/img/pan_off.png'); width: 22px; height: 22px; } /*Zoom Box Control*/ #panel_div .olControlZoomBoxItemInactive { width: 22px; height: 22px; background:#999933 url('http://dev.openlayers.org/releases/ OpenLayers-2.9.1/img/drag-rectangle-off.png'); } #panel_div .olControlZoomBoxItemActive { width: 22px; height: 22px; background:#999966 url('http://dev.openlayers.org/releases/ OpenLayers-2.9.1/img/drag-rectangle-on.png'); } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 156 ] /*Zoom to Max Extent Control*/ #panel_div .olControlZoomToMaxExtentItemInactive { width: 18px; height: 18px; background:#333399 url('http://dev.openlayers.org/releases/ OpenLayers-2.9.1/img/zoom-world-mini.png'); } </style> 5. Open up the page now and you should see something like the following: What Just Happened? We just created a panel and placed three controls inside. By clicking on one of the buttons in the panel, you activate the control (specifically, the activateControl method is called). When creating controls inside the panels that you wish to activate programmatically, you must call the activateControl method of the panel, passing in the desired control object. If you click on the ZoomBox control button, you'll notice that the Navigation control gets deactivated—only one of these controls can be active at a time. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 157 ] Since the panel control is a control object that gets added to the map, the panel control is available in the map.controls array. The same is true for controls that are added to panels. Pop Quiz – zoomBox control type Which type of control is the ZoomBox Control? 1. TYPE_BUTTON 2. TYPE_TOGGLE 3. TYPE_TOOL 4. The Navigation Control does not have a control type 5. TYPE_I_DONT_KNOW Hint: If you're not sure, take a look at the ZoomBox control object's type property. OpenLayers.Control.Panel As we saw in the previous example, we're able to create a panel and place the panel inside of a <div> element outside the map. If we do not specify the div property, the panel will be added directly to the map. The panel we made was pretty simple, but even creating more advanced panels isn't too difficult. Before we start getting too fancy though, let's go over the properties and methods of the panel class so we know what we're working with. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 158 ] Panel properties Name Type Description Default Value controls {Array {OpenLayers. Control}} Specifies an array of control objects that get added to the panel. As in the previous example, you can either instantiate objects in the call itself (via new OpenLayers.Control) or use a previously created object. When you create a panel, you need to either use this property to specify the controls the panel receives, or use the panel's addControls method. Example: controls: [new OpenLayers.Control.Navigation(), new OpenLayers.Control. ZoomBox()] [] autoActivate {Boolean} Determines if the panel should be activated when it is added to the map. If this is not set to true, you must call the panel's activate() method before you use the panel. true defaultControl {OpenLayers. Control} Specifies the default control to be activated. This should point to a control that the panel will or does own. Like in the example above, you should pass in a control object that has already been instantiated. If you instantiate an object when specifying the value, instead of passing in an already created one, nothing will happen because the default control would be pointing to a different object than one owned by the panel. Example: defaultControl: my_ control_object null saveState {Boolean} If this property is true, then the active state of the controls inside the panel (i.e., their state) will be saved if the panel is deactivated, and restored if it is activated. It will also override the defaultControl property (if set) after the first activation, restoring whatever controls were active instead. false This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 159 ] Panel functions The Panel class has just two functions that we need to know about for now. ‹‹ Activate(): This function will activate the panel control itself. By default, the autoActivate property is set to true and this function will be called automatically. However, if the panel is not activated by default, you must call this function to use the panel. ‹‹ activateControl( {OpenLayers.Control} ): This function will activate the passed in control. The control passed in must be a control that belongs to the panel. This is the function that gets called when a control inside the panel is clicked on by a user. If you wish to manually activate a control, you will call this function. Now what? Whew! We've covered the majority of controls OpenLayers offers and we've gone over how to use a panel. There's been a lot to take in, but don't feel overwhelmed. One of the reasons we covered so much is so that we can refer back to this chapter later if we need a refresher on how to use a control or what we can do with one. So, now that we're done with the nitty gritty details, let's move on to something a little bit more complex. Creating our own controls You're familiar (or perhaps fed up) with control descriptions by now. Let's make things a little more interesting and create our own control. Here's the plan: 1. Quickly talk about the Button Control class. 2. Create a Button control with custom functionality. 3. Add the button to a panel. 4. Viola! You just created your own custom control. So, before we talk about creating a custom button, let's go over the Button subclass that we didn't cover in the section above, which we'll use to create our custom controls. OpenLayers.Control.Button There's not much to cover here really, as we've been over the base control class. There are two properties we need to discuss however—the type and displayClass property. We'll use these two properties when creating our own custom control. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 160 ] Button properties Name Type Description Default Value type {Integer} This is an integer which represents the type of control this button is. This is part of what we discussed earlier—the button types. OpenLayers.Control.TYPE_ BUTTON is represented by a 1, OpenLayers.Contro.lTYPE_ TOGGLE is represented by a 2, and OpenLayers.Control.TYPE_ TOOL is represented by a 3. By default, as this is a OpenLayers.Control. TYPE_BUTTON control, the value is 1. However, by changing the value of this property you can change its control type. You can specify the type by either using the {Integer} number (such as 2), or use the built in constant name. The constant name starts with OpenLayers.Control and is followed by the control type (such as OpenLayers.Control.TYPE_ BUTTON). Both methods accomplish the same thing, but using the constant name is cleaner and easier to read. 1 (or OpenLayers. Control.TYPE_ BUTTON) displayClass {String} This string will specify the name of the CSS class the button will be assigned. An example would be 'olControlMyButton'. Whatever class you specify, OpenLayers automatically adds in 'ItemInactive' at the end of it. If it is a toggle-able button, then 'ItemActive' will be added to the end when the button is active. We'll cover this in great detail in the next chapter. If this property is not defined, it will get the default value of 'olControlButtonItem' (with 'Inactive' or 'Active' added at the end). olControlButton ItemInactive This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 161 ] Button functions There is also only one function we need to go over. One of the main uses of this function is that we usually pass it in when we instantiate the button object. ‹‹ trigger(): This function is called when the control button is clicked (assuming it is an OpenLayers.Control.TYPE_BUTTON control). If we don't define it when creating our button (or define it after the button is created), nothing will happen when we click on our button. When passing in this as a property while creating your button object, a function return code (a function's name) should be passed in and not an actual function. So, to define this on instantiation, create a function outside the new OpenLayers.Control.Button call and then refer to that function's name as the trigger. For example, var my_func = function(){ alert( 'Stop clicking me'); } var my_button = new OpenLayers.Control.Button({ trigger: my_func }); Creating a custom button You know a bit more about the Button class now, so let's put that knowledge to use. We'll create a fairly simple custom button (of control type TYPE_BUTTON) that will update the base layer's opacity and zoom the map to a random spot. We'll cover the few basic things that should be included when creating a button: ‹‹ Specifying the button's displayClass to give it style (optional) ‹‹ Creating a function and setting it as the trigger function ‹‹ Adding the button a panel, and then adding that panel to a map Time for Action – creating a simple button Let's create a simple button that has a CSS class and contains a trigger function that does a little something when we click on it. 1. Create a new page using the map template from Chapter 1. We'll be creating a button and a panel to place the button in (although, this time, the panel will be inside the map). This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 162 ] 2. Before we create our button, let's create a function that will be called when we click on the button via the trigger function. We're actually going to create a variable and assign a function to it—this is one of the things that makes JavaScript pretty powerful. Let's make the function change the map's layer opacity and zoom to a random coordinate. Inside the init() function, add a WMS layer and after the map.addLayer(wms); call, add the following. It will generate a random coordinate, set the map's center to it, and change the WMS layer's opacity: var custom_button_func = function(){ //Get a random coordinate from -90 to 90 var random_lon = Math.floor(Math.random() * 360) - 180; var random_lat = Math.floor(Math.random() * 180) - 90; if(map.layers[0].opacity === 1){ //If the layer opacity is 1 (fully opaque), then change it and zoom map.layers[0].setOpacity(.5); map.setCenter(new OpenLayers.LonLat(random_lon, random_lat), 3); } else{ //If the layer opacity is anything but 1, change it and zoom map.layers[0].setOpacity(1); map.setCenter(new OpenLayers.LonLat(random_lon, random_lat), 3); } }; 3. Now let's create our button control object. We're going to assign it a CSS class and set the trigger to be the custom_button_func we just created. So, when the button is clicked, custom_button_func function will be called. Notice how we just passed in the name of the function (also known as the function's return code) and we don't use parenthesis like we normally do when we call a function. var my_button = new OpenLayers.Control.Button({ displayClass: 'olControlCustomButton', trigger: custom_button_func }); 4. Let's create a panel that will store the newly created button. We won't pass in a div, so it will be added straight into the map. var control_panel = new OpenLayers.Control.Panel({}); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 163 ] 5. We've got our panel made, so now we need to add our custom button to it. Even though we only have one control, we still must call addControls as there is not a singular addControl method of the Panel class. control_panel.addControls([ custom_button ]) 6. Almost there! Now, let's add the panel to the map: map.addControl(control_panel); 7. Now, by default, the panel will appear in the top left corner of the map. We could change this via CSS, but let's stick to what we've covered so far and use the moveTo function, moving the panel 450 pixels to the right (the origin is the top left of the map). control_panel.moveTo(new OpenLayers.Pixel(450,0)); 8. One last thing, if we view the map right now, we wouldn't see our button. This is because we have assigned it a custom CSS class, but we have not yet defined the class. So, let's add the following to the <head> section, before the <script> tags. Even though our displayClass was set to olControlCustomButton, we must define olControlCustomButtonItemInactive, as that's the actual class name OpenLayers generates for the inactive state of the button. <style type='text/css'> /*Custom Button*/ .olControlCustomButtonItemInactive { background:#22dd22; border:5px solid #202020; cursor: pointer; height: 28px; width: 28px; } </style> This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 164 ] 9. OK. Take a look at the map. You should see a big honking ugly button on the top right hand side of the map: What Just Happened? We just created a fairly simple button control that will change the map's opacity and center location when clicked. In the next chapter, we'll talk more about how to use CSS to make it look like a button instead of an ugly green box. Before we finish up this chapter though, let's go over one more thing—creating a custom button that has a different control type than the TYPE_BUTTON control we just made. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 165 ] Other control types So, with the previous example, we created a button control whose type was TYPE_BUTTON. It works like we expect and should work, but what if we wanted it to be toggle-able? We know, from earlier in the chapter, that there are TYPE_TOOL and TYPE_TOGGLE button types, and we mentioned how we could assign a button to be one of those types. Process for creating other button control types While there are a few different ways to achieve this, the process basically boils down to the following: ‹‹ Create a control object (either by extending the base Control class—a more advanced technique, or by creating a Button control) and specify the type ‹‹ Specify functions that will get called when certain events occur (e.g., when the control is activated) To keep things simple and accomplish this, we're going create a Button control like we did in the previous example. Then, we'll specify eventHandlers that will call functions when the control is activated. But what's an event? Events An event basically means what it sounds like—something happening. Really, all user input is an event—a key press, a mouse click, etc. are all events. Using JavaScript, we're able to access user generated events, such as a mouse click, and do things when an event occurs. Events are what drive the interaction in OpenLayers—when you drag the map, you are actually issuing a mouse event that OpenLayers interprets and then updates the map accordingly. OpenLayers has its own Event class which makes interacting with events easier, and even enables us to create our own custom events. We won't get much into that right now, but we should briefly talk about something known as event handlers. Event listeners and handlers If you understood the previous paragraphs, then you may be able to guess what an event handler (and listener) is. In essence, an event listener is something that listens for events, and an event handler is something that responds to an event. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 166 ] Consider the button control we created in the previous example. The control will wait for someone to click on it—this is the event listener. When the button is clicked, the trigger function is called—this is the event handler. So, in OpenLayers, event listeners are used to add event handler functions to certain event types (such as clicks or mouse overs). Custom events While user created events give us plenty to work with, life is easier if we can work with events other than just mouse clicks and key presses. If we can interact with events that are fired off not by the user, but by the map itself, then we can do a whole lot of more interesting and neat things. And of course, we wouldn't be building this concept up if OpenLayers didn't provide us with the ability to do so. While we won't be creating our own custom events here, we will be working with custom events. When you click on a TYPE_TOGGLE control button, a custom event called activate is fired. In turn, if we want to call some function when that happens, we can assign an eventHandler to the activate event. We'll go over exactly this when we create a more advanced custom control. Creating a TYPE_TOGGLE control Now it's time to create some more custom controls with TYPE_TOGGLE and TYPE_TOOL. Two things need to happen for us to do this—we must create the control object, and then we have to assign functions that get called when the control is activated (i.e., an event handler). The previous section on events was used to provide an explanation for the theory behind events, but don't worry if you're a little confused. We're going to clarify, through code, what we will talk about. Let's get to it! Time for Action – creating a custom TYPE_TOGGLE control We're going to create a TYPE_TOGGLE control button. We'll actually be using events in two ways. We'll use events to call a function when the control is activated/deactivated (i.e., toggled). This activated/deactivated concept is important, as we cannot use controls if they are not activated. When the toggle control is activated, the activate() function that gets called will add an event handler to the map. The map event handler will look for a click event and upon a click will call a function which updates the base layer's opacity. When the control is deactivated, the deactivate() function that gets called will remove the map event handler. 1. Open up the previous example. We'll be using that as the basis for this example. We don't really need to change anything there, so let's go ahead and start creating our buttons! This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 167 ] 2. First, we're going to create the function that will randomly change the map's base layer's opacity when the map is clicked. This will only happen when our toggle control is active. Right before the map.addControl(control_panel); line, insert the following: //Function that the map will call when the map is clicked (only when // the toggle button is active though) var map_event_function = function(){ map.layers[0].setOpacity(Math.random()); } 3. Before we create a TYPE_TOGGLE button, we need to create two functions: one that will get called when it is activated and one called when it is deactivated. This is similar to what we did in the previous example. Let's do that, right after the previous code: //Create a function for the toggle button var toggle_button_activate_func = function(){ //Attach the map_event_function to the map map.events.register('click', map, map_event_function); } var toggle_button_deactivate_func = function(){ //Remove the map_event_function from the map map.events.unregister('click', map, map_event_function); //Restore the layer's opacity map.layers[0].setOpacity(1); } 4. Now, we need to create the toggle control itself. The format is similar to before, but we'll be using a couple different properties. Because this is a toggle control, we won't use a trigger function. Instead, we'll add two event listeners—one for the activate event, and another for the deactivate event. We'll also set the type to OpenLayers.Control.TYPE_TOGGLE (the TYPE_TOGGLE type—we could also set the type to 2 to accomplish the same thing, but it's much more readable to use the built in constant name). Add the following on the next line: //Create the toggle button object var custom_toggle_button = new OpenLayers.Control.Button({ displayClass: 'olControlCustomButtonToggle', eventListeners: { 'activate': toggle_button_activate_func, 'deactivate': toggle_button_deactivate_func }, This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 168 ] type: OpenLayers.Control.TYPE_TOGGLE }) 5. You can probably guess the next part—add the control to the panel. Easy enough! control_panel.addControls([custom_toggle_button]); 6. Now, just make sure the control_panel is added to the map: map.addControl(control_panel); 7. Lastly, we just need to add the appropriate CSS classes. We gave our toggle control the class of olControlCustomButtonToggle, so we'll need to add the appropriate classes. Because this is a toggle control, there are actually two CSS classes we need—one for active, and another for inactive. Add the following inside the <style> tag in the <head> section: /*Custom Toggle Button*/ .olControlCustomButtonToggleItemActive { background:#336699; border:5px solid #202020; cursor: pointer; height: 28px; width: 28px; } .olControlCustomButtonToggleItemInactive { background:#003366; border:5px solid #202020; cursor: pointer; height: 28px; width: 28px; } 8. All done! When you click the newly created control, the activate() event is fired, and since we added an event listener for it the appropriate toggle_button_ activate_func is called. It will add an event handler to the map that will wait for a click. When the mouse click event fires, the base layer's opacity will be set to some random value between 0 (fully transparent) and 1 (fully opaque). Then, when the toggle control is clicked again, it will be deactivated and the layer will return to full opaqueness. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 6 [ 169 ] What Just Happened? You probably have a good idea now of how the controls we just made work. Just as a quick recap, the following happens: 1. The custom_toggle_button toggle control waits for the activate or deactivate events to be fired (these are custom OpenLayers events, not general JavaScript events like a mouse click). This is done by registering the events with map.events.register. 2. When the custom_toggle_button control is clicked (which toggles it on), the activate control event is detected and the corresponding toggle_button_ activate_func function gets called. 3. The toggle_button_activate_func function that gets called then adds an event listener to the map (with map.events.register again). 4. This event listener will wait for a click (a mouse click, a general JavaScript event) and then fire off the map_event_function every time a click is received. 5. When the custom_toggle_button control button receives a deactivate event (i.e., it is clicked and toggled off), the corresponding toggle_button_ deactivate_func function is called. 6. The toggle_button_deactivate_func function removes the previously added map event listener with map.events.unregister. Therefore, the listener is gone and mouse clicks on the map will no longer cause the map_event_function to be fired off. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Taking Control of Controls [ 170 ] And that's about it! When using panels and controls inside panels, make sure that the controls get activated via activate() or the panels' activateControl() methods. The control cannot be used if a control is not activated (or cannot be activated by the user by clicking on the control, for example). Summary We covered a good deal in this chapter. We talked about the idea behind controls and how they are used in OpenLayers. We then demonstrated how to add controls to a map. We covered the Control class and its various subclasses in depth. We also went over how to set up panels and how to add controls to them. To finish the chapter, we provided an introduction to events and learned how to create our own custom controls. Even though we've been pretty thorough in this chapter, there's still more to learn about controls—such as how to style them. In Chapter 9, on the Vector layer class, we'll go over some Vector layer specific controls we neglected. This chapter was likely a lot to take in, but the next chapter will be a bit shorter. We'll discuss styling controls using CSS, and we'll make controls that actually look like they should be clicked, not just big green and blue squares. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 7Styling Controls So far, we've seen how we can customize nearly everything offered by OpenLayers—Layers, Controls, etc. However, we haven't been too concerned with the actual appearance of the interface of our maps. OpenLayers provides pretty good looking default controls, so you may not find it necessary to change them. Like nearly everything else in OpenLayers though, changing the style and images used by the Control class is easy. We talked about the Control class in the last chapter, and in this chapter we'll explore how OpenLayers applies styles to controls and how to go about customizing controls. In this chapter we'll: ‹‹ Talk about what CSS is and what purpose it serves ‹‹ Discuss how HTML uses CSS ‹‹ Go over how OpenLayers uses CSS ‹‹ Cover the naming schemes OpenLayers uses for controls ‹‹ Give controls some custom styles Let's dive in! This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 172 ] What is CSS? CSS is an acronym for Cascading Style Sheets. It is a type of markup language used to specify the appearance of HTML elements. CSS is actually quite simple, and if you've been able to follow along up to this point in the book, you will have absolutely no problem at all with CSS. Before we get into it a little more, let's talk a little bit about how CSS works. Ideas behind CSS and HTML HTML, CSS, and JavaScript serve three distinct purposes. ‹‹ HTML is used, as we've seen, to create the structure and content of a webpage ‹‹ CSS, on the other hand, is used to control the site's presentation, or how the page should look ‹‹ JavaScript, as we've seen throughout the book, is used to handle the logic behind the site So, when you make a site you will have at least three discrete things to consider—the HTML behind it, the CSS that styles the HTML, and the JavaScript that handles any logic or user interaction. For now, we'll focus on HTML and CSS. Ideally, HTML, CSS, and JavaScript should not mix (i.e., you shouldn't use style tags, or tags like <center>, in your HTML). Your HTML pages should link to external JavaScript and CSS files, especially in a production environment. One advantage of following this principle is that if you want to change the way your website looks, you only have to edit the CSS in one place (the external file) instead of on every page. The disadvantage is that it is slightly easier and faster to edit things if everything is contained inside the same HTML file; that's why we've been doing it this way so far. But how do we edit the CSS and what does this even mean? Editing CSS If you're familiar with CSS, feel free to skip the next few pages and go directly to the second section on How OpenLayers uses CSS. If you aren't, don't fret! It's quite easy. CSS isn't a programming language; in fact, you already are familiar with how it is structured. Let's look at the 'template' code for applying a CSS style: element { property: value; property-two: value; } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 173 ] Looks familiar? It's essentially what an anonymous object in JavaScript looks like. The main difference is that a semi colon (;) is required at the end of each line (not a comma). You'll notice we also have property: value (key: value pairs). There are many different possible properties and values. We'll cover common ones later in this chapter, but a full list can be found at http://www.w3schools.com/ css/css_reference.asp. The other difference, you'll notice, is that there is something called an element in front of the brackets. This refers to a single (or multiple) HTML element—either by tag name, class name, or ID. Multiple elements are separated by a comma. HTML elements So far, we've talked just a little bit about elements in HTML. Every <tag> in your HTML page is an element (e.g., <div id='my_div'>This div is an element</div>). Now, to use CSS, we have to have a way to refer to the element(s) we want to give a style to. This is the element part of the CSS code outline above. The DOM (Document Object Model) is a way to represent objects (such as these elements) that are in your page. We won't go into much depth about it now, but if you are interested in learning more then http://w3schools. com/dom/default.asp is a great resource. HTML—IDs and classes We can refer to specific elements in two primary ways: via an ID or class name(s). HTML IDs Every HTML element can have a single ID. The ID is unique, and no elements should share the same ID. For example, <div id='map_element'></div> With this code, we're creating a div element with an ID of 'map'—no other element (be it a div or otherwise) should have the same ID. So, IDs are very useful when we want to refer to one specific element. We've used the ID property to specify the div that our maps should appear in (via <div id='map_element'></div> and specifying map_element when instantiating our map object). This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 174 ] HTML classes There is another way to refer to elements, via class names. Classes in HTML have little in common with the classes you're used to from JavaScript. The only things you really need to know about classes in HTML is that, unlike an ID, class names can be used multiple times (that's primarily what they're designed for) and an element can have multiple class names (separated by a single space). So, what does this look like in HTML? <div id='some_div' class='some_class_1'></div> <div id='some_other_div' class='some_class1 some_class2'></div> Here, we have two different div elements, each with different IDs. However, both elements share the class some_class1 and will receive whatever properties that class contains. The second div will also inherit any properties that the class some_class contains. Styling HTML elements with CSS Now that we know how HTML uses classes and IDs, we can learn how to use them with CSS. Specifically, how do we refer to the element in our CSS code? There are three ways to specify an element in CSS: ‹‹ Element Type: Specifying the name of an element alone will apply the style to all elements of that type. For example: div { color: #ff0000; } This would apply the color #ff0000 (red) to all div elements. ‹‹ Element ID: Specifying the ID will cause the style to be applied only to the element with the specified ID. To do this, place an octothorpe (a hash or pound sign, #) in front of the desired ID. For example: #map_element { height:500px; width:500px; } ‹‹ Element Class Name: Specifying the class name will cause the style to be applied to all elements that have the desired class name. To do this, place a period in front of the class name. For example: .some_class1 { color:#0000ff; } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 175 ] Using CSS in your code So we know how to refer to elements now, but how do we use CSS in our HTML? There are three ways to go about this: ‹‹ Including an external CSS file: This is the preferred way to add CSS to our HTML pages, as we only need to change the style in one place if we need to make changes. It works similar in principle to how including an external JavaScript file works—we basically point to an external CSS file, and the page will read it in. To do this, we place a <link> tag in the <head> section of our HTML page. For example: <link rel='stylesheet' href='style.css' /> The rel attribute specifies the link is a stylesheet, and the href specifies the location of the .css file itself. ‹‹ Including the CSS in a <style> tag in the <head> tag: This is another way to specify CSS, but is the least desirable way to go about it, as we'll have to update it on every page if we want to change some style. However, when working with only one file this isn't too much of an issue and this is why we've been doing it this way throughout the book so far. In the <head> tag, you can add a <style> tag, which will contain all your CSS definitions. Everything inside the <style> tag is interpreted as CSS code, similar to how code inside a <script> tag is considered as JavaScript code. ‹‹ Including "in line" CSS definitions as style attributes on elements: We've seen this in the previous chapters. Everything inside the quotes of the style attribute is considered CSS code. An example would be: <div id='map_element' style='height:500px; width:500;'></div> For those familiar with the <link> tag, you may notice that we have only included the rel and href attributes. We do not need to specify the type attribute in HTML5, the current version of HTML at the time of writing. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 176 ] Time for Action – using external CSS files Let's create a simple page using an external CSS style sheet file (a .css file), making use of IDs and classes. 1. Create a new, empty HTML file. We'll just make a basic HTML5 page with a few <div> tags to demonstrate CSS inheritance. We'll also include a <link> tag that will reference a CSS file created in the next step, which contains all our CSS code. 2. First, we'll need to start the HTML page with a DOCTYPE tag that tells browsers this is an HTML5 standard page. Then, we'll need an html tag and a head tag, which contains some meta information (such as a title tag), and our JavaScript and CSS file links. <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' /> <link rel='stylesheet' href='ex1_style.css' /> <title>Ugly_Webpage_01 3. Now, we'll need to create a body tag which will contain our page's actual content. We'll also put in some div tags, which act as content blocks. Each div element will have a unique ID. Each div will also have at least one class. Classes can be used by multiple elements, and you can apply multiple classes to an element by separating the class name with a space:
Hello world!
The cake is a lie.
Hello, world.
4. Create a file called ex1_style.css and put in the following code. We'll specify a background and text color property for the body tag. Because elements inherit style from their parent element, every single element will also receive these two properties (but they can be overwritten): This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 177 ] body { background:#ffffff; color:#000000; } 5. Let's specify styles for elements by IDs, which we'll designate with a # sign. IDs are used to specify a single element. This will cause the element that has the specified ID to be styled; however, we will define it. We'll also specify styles for classes by using a period (.). In CSS, classes are used to apply styles to multiple elements: #title { font-size:1.5em; font-variant: small-caps; } #content { background:#ababab; color:#f0f0f0; font-weight:bold; text-align:right; } #message { background:#336699; } .background_green { background: #22dd22; } .align_left { text-align:left; } 6. You should see something like this: What Just Happened? We've demonstrated what we've been talking about so far in this chapter, including an external CSS file and using IDs and class names to reference elements. You might be wondering why some styles got applied and others did not. Let's quickly go over why that happens, and then move onwards to learn about how OpenLayers uses CSS. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 178 ] In CSS, you can use /* to begin a comment, and */ to end it. For example, /* This is a comment */ Have a Go Hero – view HTML and CSS in Firebug Open up Firebug and go to any website. Firebug will automatically build an HTML tree and populate the Style tab with the site's style information. Poke around and try changing values with Firebug, and you'll be able to immediately see the site's appearance change based on the styles you apply with Firebug. Cascading Style Sheets—Inheritance CSS uses inheritance in a similar way our classes in JavaScript do. With CSS, child elements can inherit properties of their parent elements, as well as override them. So, as in the example above, our body element had a text color (the color property) of black (#000000). This means that any element inside the body element (which is, practically, every HTML element on the page) will receive the color:#000000 property unless overwritten. This is a situation where the 'cascading' part of CSS comes in. Styles 'cascade' downwards, so the properties found in the child element (the 'bottom' most elements) will override properties which it inherits from its parents (the elements above it). Order of inheritance When trying to determine which style an element will receive, any styles from the class name will overwrite base element styles, and any styles from the ID will overwrite class styles. Take a look at the following code:
Some text
In this code, the element will have a final color of #ff0000. The base element (the div style) will be overwritten by the .random_class class style, and then the class style will be overwritten by the #my_element ID style. So, the order of importance is ID > class > base element type. Using the !important statement after the value of a property will ensure that the property is not overwritten by any of its children. If however, one of the children contains an !important statement on that property, then it will be overwritten. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 179 ] Referencing elements One very useful thing we can do with CSS is to use the inheritance we just talked about. More specifically, we can write: #my_element div { background: #2222ff; } This will apply a background color only to div elements that are children of #my_element. This nesting can be as deep as you'd like—just separate elements by a space. This, for example: #my_element #my_inner_element .random_class a { font-weight: bold; } This would be applied to all a elements (an anchor tag, specified by ) inside all elements with a class of random_class inside an element with an ID of my_inner_element inside of an element with the ID my_element. If we want to apply the same style to multiple elements, we have to simply specify the elements and place a comma in between them: #my_element1, #my_element2, .random_class { color:#cdcdcd; } Alright, we've covered the basics behind CSS and HTML element referencing, so let's see how OpenLayers uses CSS. Pop Quiz – how to reference elements Take a look at the following code. Try to think of at least three different ways you could reference the inner_most span element. Hint—think of ways to access it with inheritance as well.

This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 180 ] OpenLayers and CSS OpenLayers applies class names and IDs to most every HTML element it creates, so customizing your map's UI is quite easy once we know how it generates class names and IDs. Before we talk about how to override styles though, we need to know where the style files themselves are. Styling OpenLayers—using themes In OpenLayers, themes are used to control the appearance of your UI elements. A theme is comprised of a CSS file and any related user interface images. OpenLayers applies a lot of styles to your map by default, using a theme called default. Creating your own theme is easy though. If you want to create custom UI styles for your map, you'll essentially need to either create a 'theme folder' which will contain a CSS file and images for your UI controls to use (e.g., pan arrow images), or manually overwrite certain default styles. The theme folder can be anywhere you'd like, just keep in mind what its path is when referencing it in your HTML file. To use your own theme, you just need to do three things: 1. After including OpenLayers in your page, add a link to your CSS file which includes your customized map styles. 2. You must tell OpenLayers where to find the images you use for your map. To do this, specify the location of the folder your images are in (your theme folder) by adding the follow to your JavaScript code. The path is relative to the location of the file containing the JavaScript code: OpenLayers.ImgPath = 'path/to/your/theme_folder/'; 3. Specify the location of your theme folder when creating your map object via the theme property. The path is relative to the location of the file containing the JavaScript code. For example: var map = new OpenLayers.Map('map_element', {theme: 'theme_folder'}); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 181 ] Creating your own themes Now we know how to use themes, but how do we create a theme? Well, the answer for that is simple—we just create a CSS file, some images, and create a folder that will house them. The images will go in a folder called img inside your theme folder. Then, we just tell OpenLayers to use that theme. The expected structure of the theme folder may change with later versions of OpenLayers, so the best way to figure what images are provided by default (and their names) is to look in the default theme folder. You can find it in the theme | default folder of your downloaded OpenLayers folder. But the bigger question is how do we refer to the user interface elements? The answer lies in knowing how OpenLayers generates class names and IDs for the elements it creates. OpenLayers—class names and IDs To style an element, you must refer to it. If we want to refer to a specific div, we need to know its class and/or it's ID. OpenLayers generates class names and IDs, so we need to just know how the names are generated. The most common way is to style your controls using the generated class names. Generated class names The class names that OpenLayers generates for your controls follow the form of .olControlControlNameExtra This means that .olControl is always at the beginning of the class name, and ControlName is the name of the control class itself. The Extra refers to any additional descriptions or elements of the control (such as 'Inactive' and 'Active' added after many controls, especially button controls). So, for instance, if we wanted to style the scale control line control, we would refer to it as .olControlScaleLine As we've encountered before, for button type controls we add 'Inactive' or 'Active' at the end. Other controls, such as the OverviewMap control, also contain an OverviewMapElement class name—the element refers to the generated HTML container of the overview control. There are other similar cases, such as the LayerSwitcher control. A quick way to figure out the class name of an element is to use Firebug to inspect your page and find the element. We'll go over the class names of common controls soon, but first let's talk about another way to reference your control elements: by ID. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 182 ] Generated IDs Referring to control elements by class name is the preferred way of styling controls, but styling by ID is also possible. There are two ways to accomplish this: ‹‹ Wrong way: Using the generated ID. The ID is generated in the form of OpenLayers.Control.ControlName_XX where XX is a number that is assigned based on various things, such as control order. The number is likely to change based on how you make your map. So, if you use it in your style declarations and the ID changes, then the style will no longer be applied. ‹‹ Right way: Passing in an ID when creating the control and referring to it - in other words, when you create a new control, pass the id property with some string value which you will use as the element's ID. This way, we don't have to worry about the generated XX numbers changing, and we can be sure what the ID will be. Alright, so we know how to refer to elements now, and how OpenLayers generates class names and IDs, so let's take a look at an example to solidify the concepts. Time for Action – styling controls Let's work with styling some OpenLayers controls. We won't be creating a theme here; instead, we'll just link to an external CSS file which will overwrite the default styles of the elements we wish to style. 1. Let's start off with creating a new page. We're going to also put all our custom styles in a file called control_style.css. So, whenever we add a style, be sure to put it in the control_style.css. Also, make sure to include the CSS file in the section with: 2. Now, let's create our map object. We'll specify the controls array when we instantiate it since we do not want the PanZoombar control in this example. We also are not using a custom theme (we are just overriding styles with the external CSS file), but if we were we would specify the theme property as well. map = new OpenLayers.Map('map_element', { controls: [new OpenLayers.Control.Navigation()] }); 3. Alright, let's create some controls. We'll go over each control individually so we can see all the steps involved. First, let's create a ScaleLine control. In our JavaScript code, use the following to create a default ScaleLine control object: map.addControl(new OpenLayers.Control.ScaleLine()); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 183 ] 4. Now, let's apply some style to it. Because we haven't passed in a class name or ID, we'll use the class name that gets automatically generated. This is a standard control that does not have any 'active' or 'inactive' states, so we can just use the name generated by default, olControlScaleLine. In your control_style.css file, add the following: .olControlScaleLine { background: #777777; color:#ffffff; } 5. Most controls can be styled by just using their main class name (i.e., .olControlControlName). Other controls, such as buttons and controls with active/inactive states have additional class names. This is a good thing—you usually want a button to have a different style when it's clicked. Let's use the NavToolbar control—in your JavaScript code, input the following: map.addControl(new OpenLayers.Control.NavToolbar()); 6. Now, let's style the NavToolbar control. We have to style two things—the NavToolbar control itself (which is actually a panel) and the Inactive/Active states of the buttons it contains. The NavToolbar control also contains a ZoomBox control, which we'll need to style as well. It also contains Inactive/Active states that we'll need to style. By default, the buttons have a left and top property. We'll replace those values with 0 and add the !important value, which will make sure our newly set values are not overwritten. In your CSS file, add: .olControlNavToolbar { top: 0; } .olControlNavigationItemInactive { background: #787878 !important; border: 2px solid #232323; cursor: pointer; left:0 !important; top:0 !important; } .olControlNavigationItemActive { background: #dedede !important; border: 2px solid #787878; cursor: pointer; left:0 !important; top:0 !important; } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 184 ] .olControlZoomBoxItemInactive { background: #336699 !important; border: 2px solid #232323; cursor: pointer; left:0 !important; top:0 !important; } .olControlZoomBoxItemActive { background: #77aadd !important; border: 2px solid #5588aa; cursor: pointer; left:0 !important; top:0 !important; } 7. Let's create an OverviewMap control. In your JavaScript code, add the following: map.addControl(new OpenLayers.Control.OverviewMap()); 8. Now, let's style it. This is a control comprised of a few different elements, but they all have class names, so we can style it however we'd like. We'll style the background of the overview map when it's opened (via .olControlOverviewMap), the extent rectangle on the overview map, and the location of the minimize/maximize buttons. Add this to your CSS: .olControlOverviewMapMaximizeButton, .olControlOverviewMapMinimizeButton{ bottom:0 !important; } .olControlOverviewMapElement { background: #cdcdcd !important; } .olControlOverviewMapExtentRectangle { background:rgba(60,90,120,.7); border:2px dashed #22dd22 !important; } 9. Open up your map, move around, and look at your controls. You should see something like this: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 185 ] What Just Happened? We made a few controls and added styles to them. We applied styles in the following three different ways: ‹‹ Styling the controls themselves based on their class name (e.g., olControlScaleLine) ‹‹ Styling button controls (their Inactive and Active states) ‹‹ Styling various other elements created by the control It may not always be immediately obvious what class names to use to refer to a control, but it is always possible to figure it out. One of the quickest and easiest ways to do so is to use Firebug (or other web development tools) to inspect your page's generated HTML and look for elements that have the control's name in the class name. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 186 ] Time for Action – styling the LayerSwitcher control Let's take a look now at how to style the LayerSwitcher control. Unlike the previous example, we'll place this control in a div element outside the map. 1. Start a new page. We'll link to an external CSS file that will override the base LayerSwitcher control style, like in the previous example. Include a tag that references a file called ex3_layerswitcher_style.css, which we'll create soon. 2. Next, we'll need to create a div element that will house our control. Create a div tag after the map_element div:
3. Now, in your JavaScript code, create the map and WMS layer object as normal. Add a LayerSwitcher control, and pass in the layer_switcher_control element as the div property: map.addControl(new OpenLayers.Control.LayerSwitcher({ div: document.getElementById('layer_switcher_control') })); 4. Now, if you open up the map you should see the map with the layer switcher control beneath it: 5. By default, it looks pretty plain. It also has a border with rounded corners. First, we'll need to disable the rounded corners by passing in roundedCorner: false. map.addControl(new OpenLayers.Control.LayerSwitcher({ div: document.getElementById('layer_switcher_control'), This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 187 ] roundedCorner: false })); 6. Let's create the ex3_layerswitcher_style.css file now. We'll specify the control's style here. As we have manually specified the div property when creating the control, the first thing we'll style is the div we passed in (with an ID of layer_switcher_ control). Because we passed this in ourselves, OpenLayers does not apply any default styles to it. Let's give it a solid border and some padding. Add this to the style file: #layer_switcher_control { border:2px solid #454545; padding:2px; } 7. Now we need to start overriding the styles the control inherits from OpenLayers. First, we'll need to get the relevant class names. We can use Firebug to see that the 'Base Labels' text is inside a div with a class of baseLbl. Since it's a heading that lists base layers below it, let's make it a bit bigger and give it a background. It also falls inside our layer_switcher_control div, so when we specify it in the CSS let's be as specific as we can: #layer_switcher_control .baseLbl { background:#cdcdcd; font-size:1.3em; font-weight:bold; } 8. Let's also style the label text for the actual layer names. Using Firebug, we can see that the label text is inside a tag with a class of labelSpan, which is a child element of a div with the class baseLayersDiv. All base layers will show up in this div, while any overlay layers will show up in a div called dataLayersDiv. Using this information, let's apply a style to it, specifying the inheritance to access the element: #layer_switcher_control .baseLayersDiv .labelSpan { font-style: italic; font-weight:bold; } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Styling Controls [ 188 ] 9. Open up the page now and you should see the styled layer switcher div: What Just Happened? We just added a LayerSwitcher control outside the map and styled it. We saw how to style the div element that we created, along with styling the elements by grabbing the class names that OpenLayers generates. The LayerSwitcher control along with some others such as OverviewMap, have more elements to style than other controls. It may not be immediately obvious what elements need to be styled, but we can always use Firebug to inspect the HTML and determine the class names. Have a Go Hero – add layers Add more layers to the map you just created. Notice how there is now another set of elements for the overlay layers, in an element with a class that starts with 'dataLayers'. All your overlay layers are considered to be 'data layers' and will show up here, while base layers show up in the baseLayersDiv. Using what you learned in the previous example, apply your own styles to the overlay layers div elements. Other resources A comprehensive coverage of CSS is outside the scope of this book, but hopefully this chapter has provided necessary information for styling OpenLayers elements. More extensive information on CSS can be found at the W3Schools site, located at http://www.w3schools.com/css/default.asp. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 7 [ 189 ] A developer that has proficient design skills is not a common thing—fortunately, there are resources that provide high quality, professional user interface elements. GeoExt is a third party library that integrates ExtJS, a popular JavaScript framework, with OpenLayers, and includes numerous user interface and OpenLayers map components. It does, however, require a bit of familiarity with the ExtJS library. GeoExt can be found at http://www. geoext.org/index.html, and the ExtJS can be found at http://www.sencha.com/ products/js/. Summary In this chapter, we talked about what CSS is and what it's used for. We learned how to use CSS and HTML together, along with how to refer to elements in CSS. Then, we discussed how OpenLayers uses CSS, and how control elements get their names. Lastly, we created and applied our own custom styles to some controls. The goal of this chapter was to provide a foundation for understanding how we style controls with CSS. If you haven't worked much with CSS before or if you're unsure how to style other types of controls then don't worry. We'll be styling controls throughout the book using the same principles we introduced in this chapter, so you'll get a lot more exposure to it. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 8Charting the Map Class The Map class is, as you have probably figured out by now, the core piece behind your map. The map object(s) you create is the most important thing behind your map, as without a map object you can't do anything with layers or controls. In this chapter, we'll be talking about the Map class, which we've been taking for granted so far. Understanding the Map class will enable us to do even more cool things with our applications, and provides a way for us to programmatically tell our maps what to do. We've been using the Map class throughout the book so far without really knowing how or why. This chapter aims to not only explain how and why we've been doing things (such as using map.zoomToMaxExtent()), but also provide a thorough coverage of one of the core parts of OpenLayers—the Map class. We'll take a look at ‹‹ What the Map class is ‹‹ How the Map class relates to the other classes we've discussed ‹‹ Accessing the Map class' properties ‹‹ Using functions of the Map class ‹‹ Working with events to define and extend map behavior ‹‹ Creating a simple application that contains multiple maps This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 192 ] The Map class OpenLayers' Map class is what drives our maps. All the things we'd like to do with our maps—moving them, zooming, adding layers—all these things are made possible by this class. We've worked extensively with it already by creating a map object, adding controls and layers to it, then telling the map to zoom to the max extent. While we've covered Layers and Controls in pretty good detail, we have yet to really discuss the functionality behind the Map class, the core component of our applications. In OpenLayers, control and layer objects belong to a map object. Control and layer objects must be hooked up to a map if we want them to do anything. So, we need a map object to actually make a useful map—and as you might imagine, we'll see later in the chapter that it is possible to make an application that uses multiple map objects. Creating a map object Before we jump in, let's review the base code for instantiating a map object. We've done this many times before, but a little review won't hurt. var map = new OpenLayers.Map('html_element', { options }); The first parameter is a string consisting of the ID of the HTML element you wish the map to be placed in (almost always a div element). The second parameter is optional, and consists of an object literal, or anonymous object, (key:value pairs) containing property and value settings. So, there's not much new here yet. This is, essentially, all you need to set up your map object. However, as we've mentioned before, if you're using a projection other than EPSG:4326, such as spherical Mercator, you're going to have to set some other properties as well (such as maxExtent and units), when creating the map object. This chapter will cover most of the properties and functions available to us through the Map class, so let's start with the properties. Map class properties There is a vast array of possible properties that you can pass in, and we've already seen a few of them throughout the book. There are also quite a few properties which are pretty useful which we haven't yet covered. I'll start off by introducing a few properties, and then we'll see them in action with some examples. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 193 ] Throughout the explanation of properties and methods, I'll typically refer to an arbitrary map object as map. If your map object is called something else, then you would simply replace map with the name of your map object. Another quick note; while we will be talking about all these properties in the context of passing them in while creating the map object, you can call any of them at any time to get the value (e.g., calling map.tileSize will return the map's tile size). While we'll cover all the properties and functions relevant to all the examples and discussions in the book, for an always up to date and complete list you can visit the docs at http://dev.openlayers.org/docs/files/ OpenLayers/Map-js.html. Map properties Let's go over the map properties that are typically passed in when creating a map object. There are other properties that we can access but shouldn't pass in at instantiation time— we'll cover those next. For now, let's focus on properties to use when instantiating map objects. allOverlayers allOverlays: {Boolean} Default is false The allOverlays property specifies whether or not the map can function without any base layers. So far, we've been using base layers in all our examples, either explicitly or implicitly (letting our layers be set as base layers automatically). By setting this property, all layers will act as overlay layers. There may be times when you wish to use this—just keep in mind, when using this property, users have the ability to disable all layers and they could, in effect, see an empty map. By setting this property to true, third party API layers will also act as overlay layers. So, if you wish, you could use a Google maps layer as an overlay layer and make it semi transparent. controls controls: {Array {OpenLayers.Control}} We covered controls in Chapter 6, so we don't need to go into too much depth here. We can define our map's controls when we create the map object, or we can add them via addControl(s) after the map object has been instantiated. After the map is instantiated, we can access its controls by calling map.controls. If you do not specify an array of controls for the map to use when you create it, your map will receive four default controls: Navigation, PanZoom, ArgParser, and Attribution. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 194 ] displayProjection displayProjection: {OpenLayers.Projection} This is a property that is used mainly by controls which show coordinate information. By setting this property on the map object, any control that has a displayProjection property will be set to this value. Controls, such as the MousePosition control, can display coordinates in the displayProjection. So, your map could be in a different projection than what you wish to display the coordinates in. However, to use a displayProjection other than EPSG:4326 or EPSG:900913, Proj4js must be included on your page. This property comes in very handy if, for instance, your map is in a spherical Mercator projection (i.e., EPSG:900913), but you might wish to display coordinates in another projection, like EPSG:4326 (to display lon/lat coordinates). div div: {HTML Element or String} So far, we've been creating our maps like this: var map = new OpenLayers.Map('map_element', {}); With the div property, we can instead pass in an HTML element (or the ID of the element, as above) in the options; i.e., var map = new OpenLayers.Map({'div': 'map_element'}); One of the advantages of doing it this way is that the div property is optional, so you can create a map object and choose not to place it in any div. If you do this, you can place your map in a div later by using the render() method. This is actually almost the same thing OpenLayers is doing for us automatically when we pass in an HTML element during map object instantiation. When we do specify this div property, OpenLayers simply calls the render() method right then—we're just delaying this from happening and calling it manually if we choose not to specify a div. This can be useful when we only want to show the map after a user clicks on a button, for instance. Time for Action – using the allOverlays Map property Let's take a look at using the allOverlays property and not specifying a div when creating a map. 1. In your init() function, let's create our map object with allOverlays set to true and with some controls passed in. We will also not specify an HTML element: var map = new OpenLayers.Map({ This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 195 ] allOverlays: true, controls: [new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoom(), new OpenLayers.Control.LayerSwitcher()] }); 2. Now, let's create a couple of layers so we can see how the allOverlays property works. We'll assume that you will add WMS layers to your maps for future examples. var wms_layer_all = new OpenLayers.Layer.WMS( 'OpenLayers WMS', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}, {} ); var wms_layer_labels = new OpenLayers.Layer.WMS( 'Labels', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'clabel,ctylabel,statelabel', transparent:true}, {} ); map.addLayers([wms_layer_all, wms_layer_labels]); 3. Since we didn't specify a div when instantiating our map object, we'll have to call the render() function to place the map inside a div. The function takes in either an HTML element or the string of the element's ID, so let's pass in the element's ID. map.render('map_element'); 4. Finally, check to see if a center is set. If it is not, zoom to the max extent. We'll assume you do this step in future examples. You can place the call to the render() function after this bit if you'd like, and it will still zoom to the center or the max extent—the order in which you call render() and set the map's location is up to you. if(!map.getCenter()){ map.zoomToMaxExtent(); } This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 196 ] 5. Take a look at the map and open the layer switcher—you should be able to turn off both layers and see an empty map now. Because we don't have a base layer and all the Overlayers are disabled by default, you'll just see a blank map until you enable a layer. What Just Happened? We have just looked at a few of the properties we brought up—allOverlays, controls, and (leaving out) the div property. We also saw how the user can potentially see no map at all by turning off all the layers. The map will not automatically render, because we did not pass in an HTML element. Instead, we called the render() method and passed in an HTML element after our map object was created. There may be times when you wish to delay rendering the map, such as waiting for a user to click a button, and in those cases this is a good way to do it. We'll talk more about map methods later in this chapter, but let's get back to discussing map properties. eventListeners eventListeners: {Object} This property accepts an anonymous object containing key:value pairs of event types (as the key) and functions to call (the values) when those events are fired. We saw this property briefly in Chapter 6, and it behaves the same way here. An example would be: var map = new OpenLayers.Map('map_element', { eventListeners: { 'moveend': my_moveend_function } }); The above code assumes that a function named my_moveend_function exists. We will cover this property in much more detail, along with the possible event types, later in this chapter in the section on events. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 197 ] fallThrough fallThrough: {Boolean} Default: true The fallThrough property determines whether or not OpenLayers will allow events on the map to propagate downwards (i.e., 'fall through') to other elements on your page. Let's go over the meaning with an example. Say your map_element div has an event attached to it that will show an alert when you click on it. This event is outside of OpenLayers. Now, let's assume you create a map object and attach it to that map_element div. If fallThrough is set to true (it is by default), you will receive the original alert by clicking on the map—the mouse click event will be allowed to 'fall through'. If it is set to false, you will not receive the alert, as OpenLayers will consume that mouse click event. layers layers: {Array {OpenLayers.Layer}} We've encountered this property, to some degree, already. After the map is instantiated, we can access its layers by calling map.layers. So far, we've been creating our map object, then our layer objects, then adding the layers to the map with addLayer(). We can, however, use the layers property when instantiating our map and pass in an array of layer objects. Passing in layers using this property when we create our map object or calling addLayers essentially does the same thing—use whichever method you're most comfortable with. An example would be: var map = new OpenLayers.Map('map_element', { layers: [my_layer_1, new OpenLayers.Layer.WMS({ … })] }); You can pass in layer objects that have already been created (e.g., my_layer_1, assuming a layer object with that name has been created), or by instantiating a layer object in the call itself (e.g., new OpenLayers.Layer.WMS({...})). In the example above, the … would be replaced with the options for the WMS layer. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 198 ] maxExtent maxExtent: {OpenLayers.Bounds} Default: bounds in decimal degrees, (-180, -90, 180, 90) Setting this will specify the maximum extent of your map. Tiles that fall outside of the maximum extent will not be requested, nor can users pan to a coordinate that lies outside the maxExtent. If you are using a different projection than the default projection, you will need to change this to reflect the world's coordinates in your desired projection. The maxExtent property's data type is an OpenLayers.Bounds object. The OpenLayers. Bounds class is used to create a 'bounds' object, which contains four coordinates that make up a bounding box. The four coordinates are minimum x (left), minimum y (bottom), maximum x (right), and maximum y (top). To instantiate a bounds object, you just pass in the coordinates in the order of: var bounds_object = new OpenLayers.Bounds(minx, miny, maxx, maxy); minExtent minExtent: {OpenLayers.Bounds} Setting the minExtent will specify the minimum extent of the map. There is no default value for this property. You will usually not need to set this, unless you are specifying custom resolutions using maxResolution—in such case, you will usually set the values to (-1, -1, 1, 1). restrictedExtent restrictedExtent: {OpenLayers.Bounds} This property specifies the bounds the user can navigate in—meaning that the user will be able to only pan around inside the bounds specified by the restrictedExtent property (if it is given, and if restricting their navigation is possible). Setting this property will still allow the user to zoom out and see parts of the map that are outside the restricted extent, but the map's center will be set to the restricted extent. If you want to limit the zoom level, you could set the resolutions array, maxResolution, or scales properties (covered in the following pages). This property is similar to maxExtent, however, tiles outside the restrictedExtent will still be requested, but the user won't necessarily be able to pan to them. numZoomLevels numZoomLevels: {Integer} Default: 16 This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 199 ] The numZoomLevels property specifies the amount of possible zoom levels that your map will have. For many applications, giving users 16 zoom levels is not always desirable. In most cases, just specifying the numZoomLevels (along with maxExtent, if using a different projection) is all you need to do to set the number of zoom levels. We'll see, as we look at other properties, more ways to specify the number of zoom levels (some without using this property at all). However, let's look at how to use this property to set the number of zoom levels—it's pretty easy! Time for Action – setting zoom levels and maxExtent 1. Create your map object, by specifying the numZoomLevels and adding a PanZoomBar control. We'll also set the numZoomLevels property to 8, meaning that only eight zoom levels will be available. Lastly, we'll set the maxExtent property to include just a subset of the world, like the following: map = new OpenLayers.Map('map_element', { controls: [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.LayerSwitcher() ], numZoomLevels: 8, maxExtent: new OpenLayers.Bounds(-100, -30, 40, 30) }); 2. Open up the page, and you should see something like this: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 200 ] What Just Happened? Our map has only eight zoom levels and a maximum extent of (-100, -30, 40, 30). You must have noticed that the map started at a zoom level that is zoomed in a couple of times. This is mainly due to the fact that the furthest out zoom level would show more than the maximum extent of the map. Let's quickly talk about what zoom levels are. Zoom levels accessing the map.zoom property (or calling map.getZoom()) will show your current zoom level (an {Integer} value). The zoom level numbers descend from 0 (the 'furthest out' zoom level—the level your map will usually start at) to the maximum zoom value minus one (since the first value starts at 0, not 1). In our example, the highest zoom value we can have is 7, because our numZoomLevel setting is 8. Since the zoom values start at 0, not 1, we have to subtract one from the maximum number of zoom levels to get the current zoom level value. On the PanZoomBar control, the very bottom item on the slider represents the zoom level further out (we'll call this the minimum zoom level). The highest you can go will be referred to as the maximum zoom level. Now, if we're using a third party API layer, we can simply set minZoomLevel and maxZoomLevel on those layer properties, but to set these values for the map (and other non third party API layers), we use either scales or resolutions along with min or maxScale, and min or maxResolution. Map properties—Continued Let's continue our discussion on map properties. Resolutions resolutions: {Array{Float}} This is an array of resolutions the map will use. Each value in the array is a possible zoom level. If you do not pass this in when instantiating your map (or layer), it will be automatically determined based on other properties, such as maxExtent. Setting this property is another way to specify the number of zoom levels on your map. The resolutions array descends from high values to low values. Resolution is the width/height in map units per pixel—so, for example, 150 miles divided by 512 pixels would be a resolution of 0.29296875. If you are using a tile cache server, you will need to specify the resolutions at which the tiles are cached (as well as set the serverResolutions property, which specifies the resolutions at which the tiles are cached on the server). Otherwise, you will often simply just define the numZoomLevels to specify how many zoom levels you want. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 201 ] Another quick note on the resolutions array is that each value in the array is half the value of the previous item. So, if the first item in your resolutions array was 0.703125, the next value would be 0.3515625, the next 0.17578125, and so on. Time for Action – using resolutions array Setting the resolutions array is one way to specify the number of zoom levels. Let's see it in action. 1. In the map object definition specify the resolution array as the following: map = new OpenLayers.Map('map_element', { resolutions: [ 1.40625,0.703125, 0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125 ] }); 2. Zoom around on the map. Because we've only provided five possible resolutions, you should be able to zoom in five times. What Just Happened? Using the resolutions array, we were able to specify the number of zoom levels. Unless we're using a cached tile service, this isn't necessarily the easiest way to specify the number of zoom levels. We have mentioned earlier we can also use scales, and we can also use minResolution and maxResolution. Map/Layer property inheritance Resolutions/scales/max extent etc. properties can be specified in the map object or layer objects. Preferably, they should be specified in the map, as you won't have to worry about which layers to apply the properties to. However, if you do not specify them in the map, then the settings of the base layer will be applied to the map. This isn't necessarily bad, but it can be a bit ambiguous as to what layers are controlling the resolutions, because if these properties are set in overlay layers they won't necessarily be applied. So, to be safe, if you set those properties on the map then you don't have to worry about applying them to layers. The main exception is if you are using a third party API layer—in this case, you must set those layer properties, such as minZoomLevel, maxZoomLevel, (two third party API specific properties cannot be set on the map), and numZoomLevels. If you want to set properties that mimic limiting the minimum or maximum zoom level, you can use max/minResolution or max/minScale, which we'll cover now. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 202 ] Map properties discussion—Continued Let's get back to looking at some of the remaining map properties. maxResolution maxResolution: {Float or String (with value of 'auto')}. Default: 360 degrees / 256 px The maxResolution property specifies what the maximum resolution of the map can be. Or, in other words, how far 'zoomed out' the map can be. Setting this property will affect what the base zoom level is and how far you can zoom out. If you want to get the value for this property, then you can zoom to the desired zoom level and call map. getResolution(); or access the map.resolution; property in Firebug to get the current zoom level's resolution. Setting the maxResolution to 'auto' is one way to make sure the map's extent 'fits' your map's div completely. Setting the property to 'auto' can also be used with other properties to automatically generate zoom levels. minResolution minResolution: {Float or String (with value of 'auto')} Specifying this property will limit the minimum resolution of the map—how far in the user can zoom. This property can also be set to 'auto', but if it is then the minExtent property must be set as well. If minResolution is not set, the number of zoom levels will be determined either by the numZoomLevels setting, the resolutions array, or the scales property. Time for Action – using Min and Max resolution Let's look at how to use the minResolution and maxResolution properties and how they affect our zoom levels. 1. Instantiate your map object, by specifying the the min/max resolution properties: //Create a map with an empty array of controls map = new OpenLayers.Map('map_element', { controls: [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.LayerSwitcher() ], minResolution: 0.02197265625, maxResolution: 0.3515625 }); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 203 ] 2. You should see something like this, with limited zoom levels: 3. Now, let's recreate the map element and set the maxResolution and numZoomLevels properties: map = new OpenLayers.Map('map_element', { controls: [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.LayerSwitcher() ], maxResolution: 0.3515625, numZoomLevels:8 }); 4. The map should now show zoom levels like: This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 204 ] What Just Happened? We just used the minResolution and maxResolution properties to specify the zoom levels of our map. There is another way, apart from using resolutions, to specify zoom levels and that is by using the scales property. scales scales: {Array} To specify the scales, pass in an array of scale values. When using this property, do not use the minResolution, maxResolution, minScale, maxScale, numZoomLevels, minExtent or resolutions properties. If these properties are set, they override the scales property and it will not be used. You should also specify the unit property if your projection is using units other than 'degrees'. Like the resolutions array, scales are ordered from highest to lowest—most zoomed out to most zoomed in. You can use map.getScale() to get the scale of the current zoom level. maxScale maxScale: {Float} This specifies the maximum scale of the map. This property is similar to maxResolution—it limits how far out the user can zoom the map. If you specify the maxScale property, setting the maxResolution or minResolution properties may cause errors. Use either scales or resolutions, but avoid mixing them. minScale minScale: {Float} This property specifies the map's minimum scale. More specifically, it determines how far the user can zoom in. If you set this property, the same idea applies as with maxScales—do not mix scales and resolutions properties. When setting maxScale and minScale, the minScale value should be larger than the maxScale value. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 205 ] Time for Action – Using scales Using scales is another way we can control the zoom levels on our map. Let's take a look. 1. Create your map object using minScale and maxScale as follows: map = new OpenLayers.Map('map_element', { controls: [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.LayerSwitcher() ], maxScale: 27683990.15625, minScale: 221471921.25 }); 2. You should see something like this: 3. Now, let's go back to the code and recreate our map object. This time, we'll pass in a scales array. //Create a map with an empty array of controls map = new OpenLayers.Map('map_element', { controls: [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.LayerSwitcher() ], scales: [ 55367980.3125, 27683990.15625, 13841995.078125, 6920997.5390625], }); This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 206 ] 4. You should see something like this: What Just Happened? We just used scales to determine the zoom levels of our map. Using scales or resolutions comes in handy when you want to specify the maximum or minimum zoom levels, as well as if you want to specify a hard-coded array of zoom level values (and especially when you are working with a cached tile server). Let's talk about some other, non zoom level related properties now. panMethod panMethod: {Function} Default: OpenLayers.Easing.Expo.easeOut This property specifies what type of tween animation will be used when panning the map. The default value is a function called OpenLayers.Easing.Expo.easeOut, which causes the animation to 'ease out' (or slow down) when the panning is finished. At the time of writing, the possible values are OpenLayers.Easing.Expo, OpenLayers.Easing. Quad, and OpenLayers.Easing.Linear. Each of those three base functions are divided into three further types: easeOut ('out' means when the animation is finished), easeIn (in means when the animation starts), and easeInOut (which means the animation will happen at both times). The docs at http://dev.openlayers.org/docs/files/OpenLayers/ Tween-js.html will contain the most up to date possible values. When specifying this property, use the full combination of the base class and subclass, such as OpenLayers.Quad.easeInOut. The animation effect will occur whenever the map is panned, such as when using the pan arrow buttons, panning the map with your keyboard, or calling panTo(). This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Chapter 8 [ 207 ] panDuration panDuration: {Integer} Default: 50 Specifying this will control how long panning takes to complete. Like the panMethod property, this property only effects the panning animation of the map. Time for Action – working with Pan animations OpenLayers gives us the ability to use different types of animations when the user pans the map. Using the properties we just discussed, let's learn how to customize the pan animations. 1. Create a new page from the template in Chapter 1. Define your map object like this, passing panMethod and panDuration properties: map = new OpenLayers.Map('map_element', { panMethod: OpenLayers.Easing.Quad.easeInOut, panDuration: 100 }); 2. Pan the map by clicking on one of the arrows in the control on the top left. In Firebug, call the following panTo function to pan the map to pass in a coordinate: map.panTo(new OpenLayers.LonLat(-18,42);) 3. You should see an animation when panning the map. What Just Happened? We just used the panMethod and panDuration properties to customize our map's pan animations. Changing the panDuration will affect how long the animation itself lasts, and changing the panMethod determines which type of animation to use. By using these properties, you can mimic the way Google Maps or other third party maps function. Have a Go Hero – use different animation types Using what we learned from the previous section, try changing the panMethod property to various different possible values (such as OpenLayers.Expo.easeInOut) to see the different animation styles. Also, change the panDuration to see how much different values affect the speed of the animation. Let's get back to some more map properties now. This material is copyright and is licensed for the sole use by Jim White on 26th June 2011 1238-A Hamilton Court, Cary, 27511 Charting the Map Class [ 208 ] projection projection: {String} Default: 'EPSG:4326' We've covered this in depth in Chapter 4, our chapter on projections, so we don't need to spend much time going over it here. Essentially, if you are using a projection other than EPSG:4326, then you can set the EPSG code here to specify your map's projection. All your layers should be in the same projection as your map's projection. If you do set the projection here, you will also usually have to specify related properties such as the maxExtent, maxResolution, and units (if the projection is not in degrees). theme theme: {String or null} Default: 'theme/default/style.css' This property specifies the relative path (meaning, relative to the path your map page is in) to the map's theme style file—a CSS file. If you are using your own theme file, you should specify the path to the CSS file here, along with setting the OpenLayers.ImgPath (the path of any custom map images). This property can also be set to null, in which case you can simply include CSS file link tags in your page (or put style information in