UNPKG

2.38 kBJavaScriptView Raw
1goog.require('ol.Map');
2goog.require('ol.View');
3goog.require('ol.format.GeoJSON');
4goog.require('ol.geom.MultiPoint');
5goog.require('ol.layer.Vector');
6goog.require('ol.source.Vector');
7goog.require('ol.style.Circle');
8goog.require('ol.style.Fill');
9goog.require('ol.style.Stroke');
10goog.require('ol.style.Style');
11
12var styles = [
13 /* We are using two different styles for the polygons:
14 * - The first style is for the polygons themselves.
15 * - The second style is to draw the vertices of the polygons.
16 * In a custom `geometry` function the vertices of a polygon are
17 * returned as `MultiPoint` geometry, which will be used to render
18 * the style.
19 */
20 new ol.style.Style({
21 stroke: new ol.style.Stroke({
22 color: 'blue',
23 width: 3
24 }),
25 fill: new ol.style.Fill({
26 color: 'rgba(0, 0, 255, 0.1)'
27 })
28 }),
29 new ol.style.Style({
30 image: new ol.style.Circle({
31 radius: 5,
32 fill: new ol.style.Fill({
33 color: 'orange'
34 })
35 }),
36 geometry: function(feature) {
37 // return the coordinates of the first ring of the polygon
38 var coordinates = feature.getGeometry().getCoordinates()[0];
39 return new ol.geom.MultiPoint(coordinates);
40 }
41 })
42];
43
44var geojsonObject = {
45 'type': 'FeatureCollection',
46 'crs': {
47 'type': 'name',
48 'properties': {
49 'name': 'EPSG:3857'
50 }
51 },
52 'features': [{
53 'type': 'Feature',
54 'geometry': {
55 'type': 'Polygon',
56 'coordinates': [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6],
57 [-3e6, 6e6], [-5e6, 6e6]]]
58 }
59 }, {
60 'type': 'Feature',
61 'geometry': {
62 'type': 'Polygon',
63 'coordinates': [[[-2e6, 6e6], [-2e6, 8e6], [0, 8e6],
64 [0, 6e6], [-2e6, 6e6]]]
65 }
66 }, {
67 'type': 'Feature',
68 'geometry': {
69 'type': 'Polygon',
70 'coordinates': [[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6],
71 [3e6, 6e6], [1e6, 6e6]]]
72 }
73 }, {
74 'type': 'Feature',
75 'geometry': {
76 'type': 'Polygon',
77 'coordinates': [[[-2e6, -1e6], [-1e6, 1e6],
78 [0, -1e6], [-2e6, -1e6]]]
79 }
80 }]
81};
82
83var source = new ol.source.Vector({
84 features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
85});
86
87var layer = new ol.layer.Vector({
88 source: source,
89 style: styles
90});
91
92var map = new ol.Map({
93 layers: [layer],
94 target: 'map',
95 view: new ol.View({
96 center: [0, 3000000],
97 zoom: 2
98 })
99});