-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathleaflet-map-component.html
236 lines (205 loc) · 6.08 KB
/
leaflet-map-component.html
1
2
3
4
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
52
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!--
The `leaflet-map` element renders a [Leaflet](http://leafletjs.com/) map.
##### Example
<style>
leaflet-map {
height: 100%;
}
</style>
<leaflet-map longitude="77.2" latitude="28.4" zoom="12"></leaflet-map>
##### Example: Add markers & circles
<leaflet-map longitude="77.2" latitude="28.4" zoom="12">
<leaflet-marker longitude="77.2" latitude="28.4">
Marker
</leaflet-marker>
<leaflet-circle longitude="77.2" latitude="28.4" radius="300">
Circle
</leaflet-circle>
</leaflet-map>
@element leaflet-map
@blurb Element for rendering a leaflet map
@status alpha
@homepage http://prtksxna.github.io/leaflet-map-component/components/leaflet-map-component/
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="leaflet-import.html">
<link rel="import" href="leaflet-marker-component.html">
<link rel="import" href="leaflet-circle-component.html">
<link rel="import" href="leaflet-rectangle-component.html">
<link rel="import" href="leaflet-polyline-component.html">
<link rel="import" href="leaflet-polygon-component.html">
<polymer-element name="leaflet-map" attributes="tileServer latitude longitude zoom fitToMarkers options">
<template>
<style>
:host {
display: block;
position: relative;
}
#map {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
<link rel="stylesheet" href="../leaflet/dist/leaflet.css">
<div id="map"></div>
<content id="features" select="leaflet-marker, leaflet-circle, leaflet-rectangle, leaflet-polyline, leaflet-polygon"></content>
</template>
<script>
Polymer( 'leaflet-map', {
publish: {
/**
* The map's latitude coordinate.
*
* @attribute latitude
* @type number
* @default 0
*/
latitude: { value: 0, reflect: true },
/**
* The map's longitude coordinate.
*
* @attribute longitude
* @type number
* @default 0
*/
longitude: { value: 0, reflect: true },
/**
* The map's zoom level
*
* @attribute zoom
* @type number
* @default 10
*/
zoom: { value: 10, reflect: true }
},
/**
* The map's tile server URL
*
* @attribute tileServer
* @type string
* @default http://{s}.tile.osm.org/{z}/{x}/{y}.png
*/
tileServer: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
/**
* If set, the map is zoomed such that all elements in it are visible
*
* @attribute fitToMarkers
* @type boolean
* @default false
*/
fitToMarkers: false,
/**
* Pass extra configurations to the Map constructor
*
* @attribute options
* @type object
* @default {}
*/
options: {},
observe: {
latitude: 'updateCenter',
longitude: 'updateCenter'
},
attached: function () {
var baseLayer = L.tileLayer( this.tileServer );
var lat = this.latitude;
var lon = this.longitude;
var zoom = this.zoom;
var opts = {
center: new L.LatLng( this.latitude, this.longitude ),
zoom: this.zoom,
layers: [baseLayer]
};
if (typeof this.options === 'object')
Polymer.extend(opts, this.options);
this.map = new L.Map( this.$.map , opts);
this.map.on( 'zoomend', function ( event ) {
this.zoom = event.target.getZoom();
/**
* Fired when the map has stopped [zooming](http://leafletjs.com/reference.html#map-zoomend)
*
* @event zoomened
*/
this.fire( 'zoomend' );
}.bind( this ) );
this.map.on( 'moveend', function ( event ) {
var center = event.target.getCenter();
this.latitude = center.lat;
this.longitude = center.lng;
/**
* Fired when the map has stopped [moving](http://leafletjs.com/reference.html#map-moveend)
*
* @event moveend
*/
this.fire( 'moveend' );
}.bind( this ) );
this.updateFeatures();
},
zoomChanged: function () {
if ( this.map ) {
this.map.setZoom( this.zoom );
}
},
updateCenter: function () {
if ( this.map && this.latitude && this.longitude ) {
this.map.panTo( L.latLng( this.latitude, this.longitude ), { animate: true } );
}
},
updateFeatures: function () {
this.features = Array.prototype.slice.call( this.$.features.getDistributedNodes() );
this.onMutation( this, this.updateFeatures );
if ( this.features.length && this.map ) {
for ( var i = 0, f; f = this.features[i]; i++ ) {
f.map = this.map;
}
}
},
fitToMarkersChanged: function () {
if ( this.map && this.fitToMarkers) {
var bounds = [];
for( var i = 0, f; f = this.features[i]; i++ ) {
if ( f.latitude && f.longitude ) {
bounds.push( [ f.latitude, f.longitude ] );
} else if ( f.topleft && f.bottomright ) { // If it s a rectangle
bounds.push( f.topleft );
bounds.push( f.bottomright );
}
}
if ( bounds.length > 0 ) {
this.map.fitBounds( bounds );
this.map.invalidateSize();
}
}
},
/**
* Clears all elements from the map
*
* @method clear
*/
clear: function () {
for( var i = 0, f; f = this.features[i]; i++ ) {
this.map.removeLayer( f.feature );
f.map = null;
}
},
/**
* Returns a GeoJSON including all the features of the map
*
* @method toGeoJSON
*/
toGeoJSON: function () {
var geoJSON = {
"type": "FeatureCollection",
"features": []
};
for( var i = 0, f; f = this.features[i]; i++ ) {
geoJSON.features.push( f.feature.toGeoJSON() );
}
return geoJSON;
}
} );
</script>
</polymer-element>