-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathol-map-functions.js
180 lines (163 loc) · 4.85 KB
/
ol-map-functions.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
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
/*
Functions
https://nominatim.openstreetmap.org/search?q=Warszaw&format=json
*/
// Get address from location
function ReverseGeocoding(lon, lat) {
fetch('https://nominatim.openstreetmap.org/reverse?format=json&lon=' + lon + '&lat=' + lat).then(function(response) {
return response.json();
}).then(function(json) {
console.log(json);
if(json != undefined){
// document.getElementById('address').innerHTML = json.display_name;
}
})
}
// Get location from address
function LocationGeocoding(address) {
fetch('https://nominatim.openstreetmap.org/search?q=' + address + '&format=json').then(function(response) {
console.log("Geocoding response :: ", response);
return response.json();
}).then(function(json) {
if(json != undefined){
// document.getElementById('location').innerHTML = json[0].lat + ' ' + json[0].lon;
var obj = json[0];
console.log(obj);
console.log(obj.lat, obj.lon);
SetMarker(obj);
}
})
}
function getLatLng(e){
console.log(e.value);
LocationGeocoding(e.value);
}
function getLatLngClick(e){
var e = document.getElementById("address");
console.log(e.value);
LocationGeocoding(e.value);
}
function PointToLonLat(evt)
{
var coordinate = ol.proj.toLonLat(evt.coordinate).map(function(val) {
return val.toFixed(6);
});
console.log("Coordinates: ", coordinate, evt.coordinate);
var lon = document.getElementById('lon').value = coordinate[0];
var lat = document.getElementById('lat').value = coordinate[1];
console.log("Coordinates Lon Lat: ", lon, lat);
return coordinate;
}
function CenterMap(long, lat, zoom = 5)
{
console.log("New position Long: " + long + " Lat: " + lat);
map.getView().setCenter(ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857'));
map.getView().setZoom(zoom);
}
function SimpleMarker(lon, lat)
{
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat]))
})
]
})
});
map.addLayer(layer);
}
function SetMarker(evt)
{
// clear
MapSource.clear();
var feature = new ol.Feature({
// Object
geometry: new ol.geom.Point(ol.proj.fromLonLat([evt.lon, evt.lat])),
// Event
//geometry: new ol.geom.Point(evt.coordinate)
name: 'Marker text',
desc: '<label>Details</label> <br> Latitude: ' + evt.lat + ' Longitude: ' + evt.lon
});
feature.setStyle(iconStyle);
MapSource.addFeature(feature);
// Center
CenterMap(evt.lon, evt.lat);
// Set div
SetDivLonLat(evt.lon, evt.lat);
}
function SetDivLonLat(lon,lat)
{
var loc = document.getElementById('location');
loc.value = lon + ',' + lat;
console.log(loc, lon, lat);
}
function PopUp(lon, lat)
{
var location = new ol.geom.Point(lon, lat).transform('EPSG:4326', 'EPSG:3857');
var container = document.getElementById('map-popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
var overlay = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
overlay.setPosition(ol.proj.fromLonLat([lon, lat]));
map.addOverlay(overlay);
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};
}
// Show marker
function MarkerOnTop(feature, show = false)
{
var style = feature.getStyle();
if(show){
style.zIndex = 9999;
style.zIndex_ = 9999;
}else{
style.zIndex = 999;
style.zIndex_ = 999;
}
console.log(style);
feature.setStyle(style);
}
// Animate marker from faeture
function AnimatePoint(feature, distance = 50, speed = 5)
{
console.log("Geometry: ", feature.getGeometry().getCoordinates());
// var point = new ol.geom.Point(ol.proj.fromLonLat([evt.lon, evt.lat]));
// Coordinates
var c = feature.getGeometry().getCoordinates();
var start = c[1];
var end = start + distance;
var curr = start;
var up = true;
window.setInterval(() => {
if(up == true)
{
curr = curr + speed;
var pos = [c[0], curr];
// console.log("Current: ", pos);
feature.getGeometry().setCoordinates(pos);
if(curr > end)
{
up = false;
}
}else{
curr = curr - speed;
var pos = [c[0], curr];
// console.log("Current: ", pos);
feature.getGeometry().setCoordinates(pos);
if(curr < start)
{
up = true;
}
}
}, 35);
}