-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet14-usenewapi.html
82 lines (63 loc) · 2.59 KB
/
leaflet14-usenewapi.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<title>Map</title>
<!-- Load Leaflet.js and styles from a CDN -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-ajax/2.1.0/leaflet.ajax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style type="text/css">
#map {width: 100%; height: 600px;}
</style>
</head>
<body>
<!-- The div where the map goes -->
<div id="map"></div>
<!-- Map javascript -->
<script type="text/javascript">
// Create a map
var map = L.map('map')
.setView([52.2015, 0.127], 17); // Easy way to work out lat/lon/zoom is http://www.openstreetmap.org/
// Add tile background
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
function loadData () {
// Add the GeoJSON data in the file to the map
var url = 'http://leaflet/leaflet13-apigeojson.php';
var params = {
key: 'b2b70892933aa9a8',
limit: 50,
fields: 'id,latitude,longitude,caption,username,categoryId,categoryName',
bbox: map.getBounds().toBBoxString()
};
// Obtain over AJAX
$.ajax({
type: "GET",
data: params,
url: url,
dataType: 'json',
success: function (data) {
L.geoJson(data, {
// Set popup
onEachFeature: function (feature, layer) {
//console.log(feature.properties);
var popupContent = '<p><strong>' + feature.properties.caption + '</strong></p>';
popupContent += '<p>By: ' + feature.properties.username + '</p>';
popupContent += '<p>Category: ' + feature.properties.categoryId + '</p>';
layer.bindPopup(popupContent);
}
}).addTo(map);
}
});
}
// Initial load
loadData();
// Set a handler to reload when moving the map
map.on ('moveend', function (e) {
loadData();
});
</script>
</body>
</html>