Blame view

controllets/select-marker-map-controllet/select-marker-map-controllet.html 14 KB
edda4294   Luigi Serra   updates
1
2
3
  <link rel="import" href="../../bower_components/polymer/polymer.html"/>
  <link rel="import" href="../../bower_components/paper-material/paper-material.html"/>
  <link rel="import" href="../../bower_components/paper-fab/paper-fab.html"/>
210f1212   Luigi Serra   updates
4
  <link rel="import" href="../../bower_components/iron-icons/maps-icons.html">
edda4294   Luigi Serra   updates
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  <link rel="import" href="../../bower_components/iron-icons/iron-icons.html"/>
  <link rel="import" href="../../bower_components/paper-dialog/paper-dialog.html"/>
  <link rel="import" href="../../bower_components/neon-animation/neon-animation.html"/>
  <link rel="import" href="../../bower_components/paper-dropdown-menu/paper-dropdown-menu.html"/>
  <link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html"/>
  <link rel="import" href="../../bower_components/paper-item/paper-item.html"/>
  
  <script src="../../locales/paper_card_controllet_ln.js"></script>
  
  <dom-module id="select-marker-map-controllet">
  
      <template>
          <style is="custom-style">
              #map_tootlbar{
                  background: #ffffff;
                  border: 1px solid #ccc;
                  position: absolute;
                  top: 2%;
                  right: 1%;
                  z-index: 1;
                  padding: 4px;
              }
  
              div.ol3-geocoder-container{
                  position: absolute;
                  top: 26%;
                  left: .5em;
                  box-sizing: border-box;
              }
          </style>
          <link href="http://cdn.jsdelivr.net/openlayers.geocoder/latest/ol3-geocoder.min.css" rel="stylesheet">
  
  
          <div id="map_tootlbar">
              <paper-dropdown-menu label="Interaction" id="interaction_box">
                  <paper-listbox id="interaction_box" class="dropdown-content" selected={{selected_interaction}}>
                      <paper-item>Marker</paper-item>
                      <paper-item>LineString</paper-item>
                      <paper-item>Polygon</paper-item>
                      <paper-item disabled>Circle</paper-item>
                      <paper-item>Square</paper-item>
                      <paper-item>Box</paper-item>
                      <paper-item>Text</paper-item>
                  </paper-listbox>
              </paper-dropdown-menu>
  
              <!--<paper-input id="shape_label" label="Shape label" maxlenght="30" value="{{label}}"></paper-input>-->
210f1212   Luigi Serra   updates
52
              <paper-fab id="map_layer" class="cocreation_view_modes_button" mini icon="maps:satellite" on-click="_handleMapLayerClick" style="background-color:#B6B6B6"></paper-fab>
