-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
93 lines (85 loc) · 2.12 KB
/
main.js
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
import "ol/ol.css";
import KML from "ol/format/KML";
import Map from "ol/Map";
import Stamen from "ol/source/Stamen";
import VectorSource from "ol/source/Vector";
import View from "ol/View";
import { Circle as CircleStyle, Fill, Stroke, Style } from "ol/style";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
var styleCache = {};
var styleFunction = function (feature) {
// 2012_Earthquakes_Mag5.kml stores the magnitude of each earthquake in a
// standards-violating <magnitude> tag in each Placemark. We extract it from
// the Placemark's name instead.
var name = feature.get("name");
var magnitude = parseFloat(name.substr(2));
var radius = 5 + 20 * (magnitude - 5);
var style = styleCache[radius];
if (!style) {
style = new Style({
image: new CircleStyle({
radius: radius,
fill: new Fill({
color: "rgba(255, 153, 0, 0.4)"
}),
stroke: new Stroke({
color: "rgba(255, 204, 0, 0.2)",
width: 1
})
})
});
styleCache[radius] = style;
}
return style;
};
var vector = new VectorLayer({
source: new VectorSource({
url: "data/kml/2012_Earthquakes_Mag5.kml",
format: new KML({
extractStyles: false
})
}),
style: styleFunction
});
var raster = new TileLayer({
source: new Stamen({
layer: "toner"
})
});
var map = new Map({
layers: [raster, vector],
target: "map",
view: new View({
center: [0, 0],
zoom: 3
})
});
var info = $("#info");
info.tooltip({
animation: false,
trigger: "manual"
});
var displayFeatureInfo = function (pixel) {
info.css({
left: pixel[0] + "px",
top: pixel[1] - 15 + "px"
});
var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
return feature;
});
if (feature) {
info.attr("data-original-title", feature.get("name")).tooltip("show");
} else {
info.tooltip("hide");
}
};
map.on("pointermove", function (evt) {
if (evt.dragging) {
info.tooltip("hide");
return;
}
displayFeatureInfo(map.getEventPixel(evt.originalEvent));
});
map.on("click", function (evt) {
displayFeatureInfo(evt.pixel);
});