forked from Maps4HTML/MapML.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-area.html
More file actions
161 lines (157 loc) · 6.18 KB
/
map-area.html
File metadata and controls
161 lines (157 loc) · 6.18 KB
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
<link rel="import" href="./bower_components/polymer/polymer.html">
<script src="scripts/lib/leaflet-src.js"></script>
<script src="scripts/lib/proj4-compressed.js"></script>
<script src="scripts/lib/proj4leaflet.js"></script>
<script src="scripts//mapml.js"></script>
<script src="scripts/URI.js"></script>
<dom-module id="map-area">
<!-- in polymer 1.0.x, styles must be outside of the <template> element
https://www.polymer-project.org/1.0/docs/migration.html#local-dom-template -->
<style>
:host {
display: none;
}
</style>
<script>
MapArea = Polymer({
is: "map-area",
properties: {
alt : {
type: String,
reflectToAttribute: true
},
coords : {
type: String,
reflectToAttribute: true
},
href : {
type: String,
reflectToAttribute: true
},
shape : {
type: String,
/* to HTML5's default, circle, poly and rect, add marker, line */
value: 'default',
reflectToAttribute: true
},
rel : {
type: String,
reflectToAttribute: true
},
type : {
type: String,
reflectToAttribute: true
},
target : {
type: String,
reflectToAttribute: true
}
},
ready: function() {
console.log(this.localName + '#' + this.id + ' is ready');
},
attached: function() {
console.log(this.localName + '#' + this.id + ' is attached');
// if the map has been attached, set this layer up wrt Leaflet map
if (this.parentElement._map) {
this._attachedToMap();
}
},
_attachedToMap: function() {
// need the map to convert container points to LatLngs
this._map = this.parentElement._map;
var map = this.parentElement._map;
// don't go through this if already done
if (!this._feature) {
// compute the style properties to be applied to the feature
var options = this._styleToPathOptions(window.getComputedStyle(this)),
points = this.coords ? this._coordsToArray(this.coords): null;
if (this.shape === 'marker') {
if (this.alt) {
// Leaflet markers use the options.title as a tooltip
options['title'] = this.alt;
}
this._feature = L.marker(map.containerPointToLatLng(points[0]),options).addTo(map);
} else if (this.shape === 'circle') {
var pixelRadius = parseInt(this.coords.split(",")[2]),
pointOnCirc = L.point(points[0]).add(L.point(0,pixelRadius)),
latLngOnCirc = map.containerPointToLatLng(pointOnCirc),
latLngCenter = map.containerPointToLatLng(points[0]),
radiusInMeters = map.distance(latLngCenter, latLngOnCirc);
this._feature = L.circle(latLngCenter, radiusInMeters, options).addTo(map);
} else if (!this.shape || this.shape === 'rect') {
var bounds = L.latLngBounds(map.containerPointToLatLng(points[0]), map.containerPointToLatLng(points[1]))
this._feature = L.rectangle(bounds, options).addTo(map);
} else if (this.shape === 'line') {
this._feature = L.polyline(this._pointsToLatLngs(points),options).addTo(map);
} else if (this.shape === 'poly') {
this._feature = L.polygon(this._pointsToLatLngs(points),options).addTo(map);
} else if (this.shape === 'default') {
// whole initial area of map is a hyperlink
this._feature = L.rectangle(map.getBounds(),options).addTo(map);
}
if (this.shape !== 'marker' && this.alt) {
// other Leaflet features are implemented via SVG. SVG displays tooltips
// based on the <svg:title> graphics child element.
var title = L.SVG.create('title'),
titleText = document.createTextNode(this.alt);
title.appendChild(titleText);
this._feature._path.appendChild(title);
}
if (this.href) {
// conditionally act on click on an area link. If no link it should be an
// inert area, but Leaflet doesn't quite support this. For a full
// implementation, we could actually use an image map replete with area
// children which would provide the linking / cursor change behaviours
// that are familiar to HTML authors versed in image maps.
this._feature.on('click', function() {if (this.href) {window.open(this.href);}}, this);
}
}
},
detached: function() {
this._map.removeLayer(this._feature);
delete this._feature;
},
_coordsToArray: function (containerPoints) {
// returns an array of arrays of coordinate pairs coordsToArray("1,2,3,4") -> [[1,2],[3,4]]
for (var i=1, points = [], coords = containerPoints.split(",");i<coords.length;i+=2) {
points.push([parseInt(coords[i-1]),parseInt(coords[i])]);
}
return points;
},
_pointsToLatLngs: function(points) {
// points should be an array of nested container coordinates [[x1,y1],[x2,y2](,[xN,yN])]
var latLngArray = [];
if (this._map) {
for (var i=0,map = this._map;i<points.length;i++) {
latLngArray.push(map.containerPointToLatLng(points[i]));
}
}
return latLngArray;
},
_styleToPathOptions: function(style) {
var options = {};
if(style.stroke !== 'none') {
options['stroke'] = true;
options['color'] = style.stroke;
options['opacity'] = style.strokeOpacity;
options['weight'] = parseInt(style.strokeWidth);
options['dashArray'] = style.strokeDasharray;
options['lineCap'] = style.strokeLinecap;
options['lineJoin'] = style.strokeLinejoin;
} else {
options['stroke'] = false;
}
if (style.fill !== 'none') {
options['fill'] = true;
options['fillColor'] = style.fill;
options['fillOpacity'] = style.fillOpacity;
options['fillRule'] = style.fillRule;
} else {
options['fill'] = false;
}
return options;
}
});
</script>
</dom-module>