-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
66 lines (52 loc) · 2.35 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Geo Search Example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.ie.css" /><![endif]-->
</head>
<body>
<h1>Super Simple GeoJSON Nearby API</h1>
<hr/>
<p>For more information, check out the <a href="https://github.com/iandees/geojson-api">code on Github</a>.</p>
<h3>Example</h3>
<hr/>
<p>
The map below is a quick example of the API in action. Click anywhere to find the 5 closest points.
</p>
<div id="map" style="width: 600px; height: 400px"></div><br/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
<script>
var map = L.map('map').setView([39.272, -96.715], 5);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
}).addTo(map);
var geojsonLayer = new L.GeoJSON([], {
onEachFeature: function (feature, layer) {
layer.bindPopup("<pre>" + JSON.stringify(feature.properties, null, 1) + "</pre>");
}
});
function onMapClick(e) {
console.log("You clicked the map at " + e.latlng.toString());
var xhrequest = new XMLHttpRequest();
var url= "/points/nearby?lat=" + +e.latlng.lat.toFixed(6) + "&lon=" + +e.latlng.lng.toFixed(6);
xhrequest.onreadystatechange = function() {
if (xhrequest.readyState == 4) {
if (xhrequest.status == 200) {
geojsonLayer.clearLayers();
var layerItems = JSON.parse(xhrequest.responseText);
geojsonLayer.addData(layerItems);
map.addLayer(geojsonLayer);
}
}
};
xhrequest.open('GET', url, true);
xhrequest.send(null);
}
map.on('click', onMapClick);
</script>
</body>
</html>