Creating a loader function isn't as hard as it might sound, let's walk through a concrete example that loads features from a WFS server. In this example, we will be using the popular jQuery library to help us load data. If you are not familiar with jQuery, see http://jquery.com for more information.
<script> tag to load jQuery:<!doctype html>
<html>
<head>
<title>vector Examples</title>
<link rel="stylesheet" href="../assets/ol3/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="../assets/ol3/ol.js" type="text/javascript"></script>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
var tiledRaster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var center = ol.proj.transform([-72.6, 41.7], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center: center,
zoom: 12,
});
var map = new ol.Map({
target: 'map',
layers: [tiledRaster],
view: view
});
</script>
</body>
</html>Next, we'll need to create a style object for displaying our vectors. Don't worry too much about what this is, we'll cover it in detail in Chapter 6, Styling Vector Layers. Add this after the tiledRaster and before the center:
var fill = new ol.style.Fill({
color: 'rgba(0,0,0,0.2)'
});
var stroke = new ol.style.Stroke({
color: 'rgba(0,0,0,0.4)'
});
var circle = new ol.style.Circle({
radius: 6,
fill: fill,
stroke: stroke
});
var vectorStyle = new ol.style.Style({
fill: fill,
stroke: stroke,
image: circle
});vector_template.html and make a copy of it to use for the rest of this example. We'll be using vector_template.html as the starting point for several examples.vectorStyle object, which will handle loading features from the remote server:var vectorLoader = function(extent, resolution, projection) {
var url = 'http://demo.boundlessgeo.com/geoserver/wfs?service=WFS&' +
'version=1.1.0&request=GetFeature&typename=osm:builtup_area&' +
'outputFormat=text/javascript&format_options=callback:loadFeatures' +
'&srsname=EPSG:3857&bbox=' + extent.join(',') + ',EPSG:3857';
$.ajax({
url: url,
dataType: 'jsonp'
});
};
var loadFeatures = function(response) {
var features = vectorSource.readFeatures(response);
vectorSource.addFeatures(features);
};loadFeatures function:var vectorSource = new ol.source.ServerVector({
format: new ol.format.GeoJSON(),
loader: vectorLoader,
strategy: ol.loadingstrategy.createTile(new ol.tilegrid.XYZ({
maxZoom: 19
})),
projection: 'EPSG:3857',
});
var serverVector = new ol.layer.vector({
source: vectorSource,
style: vectorStyle
});serverVector layer to the map's layers:var map = new ol.Map({
renderer: 'canvas',
target: 'map',
layers: [tiledRaster, serverVector],
view: view
});
We just created a vector layer that reads features from a remote WFS server as the map is panned and zoomed. A lot just happened, so let's review each step in detail.
In step 1, we set up a new OpenLayers application using the same structure as we've used for all the other examples so far. Nothing new here, except we've chosen a center for the map view somewhere in the north eastern United States and named our base layer tiledRaster to help differentiate it from the vector layers we will be working with.
In step 2, we created a simple vector style object to use with all our vector layers. A vector style consists of several components, in this case, we are defining the style to use for fill (polygons), stroke (lines), and image (points). The color for each is defined using the rgba() syntax, which stands for red, green, blue, and alpha (opacity). In this case, we are using a simple black color and 20 percent opacity. We'll cover styles in much more detail in the next chapter.
Step 4 is really the heart of this example. First, we defined a vectorLoader function. This function is used by ol.source.ServerVector to fetch features from a remote server. The vectorLoader function declares a URL that points to a WFS server. You might think the URL looks a bit complicated, and you are right. For now, just trust that it does the right thing, which is to ask the server for all the vector features contained within the current extent. The jQuery ajax function is used to actually trigger loading the URL. The jsonp data type tells jQuery to request the data by inserting it as a <script> tag, and the callback option in the URL identifies the function that will be executed when the <script> tag is loaded; in this case, the loadFeatures function.
The loadFeatures function takes the features provided by the server, reads them using the vector source (which has a GeoJSON format for parsing the data), and adds them to the layer.
In step 5, we combined all the previous pieces to create the source and layer objects. To define the source, we used ol.source.ServerVector and provided it with a format (GeoJSON in this case), the loader function we defined in the previous step, a loading strategy, and a projection. We'll discuss the loading strategy a bit later. Finally, we created the actual layer object using ol.layer.vector and provide it with our source and style.
After we added this to the map's layers in step 5, we can see it in action. Every time the map is panned or zoomed, the vector layer checks its source for features. The ServerVector source requests data from a remote server using a function that we provide. This gives you a lot of flexibility, but also means that you have to understand how to load data from a remote server. Fortunately, libraries such as jQuery have very good convenience methods for doing just this. The ServerVector source uses its loading strategy to decide how to divide up the requests to the server and passes the appropriate parameters to your loader function. We'll talk a bit more about loading strategies in some time. Once the features are loaded, you can parse them and add them to the source directly. The vector layer then draws the features using the provided style.
To see what happens when features are requested from the server, open Web Inspector, switch to the Network tab, and make sure All is selected, then reload the page.

You can identify the feature requests because they start with wfs?service=WFS. Click on a request to view its response. Notice how some requests have no features, and some have features. Notice how the response is formatted—it looks like a JavaScript function call to loadFeatures() with an object literal as the argument. The loadFeatures() function is the one we specified and that we implemented. This is an example of how jsonp works.
Now that we've seen it in action, let's review the constructor options for a ServerVector source:
The final vector source is TileVector. This source loads features by requesting them in batches based on a tile grid exactly the same way that the raster sources work. The job of a tile grid is to divide the world up into rows and columns and convert geographic coordinates for the current view into row and column references.
If you were following closely in the previous section, you might wonder how TileVector is different from a ServerVector configured with a loading strategy based on ol.loadingstrategy.createTile. The main differences are as follows:
TileVector source has its own loading functionality—you do not need to provide one.TileVector source uses the row, column, and zoom level rather than geographic coordinates to request features from the server.TileVector source provides convenient options for working with tile-based servers.There are several ways of producing and serving vector tiles. One way is to use a program that splits vector data up into tiles based on a particular tile grid and save the tiles to disk in a directory structure based on the zoom level, column,and row. Another way is to use a server that can split up vector data on the fly. The OpenStreetMap wiki has a page dedicated to vector tiles and related resources at http://wiki.openstreetmap.org/wiki/vector_tiles.