-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
68 lines (64 loc) · 1.94 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
import 'ol/ol.css';
import ol from 'ol';
import proj from 'ol/proj';
import extent from 'ol/extent';
import Map from 'ol/map';
import View from 'ol/view';
import Vector from 'ol/source/vector';
import Heatmap from 'ol/layer/heatmap';
import Tile from 'ol/layer/tile';
import XYZ from 'ol/source/xyz';
import GeoJSON from 'ol/format/geojson';
import loadingstrategy from 'ol/loadingstrategy';
import OSM from 'ol/source/osm';
var vs = new Vector({
format: new GeoJSON(),
loader: function(ext, resolution, projection) {
var pt = proj.transform(extent.getCenter(ext), projection.getCode(), 'EPSG:4326');
var xhr = new XMLHttpRequest();
xhr.open('GET', '/api.php?long=' + pt[0] + '&lat=' + pt[1] + '&range=' + (extent.getWidth(ext) / proj.METERS_PER_UNIT.m / 1000) / 2);
var onError = function() {
vs.removeLoadedExtent(ext);
}
xhr.onerror = onError;
xhr.onload = function() {
if (xhr.status == 200) {
vs.clear();
vs.addFeatures(vs.getFormat().readFeatures(xhr.responseText, {
dataProjection: "EPSG:4326",
featureProjection: projection.getCode()
}));
} else {
onError();
}
}
xhr.send();
},
strategy: loadingstrategy.bbox
});
window.map = new Map({
target: 'heatmap',
layers: [
new Tile({
source: new OSM()
}),
new Heatmap({
weight: "1",
source: vs,
zIndex: 1
})
],
view: new View({
center: [0, 0],
zoom: 2
})
});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position) {
var vw = map.getView();
var pt = proj.transform([position.coords.longitude, position.coords.latitude], 'EPSG:4326', vw.getProjection().getCode());
vw.setCenter(pt);
vw.setZoom(8);
}