This chapter's goal is to review main interactions in OpenLayers 3. Interactions are components that manage relations between mouse or keyboard actions and the map. They do not rely on HTML elements like buttons.
What are the main interactions? It's the most asked function for end users, such as interactions with events on the map to retrieve map or layers information or to create new information with a drawing. You can, for example, click on a polygon representing a property and find out who owns the place and since when. All such information is called attributes. Along this chapter, we will see how to use main interaction-related components, in particular for querying, drawing, and modifying geographical features. Then, we will see the default interactions at both the functional and code levels. We will end by reviewing the remaining interactions.
In this chapter, we will cover:
Let's get started by reviewing the part related to conversion and selection of vector formats.
Selection is mainly achieved through vector layers. So, a small introduction to data conversion can be useful.
In this section, we want to introduce you to the creation of static data in a particular vector, which is required in most cases where there is a need to click on your map to get information.
Let's see how you can provide vector data to our OpenLayers 3 library. We will not explain again how to work with vectors here—just how to provide them.
If you remember, we told you in Chapter 1, Getting Started with OpenLayers, in the section Anatomy of a web-mapping application, that the OpenLayers 3 library can consume dynamic vector data or static. The main difference resides in the fact that data is dynamically provided on demand. At the end of it all, OpenLayers consumes the same formats.
As a reminder for vector layers, the main following formats are supported:
You can go further by reviewing the content from Chapter 5, Using Vector Layers, and inspecting all ol.format.* available following the API documentation at http://openlayers.org/en/v3.0.0/apidoc/ol.format.html. You should not forget some formats can be abstract classes for other formats.
We have chosen to focus only on GeoJSON and KML: they are the most used formats for static files. As long as your applications do not use large amount of data, it's one of the most practical solutions. But most of the time, we don't have data directly available in those formats: a large percentage of geographic data production and manipulation is done using desktop mapping software that does not use those formats as a primary storage solution.
To understand why, let's see a context where you start to make your own examples. In this case, you have three main choices to have data:
From a programmer's point of view, GIS (Geographical Information System) is software to manage geographical datasets and make maps. We really chose to restrict the definition to keep it simple. That's why we encourage you to learn further by starting with the GIS Wikipedia page http://en.wikipedia.org/wiki/Geographic_information_system and also try out searches through search engines on the topic.
GIS was first used for desktop applications. With the growth of the Web, the GIS world extended to the Internet. The main problems to fill the gap between desktop-oriented geographic data and web geographic data were:
The second is also partly broken with open data, but lots of open data datasets always use GIS Desktop formats like the shapefile. We will not explain this format but demonstrate by an example of how you can do some data conversion.
To find open data portals, the main entry for America is https://www.data.gov.
For Europe, you should visit http://publicdata.eu.
For a worldwide overview, go at http://datacatalogs.org, a website for A Comprehensive List of Open Data Catalogs from Around the World.
To grasp the state of OpenData in your country, you can visit the Global Open Data Index at http://global.census.okfn.org. It's a website maintained by the community to make surveys about each country's open data initiative. The focus is mainly about the type of open data datasets available.