我在MySQL数据库中有一个表,它有一个几何字段(多边形)
现在我可以查询几何字段并获得这样的geojson:
SELECT ST_AsGeoJSON(b.shape) FROM `tbl_polygon` as b where ST_Intersects(b.shape, ST_GeomFromText('Polygon((28.0921 -26.11351,28.09210 -26.11288,28.0929502 -26.1128831,28.092950 -26.113510,28.0921027 -26.113510))',1))
在MySQL中,我得到了一个geojson:
{"type": "Polygon", "coordinates": [[[28.092521118...
现在这里我遇到了困难,我有一个leafletjs地图,想要将这个geojson显示为图层。
那么我怎样才能从外部的php文件中回显geojson,这样我就可以将geojson放到我的javascript中,并使用Leaflet的L.geoJSON来添加图层。
我不确定这是否会以这种方式运作。 任何建议都会很棒。 谢谢,
根据Majid Abbasi的回答(谢谢)。我试过了,但显然我错过了一些东西我只是得到一个没有错误的空白页面。谢谢马吉德。 这是我的index.php文件中的代码:
的index.php
<?php
require_once(conn.php);
$myquery = "SELECT ST_AsGeoJSON(b.shape) FROM `tbl_polygon` as b where ST_Intersects(b.shape, ST_GeomFromText('Polygon((28.092102706432343 -26.113510096462498,28.092102706432343 -26.11288390575381,28.092950284481045 -26.11288390575381,28.092950284481045 -26.113510096462498,28.092102706432343 -26.113510096462498))',1))";
$result = mysqli_query($connect, $myquery);?>
<div id="mapid" style="width: 600px; height: 400px;"></div>
var mymap = L.map('mapid').setView([28.092102706432343, -26.113510096462498], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(mymap);
var data = <?php echo $result; ?>
ployLayer = L.geoJson(data).addTo(map);
答案 0 :(得分:1)
您需要在PHP文件中定义一个JavaScript变量,并调用初始化函数来创建地图并将数据传递给:
<?php
require_once(conn.php);
$myquery = "SELECT ST_AsGeoJSON(b.shape) as jsonData FROM `tbl_polygon` as b where ST_Intersects(b.shape, ST_GeomFromText('Polygon((28.092102706432343 -26.113510096462498,28.092102706432343 -26.11288390575381,28.092950284481045 -26.11288390575381,28.092950284481045 -26.113510096462498,28.092102706432343 -26.113510096462498))',1))";
$result = mysqli_query($connect, $myquery);
$data = mysqli_fetch_fields(result);
$ST_AsGeoJSON = $data->jsonData;
?>
<script language="javascript">
var data = <?php echo $ST_AsGeoJSON; ?>;
</script>
var mymap = L.map('mapid').setView([28.092102706432343, -26.113510096462498], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(mymap);
ployLayer = L.geoJson(data).addTo(map);
请注意,您应该在此脚本之前运行查询并获取数据并将其放在$ ST_AsGeoJSON上。
并且应该在你的javascript中创建map的初始化方法。