我想在我的网站上动态生成的开放街道地图上显示来自巴黎开放数据 (https://opendata.paris.fr/) 的数据。
我有兴趣拥有几个可自定义的字段(在
refine
中使用),例如:
我想要生成的典型表示是基于事件地理定位的点(字段
coordonnes
或 longitude
和 latitude
),如下所示:
https://www.onisr.securite-routiere.gouv.fr/cartographie-des-accidents-metropole-dom-tom
你能帮我吗?
我在 2023 年 11 月 4 日:
我使用此代码来显示标记:
<html><body>
<div id="mapdiv"></div>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script>
map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());
var lonLat = new OpenLayers.LonLat(2.335867, 48.862943)
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var zoom=16;
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(lonLat));
map.setCenter (lonLat, zoom);
</script>
</body></html>
如何添加从以下位置检索到的标记: https://opendata.paris.fr/api/explore/v2.1/catalog/datasets/accidentologie0/records?limit=20 ?
我可以用以下代码解决我的问题:
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>OpenStreetMap</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
<!-- CSS -->
<style>
body{
margin:0
}
#maCarte{
height: 100vh;
}
</style>
</head>
<body>
<div id="maCarte"></div>
<!-- Fichiers Javascript -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>
<script>
// On initialise la carte
var carte = L.map('maCarte').setView([48.825487, 2.338575], 15);
// On charge les "tuiles"
L.tileLayer('https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png', {
// Il est toujours bien de laisser le lien vers la source des données
attribution: 'données © <a href="//osm.org/copyright">OpenStreetMap</a>/ODbL - rendu <a href="//openstreetmap.fr">OSM France</a>',
minZoom: 1,
maxZoom: 20
}).addTo(carte);
var mortIcon = L.icon({
iconUrl: 'mort_dot.png',
iconSize: [20, 20],
shadowSize: [20, 20],
shadowAnchor: [22, 94]
});
var legerIcon = L.icon({
iconUrl: 'leger_dot.png',
iconSize: [20, 20],
shadowSize: [20, 20],
shadowAnchor: [22, 94]
});
var graveIcon = L.icon({
iconUrl: 'grave_dot.png',
iconSize: [20, 20],
shadowSize: [20, 20],
shadowAnchor: [22, 94]
});
var inconnuIcon = L.icon({
iconUrl: 'inconnu_dot.png',
iconSize: [20, 20],
shadowSize: [20, 20],
shadowAnchor: [22, 94]
});
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = () => {
// La transaction est terminée ?
if(xmlhttp.readyState == 4){
// Si la transaction est un succès
if(xmlhttp.status == 200){
// On traite les données reçues
let donnees = JSON.parse(xmlhttp.responseText)
// On boucle sur les données (ES8)
Object.entries(donnees.results).forEach(results => {
let marker
switch (results[1].gravite) {
case 'Blessé léger':
marker = L.marker([results[1].latitude, results[1].longitude], {icon: legerIcon}).addTo(carte);
break;
case 'Blessé hospitalisé':
marker = L.marker([results[1].latitude, results[1].longitude], {icon: graveIcon}).addTo(carte);
break;
case 'Tué':
marker = L.marker([results[1].latitude, results[1].longitude], {icon: mortIcon}).addTo(carte);
break;
default:
marker = L.marker([results[1].latitude, results[1].longitude], {icon: inconnuIcon}).addTo(carte);
}
let categorie_victime
if (results[1].categorie == results[1].victime_type) {
categorie_victime = results[1].categorie
} else {
categorie_victime = results[1].categorie + " " + results[1].victime_type
}
marker.bindPopup("<b>" + results[1].annee + "</b> : " + categorie_victime + ", " + results[1].age + " ans (" + results[1].idusager + ")")
})
} else {
console.log(xmlhttp.statusText);
}
}
}
xmlhttp.open("GET", "https://opendata.paris.fr/api/explore/v2.1/catalog/datasets/accidentologie0/records?refine=iris%3A%22Parc%20de%20Montsouris%201%22&refine=iris%3A%22Parc%20de%20Montsouris%202%22&refine=iris%3A%22Parc%20de%20Montsouris%203%22&refine=iris%3A%22Parc%20de%20Montsouris%204%22&refine=iris%3A%22Parc%20de%20Montsouris%205%22&refine=iris%3A%22Parc%20de%20Montsouris%206%22&refine=iris%3A%22Parc%20de%20Montsouris%207%22&refine=pim_bp%3A%22Paris%20Intra%20Muros%22&limit=100");
// &refine=pim_bp%3A%22Paris%20Intra%20Muros%22 //
xmlhttp.send(null);
</script>
感谢您提供的示例: https://nouvelle-techno.fr/articles/openstreetmap-inserer-des-marqueurs-depuis-une-base-de-donnees