edda4294   Luigi Serra   updates
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  
          </div>
  
          <div id="map" tabindex="0" style="height: 100%; width: 100%"></div>
          <div id="popup" class="ol-popup">
              <a href="#" id="popup-closer" class="ol-popup-closer"></a>
              <div id="popup_content"></div>
          </div>
      </template>
  
      <script src="http://openlayers.org/en/v3.17.1/build/ol.js"></script>
      <script src="http://cdn.jsdelivr.net/openlayers.geocoder/latest/ol3-geocoder.js"></script>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.6/proj4.js"></script>
  
      <script>
  
          _this = null;
          Polymer({
              is: "select-marker-map-controllet",
  
              listeners : {
                  'iron-select' : '_changeInteraction'
              },
  
              properties: {
                  lang:{
                      type: String,
                      value: navigator.language
                  },
                  map:{
                      type: Object
                  },
  
                  olview:{
                      type: Object
                  },
  
                  geocoder:{
                      type: Object
                  },
  
                  overlay:{
                      type: Object
                  },
  
                  iconStyle:{
                      type : Object
                  },
  
                  vectorSource:{
                      type: Object
                  },
  
                  coordinate: {
                      type: Array,
                      value: []
                  },
  
                  interaction:{
                      type: String,
                      value: "marker"//marker - geojson
                  },
  
                  selected_interaction:{
                      type: Number,
                      value: 0
                  },
  
                  draw_layer:{
                      type: Object
                  },
  
                  source: {
                      type: Object
                  },
  
                  draw:{
                      type: Object
                  },
  
                  textStyle:{
                      type: Object
                  },
  
                  geoStyle:{
                      type: Object
                  },
  
                  label: {
                      type: String,
                      value: ""
210f1212   Luigi Serra   updates
144
145
146
147
148
149
                  },
  
                  satLayer: {
                      type: Object,
                      value: new ol.layer.Tile({
                                title: 'Global Imagery',
f492146f   Luigi Serra   updates
150
                                visible: false,
210f1212   Luigi Serra   updates
151
152
153
154
155
156
157
158
159
160
                                source: new ol.source.BingMaps({
                                  key : "AiAjdlCagGtE5269Z4YP3e3VtMkzzAKvowq5cQadahNZjVxttUxdDS40u91Uv8qP",
                                  imagerySet: 'AerialWithLabels'
                                })
                            })
                  },
  
                  layers: {
                      type : Array,
                      value : []
edda4294   Luigi Serra   updates
161
                  }
210f1212   Luigi Serra   updates
162
  
edda4294   Luigi Serra   updates
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
              },
  
              ready : function(){
                  _this = this;
                  this.olview = new ol.View({
                              center: [0, 0],
                              projection: "EPSG:4326",
                              zoom: 3,
                              minZoom: 2,
                              maxZoom: 20
                          });
  
                  var baseLayer = new ol.layer.Tile({
                      source: new ol.source.OSM()
                  });
  
                  //marker layer
                  this.iconStyle = new ol.style.Style({
                      image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
                          anchor: [0.5, 46],
                          anchorXUnits: 'fraction',
                          anchorYUnits: 'pixels',
69872d75   Luigi Serra   updates
185
                          opacity: 0.9,
edda4294   Luigi Serra   updates
186
187
188
189
190
191
192
193
194
195
196
197
198
                          src: 'http://services.routetopa.eu/DEEalerProvider/COMPONENTS/datalets/leafletjs-datalet/leafletsjs/images/marker-icon.png'
                      }))
                  });
  
                  this.vectorSource = new ol.source.Vector({});
  
                  //draw layer
                  this.source = new ol.source.Vector({wrapX: false});
  
                  this.draw_layer = new ol.layer.Vector({
                      source: this.source,
                      style: new ol.style.Style({
                          fill: new ol.style.Fill({
69872d75   Luigi Serra   updates
199
                              color: 'rgba(255, 255, 255, 1.0)'
edda4294   Luigi Serra   updates
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
                          }),
                          stroke: new ol.style.Stroke({
                              color: '#ffff00',
                              width: 4
                          }),
                          image: new ol.style.Circle({
                              radius: 7,
                              fill: new ol.style.Fill({
                                  color: '#ffff00'
                              })
                          })
                      })
                  });
  
                  this.map = new ol.Map({
                      target:'map',
                      view: this.olview,
                      controls: ol.control.defaults({
                      attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
                          collapsible: false
                      })}),
                      loadTilesWhileAnimating: true,
                      loadTilesWhileInteracting: true,
                      layers: [baseLayer,
210f1212   Luigi Serra   updates
224
                               this.satLayer,
edda4294   Luigi Serra   updates
225
                               new ol.layer.Vector({
210f1212   Luigi Serra   updates
226
                                  source: this.vectorSource
edda4294   Luigi Serra   updates
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
                               }),
                               this.draw_layer
                      ]
                  });
  
                  //Instantiate with some options and add the Control
                  this.geocoder = new Geocoder('nominatim', {
                      provider: 'photon',
                      lang: 'en',
                      placeholder: 'Search for ...',
                      limit: 5,
                      keepOpen: true,
                      preventDefault : true
                  });
  
                  this.overlay = new ol.Overlay({
                      element: document.getElementById('popup'),
                      offset: [0, -40]
                  });
  
                  this.map.addControl(this.geocoder);
  
                  //Listen when an address is chosen
                  this.geocoder.on('addresschosen', function(evt){
                      _this.olview.setCenter(evt.coordinate);
                      _this.olview.setZoom(16);
                      if(_this.selected_interaction == 0) {
                          _this.setMarker(evt.coordinate);
                      }
  
                      //_this.$.popup_content.innerHTML = '<p>'+ evt.address.formatted +'</p>';
                      //_this.overlay.setPosition(coord);
                  });
  
                  this.map.on('click', function(evt) {
                      if(_this.selected_interaction == 0) {
                          _this.setMarker(evt.coordinate);
                      }
                  });
  
                  this.async(function(){this.resize($(document).height(), $(document).width())}, 100);
              },
  
              setMarker: function(coordinate){
                  var iconFeature = new ol.Feature({
                      geometry: new ol.geom.Point(coordinate),
                      name: 'New Point'
                  });
  
                  iconFeature.setStyle(_this.iconStyle);
  
                  this.vectorSource.clear(true);
                  this.vectorSource.addFeature( iconFeature );
  
                  //this.coordinate =  ol.proj.transform([coordinate[1],coordinate[0]], "EPSG:900913", "EPSG:4326");
                  this.coordinate = [coordinate[1],coordinate[0]];
              },
  
              addInteraction : function() {
                  this.map.removeInteraction(this.draw);
                  var geometryFunction, maxPoints, interaction_type;
                  switch(this.selected_interaction){
                      case 0:
                          //this.map.addInteraction(ol.interaction.default());
                          break;
                      case 1:
                          interaction_type = "LineString";
                          break;
                      case 2:
                          interaction_type = "Polygon";
                          break;
                      case 3:
                          interaction_type = "Circle";
                          alert("Circle is not supported by GeoJSON");
                          return;
                          break;
                      case 4:
                          interaction_type = "Circle";
                          geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
                          break;
                      case 5:
                          interaction_type = "LineString";
                          maxPoints = 2;
                          geometryFunction = function(coordinates, geometry) {
                              if (!geometry) {
                                  geometry = new ol.geom.Polygon(null);
                              }
                              var start = coordinates[0];
                              var end = coordinates[1];
                              geometry.setCoordinates([
                                  [start, [start[0], end[1]], end, [end[0], start[1]], start]
                              ]);
                              return geometry;
                          };
                  }
  
                  if(this.selected_interaction != 0) {
                      this.draw = new ol.interaction.Draw({
                          source: this.source,
                          type: /** @type {ol.geom.GeometryType} */ (interaction_type),
                          geometryFunction: geometryFunction,
                          maxPoints: maxPoints,
                          style : this.getTextStyle(-12)
                      });
  
                      this.map.addInteraction(this.draw);
  
                      this.draw.on('drawend', function(e) {
                          e.feature.setStyle(_this.getTextStyle(-12));
                      });
                  }
              },
  
              getGeoJSON: function(){
                  var allFeatures = this.draw_layer.getSource().getFeatures();
                  var format = new ol.format.GeoJSON();
                  return format.writeFeatures(allFeatures);
              },
  
              _changeInteraction: function(e){
                  this.addInteraction();
              },
  
