UNPKG

3.57 kBJavaScriptView Raw
1goog.require('ol.Map');
2goog.require('ol.View');
3goog.require('ol.format.GPX');
4goog.require('ol.format.GeoJSON');
5goog.require('ol.format.IGC');
6goog.require('ol.format.KML');
7goog.require('ol.format.TopoJSON');
8goog.require('ol.interaction');
9goog.require('ol.interaction.DragAndDrop');
10goog.require('ol.layer.Tile');
11goog.require('ol.layer.Vector');
12goog.require('ol.source.BingMaps');
13goog.require('ol.source.Vector');
14goog.require('ol.style.Circle');
15goog.require('ol.style.Fill');
16goog.require('ol.style.Stroke');
17goog.require('ol.style.Style');
18
19
20var defaultStyle = {
21 'Point': new ol.style.Style({
22 image: new ol.style.Circle({
23 fill: new ol.style.Fill({
24 color: 'rgba(255,255,0,0.5)'
25 }),
26 radius: 5,
27 stroke: new ol.style.Stroke({
28 color: '#ff0',
29 width: 1
30 })
31 })
32 }),
33 'LineString': new ol.style.Style({
34 stroke: new ol.style.Stroke({
35 color: '#f00',
36 width: 3
37 })
38 }),
39 'Polygon': new ol.style.Style({
40 fill: new ol.style.Fill({
41 color: 'rgba(0,255,255,0.5)'
42 }),
43 stroke: new ol.style.Stroke({
44 color: '#0ff',
45 width: 1
46 })
47 }),
48 'MultiPoint': new ol.style.Style({
49 image: new ol.style.Circle({
50 fill: new ol.style.Fill({
51 color: 'rgba(255,0,255,0.5)'
52 }),
53 radius: 5,
54 stroke: new ol.style.Stroke({
55 color: '#f0f',
56 width: 1
57 })
58 })
59 }),
60 'MultiLineString': new ol.style.Style({
61 stroke: new ol.style.Stroke({
62 color: '#0f0',
63 width: 3
64 })
65 }),
66 'MultiPolygon': new ol.style.Style({
67 fill: new ol.style.Fill({
68 color: 'rgba(0,0,255,0.5)'
69 }),
70 stroke: new ol.style.Stroke({
71 color: '#00f',
72 width: 1
73 })
74 })
75};
76
77var styleFunction = function(feature, resolution) {
78 var featureStyleFunction = feature.getStyleFunction();
79 if (featureStyleFunction) {
80 return featureStyleFunction.call(feature, resolution);
81 } else {
82 return defaultStyle[feature.getGeometry().getType()];
83 }
84};
85
86var dragAndDropInteraction = new ol.interaction.DragAndDrop({
87 formatConstructors: [
88 ol.format.GPX,
89 ol.format.GeoJSON,
90 ol.format.IGC,
91 ol.format.KML,
92 ol.format.TopoJSON
93 ]
94});
95
96var map = new ol.Map({
97 interactions: ol.interaction.defaults().extend([dragAndDropInteraction]),
98 layers: [
99 new ol.layer.Tile({
100 source: new ol.source.BingMaps({
101 imagerySet: 'Aerial',
102 key: 'As1HiMj1PvLPlqc_gtM7AqZfBL8ZL3VrjaS3zIb22Uvb9WKhuJObROC-qUpa81U5'
103 })
104 })
105 ],
106 target: 'map',
107 view: new ol.View({
108 center: [0, 0],
109 zoom: 2
110 })
111});
112
113dragAndDropInteraction.on('addfeatures', function(event) {
114 var vectorSource = new ol.source.Vector({
115 features: event.features
116 });
117 map.addLayer(new ol.layer.Vector({
118 source: vectorSource,
119 style: styleFunction
120 }));
121 map.getView().fit(vectorSource.getExtent());
122});
123
124var displayFeatureInfo = function(pixel) {
125 var features = [];
126 map.forEachFeatureAtPixel(pixel, function(feature) {
127 features.push(feature);
128 });
129 if (features.length > 0) {
130 var info = [];
131 var i, ii;
132 for (i = 0, ii = features.length; i < ii; ++i) {
133 info.push(features[i].get('name'));
134 }
135 document.getElementById('info').innerHTML = info.join(', ') || '&nbsp';
136 } else {
137 document.getElementById('info').innerHTML = '&nbsp;';
138 }
139};
140
141map.on('pointermove', function(evt) {
142 if (evt.dragging) {
143 return;
144 }
145 var pixel = map.getEventPixel(evt.originalEvent);
146 displayFeatureInfo(pixel);
147});
148
149map.on('click', function(evt) {
150 displayFeatureInfo(evt.pixel);
151});