2019-05-30 08:29:08 +02:00
|
|
|
var map = L.map('mapid');
|
2019-04-04 20:41:43 +02:00
|
|
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
2020-02-29 17:23:20 +01:00
|
|
|
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
2019-04-04 20:41:43 +02:00
|
|
|
}).addTo(map);
|
|
|
|
|
|
|
|
|
2021-07-24 17:45:19 +02:00
|
|
|
let base_request_uri = "https://photon.komoot.io/api/?limit=1&q=";
|
2019-05-30 08:29:08 +02:00
|
|
|
|
2019-04-04 20:41:43 +02:00
|
|
|
function performRequest(url, location, success_callback) {
|
2020-02-29 17:23:20 +01:00
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open('GET', url, true);
|
|
|
|
request.onload = function() {
|
|
|
|
if (this.status >= 200 && this.status < 400) {
|
|
|
|
// Success!
|
|
|
|
var data = JSON.parse(this.response);
|
|
|
|
success_callback(location, data);
|
|
|
|
} else {
|
|
|
|
// We reached our target server, but it returned an error
|
|
|
|
}
|
|
|
|
};
|
|
|
|
request.onerror = function() {
|
|
|
|
console.log("Error requestion location coordinates");
|
|
|
|
// There was a connection error of some sort
|
|
|
|
};
|
|
|
|
request.send();
|
2019-04-04 20:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let marker_icon = L.icon({
|
2020-02-29 17:23:20 +01:00
|
|
|
iconUrl: "/static/images/marker-icon.png",
|
|
|
|
shadowUrl: "/static/images/marker-shadow.png",
|
|
|
|
iconAnchor: [12, 41]
|
2019-04-04 20:41:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let callback = function OSMCallBack(location, data) {
|
2020-02-29 17:23:20 +01:00
|
|
|
let lat, lon;
|
|
|
|
if (data.features.length >= 1) {
|
|
|
|
let place = data.features[0].properties;
|
|
|
|
lat = data.features[0].geometry.coordinates[1];
|
|
|
|
lon = data.features[0].geometry.coordinates[0];
|
2019-04-04 20:41:43 +02:00
|
|
|
|
2020-02-29 17:23:20 +01:00
|
|
|
let marker = L.marker([lat, lon], {
|
|
|
|
icon: marker_icon
|
|
|
|
}).addTo(map)
|
|
|
|
.bindPopup(location.name + ', ' + location.address, {offset: new L.Point(0, -16)}).on('click', function () {
|
|
|
|
let win = window.open(location.url, '_blank');
|
|
|
|
win.focus();
|
|
|
|
});
|
2019-04-04 20:41:43 +02:00
|
|
|
|
2020-02-29 17:23:20 +01:00
|
|
|
marker.on('mouseover', function(env) {
|
|
|
|
marker.openPopup();
|
|
|
|
});
|
|
|
|
marker.on('mouseout', function(env) {
|
|
|
|
marker.closePopup();
|
|
|
|
});
|
2019-05-30 08:29:08 +02:00
|
|
|
|
2020-02-29 17:23:20 +01:00
|
|
|
if (location.center) {
|
|
|
|
map.setView([lat, lon], 14);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log(`Location ${JSON.stringify(location, null, 2)} returned no features, are you sure this is a valid address?`);
|
|
|
|
}
|
2019-04-04 20:41:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function loadmap(locations) {
|
2020-02-29 17:23:20 +01:00
|
|
|
for (let loc of locations) {
|
|
|
|
let request_uri = base_request_uri + loc.address;
|
|
|
|
performRequest(request_uri, loc, callback);
|
|
|
|
}
|
2019-04-04 20:41:43 +02:00
|
|
|
}
|