Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move script to separate js file, add logging for bad adresses #102

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions app/static/js/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var map = L.map('mapid').setView([
51.0231119, 3.7102741
], 14);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);


function performRequest(url, location, success_callback) {
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();
}

let marker_icon = L.icon({
iconUrl: "/static/images/marker-icon.png",
shadowUrl: "/static/images/marker-shadow.png"
});

let callback = function OSMCallBack(location, data) {
var lat, lon;
if (data.features.length >= 1) {
var place = data.features[0].properties;
lat = data.features[0].geometry.coordinates[1];
lon = data.features[0].geometry.coordinates[0];

let marker = L.marker([lat, lon], {
icon: marker_icon
}).addTo(map)
.bindPopup(location.name + ', ' + location.address);

marker.on('mouseover', function(env) {
marker.openPopup();
});
marker.on('mouseout', function(env) {
marker.closePopup();
});
} else {
console.log(`Location ${JSON.stringify(location, null, 2)} returned no features, are you sure this is a valid address?`);
}
};

function loadmap(locations) {
for (let loc of locations) {
let request_uri = "https://photon.komoot.de/api/?limit=1&q=" + loc.address;
performRequest(request_uri, loc, callback);
};
}
77 changes: 11 additions & 66 deletions app/templates/maps.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,19 @@
{% block scripts %}
{{super()}}
<script src="{{ url_for('static', filename='js/leaflet.js')}}"></script>

<script src="{{ url_for('static', filename='js/map.js' ) }}"></script>
<script>
var map = L.map('mapid').setView([
51.0231119,3.7102741], 14);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);


function performRequest(url, location, success_callback) {
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 () {
// There was a connection error of some sort
};
request.send();
}

let marker_icon = L.icon({
iconUrl: "{{url_for('static', filename='images/marker-icon.png')}}",
shadowUrl: "{{url_for('static', filename='images/marker-shadow.png')}}"
});

let callback = function OSMCallBack(location, data){
var lat, lon;
if (data.features.length >= 1 ) {
var place = data.features[0].properties;
lat = data.features[0].geometry.coordinates[1];
lon = data.features[0].geometry.coordinates[0];

let marker = L.marker([lat, lon], {icon: marker_icon}).addTo(map)
.bindPopup(location.name + ', ' + location.address);

marker.on('mouseover', function(env) {
marker.openPopup();
});
marker.on('mouseout', function(env){
marker.closePopup();
});
}
};
let locations = [];
let loc = {};


let locations = [];
let loc = {};
{% for loc in locations -%}

loc = { "address" : "{{loc.address}}",
"name" : "{{loc.name}}"
};

locations.push(loc);

{%- endfor %}

for (let loc of locations) {
performRequest("https://photon.komoot.de/api/?limit=1&q=" + loc.address, loc, callback)
{% for loc in locations -%}
loc = {
"address": "{{loc.address}}",
"name": "{{loc.name}}"
};
locations.push(loc);
{%- endfor %}


loadmap(locations);
</script>
{% endblock %}
{% endblock %}