-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_photon-with-ban.html
117 lines (95 loc) · 3.43 KB
/
demo_photon-with-ban.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
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
<!doctype html>
<html lang="fr">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet-control-geocoder@3.1.0/dist/Control.Geocoder.css" />
<script src="https://unpkg.com/leaflet-control-geocoder@3.1.0/dist/Control.Geocoder.js"></script>
<style type="text/css">
body {
margin: .5em;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
p {
margin: .4em;
}
#map {
height: 60vh;
width: 100%;
}
</style>
</head>
<body>
<h1>leaflet-control-geocoder Photon & BAN</h1>
<p>Le geocodeur <code>Photon</code> fourni avec le plugin <a href="https://github.com/perliedman/leaflet-control-geocoder">Leaflet Control
Geocoder</a>
peut fonctionner avec le service <a href="https://adresse.data.gouv.fr/">https://adresse.data.gouv.fr/</a>.
</p>
<p>
Il faut le configurer ainsi <small>(<em>Javascript</em>)</small> :
</p>
<pre><code>
const geocoder = L.Control.Geocoder.photon({
serviceUrl: 'https://api-adresse.data.gouv.fr/search',
reverseUrl: 'https://api-adresse.data.gouv.fr/reverse',
nameProperties: ['housenumber', 'street', 'postcode', 'city', 'context']
});
</code></pre>
<p><small><em>Les données du résultat sont affichées dans la console Javascript du navigateur.</em></small></p>
<div id="map"></div>
<p style="text-align: center;">
<a href="https://cyrille37.github.io/leaflet-control-geocoder-banfrance/">le projet sur github</a>
</p>
<script type="text/javascript">
const map = L.map('map', {
scrollWheelZoom: true,
}).setView([47.38254, 0.69583], 12);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
const geocoder = L.Control.Geocoder.photon({
serviceUrl: 'https://api-adresse.data.gouv.fr/search',
reverseUrl: 'https://api-adresse.data.gouv.fr/reverse',
nameProperties: ['housenumber', 'street', 'postcode', 'city', 'context']
});
const control = L.Control.geocoder({
collapsed: false,
suggestMinLength: 4,
suggestTimeout: 500,
geocoder: geocoder,
//defaultMarkGeocode: false
})
.addTo(map);
/**
* Événement déclenché au choix d'une adresse parmi celles proposées par "L.Control.geocoder".
*/
control.on('markgeocode', e => {
console.debug('Geocode result:', e.geocode);
console.debug('Geocode result.name:', e.geocode.name);
});
let marker;
/**
* Pour retrouver l'addresse d'un point sur la carte (geocodage inverse).
*/
map.on('click', function (e) {
geocoder.reverse(e.latlng, map.options.crs.scale(map.getZoom())).then((results) => {
console.debug('Reverse results:', results);
var r = results[0];
if (r) {
console.debug('Reverse result.name:', r.name);
if (marker) {
marker.
setLatLng(r.center).
setPopupContent(r.html || r.name).
openPopup();
} else {
marker = L.marker(r.center).bindPopup(r.name).addTo(map).openPopup();
}
}
})
})
</script>
</body>
</html>