在MongoDB坐标的传单地图上放置一个标记



我想在传单地图上放置标记,但来自数据库, 我想节省 mongodb 的纬度和长度,并在地图上显示它们像标记一样,这可能吗?

例如使用 JQuery Ajax 创建一个请求:

$.ajax({url: "/your_data_provider.php", success: function(result){
//result = JSON.parse(result); // If your result is not a json Object.
// It depends on what your data looks like
//Example 1: Lat and Lng has a own field in the db
result.forEach(function(data){
var lat = data.lat;
var lng = data.lng;
var marker = L.marker([lat, lng]).addTo(map);
});

//Example 2: you have one geojson-data string field "geo" in db
// Before inserting in db create a featuregroup `var fg = L.featureGroup();`
// and add all markers to the group `marker.addto(fg);`.
// Then you can call `var datageo = fg.toGeoJSON();` and add this datageo to the db in the field "geo"
result.forEach(function(data){
var geo = data.geo;
L.geoJSON(geo).addTo(map);
});
//https://leafletjs.com/examples/geojson/

},
error: function(xhr){
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}});
});

此外,您还需要一个数据提供程序。您可以创建 REST-APIwww.url.com/data/postions/或直接调用 php 文件。

PHP文件:

// I have never used MongoDB, you have to code your own request. I copied it.
<?php
header('Content-type:application/json;charset=utf-8');
try {
$mng = new MongoDBDriverManager("mongodb://localhost:27017");
$listdatabases = new MongoDBDriverCommand(["listDatabases" => 1]);
$res = $mng->executeCommand("admin", $listdatabases);
$databases = current($res->toArray());
$result = new array();
$x = 0;
foreach ($databases->databases as $el) {
$result[$x]['id'] = $el->id;
//Example 1:
$result[$x]['lat'] = $el->lat;
$result[$x]['lng'] = $el->lng;
//Example 2:
$result[$x]['geo'] = $el->geo;
$x++;
}
echo json_encode($result);
} catch (MongoDBDriverExceptionException $e) {
$error = new array();
$error['exception'] = $e->getMessage();
$error['line'] = $e->getLine();
echo json_encode($error);
}
?>

最新更新