210f1212   Luigi Serra   updates
350
351
352
353
354
              _handleMapLayerClick: function(e){
                  this.satLayer.setVisible(!this.satLayer.getVisible());
  
              },
  
edda4294   Luigi Serra   updates
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
              getTextStyle: function(offsetX) {
                  return new ol.style.Style({
                      fill: new ol.style.Fill({
                          color: 'rgba(255, 255, 255, 0.6)'
                      }),
                      stroke: new ol.style.Stroke({
                          color: '#ffff00',
                          width: 4
                      }),
                      text : new ol.style.Text({
                          fill : new ol.style.Fill({
                              color : '#000'
                          }),
                          stroke : new ol.style.Stroke({
                              color : '#fff',
                              width : 4
                          }),
                          text : this.label,
                          font : '20px Verdana',
                          offsetX : offsetX ? offsetX : 0,
                          offsetY : 12
                      })
                  });
              },
  
              resize : function(h,w){
                  this.$.map.style.height = h + "px";
                  this.$.map.style.width  = w + "px";
                  $("#OpenLayers_Map_2_OpenLayers_ViewPort").css('height',($(document).height() + "px"));
                  $("#OpenLayers_Map_2_OpenLayers_ViewPort").css('width',($(document).width() + "px"));
                  this.map.updateSize();
              }
          })
      </script>
  
  </dom-module>