我成功地将PostGIS点特征添加到Leaflet中,但我在将PostGIS的线性特征添加到Leaflet中时遇到了麻烦。当我使用以下功能时,我能够在警报窗口中看到关系数据 alert(response)
但我似乎无法让它在地图上绘制。是否有一个特殊的方式,我需要符号化的线特征?
以下是我正在使用的代码(对于PostGIS点特征来说,这很好)。
//PHP
<?php
$dsn = "pgsql:host=xxxxx;dbname=postgres;port=5432";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
];
$pdo = new PDO($dsn, 'postgres', 'xxxxxx', $opt);
$result = $pdo->query("SELECT *, ST_AsGeoJSON(geom,5) AS geojson FROM testlyr");
$features=[];
foreach($result AS $row) {
unset($row['geom']);
$geometry=$row['geojson']=json_decode($row['geojson']);
unset($row['geojson']);
$feature=["type"=>"Feature", "geometry"=>$geometry, "properties"=>$row];
array_push($features, $feature);
}
$featureCollection=["type"=>"FeatureCollection", "features"=>$features];
echo json_encode($featureCollection);
?>
_
//JS
function mylineStyle(feature) {
return {
color: "#3388ff",
weight: 10,
};
}
$.ajax({
url: "load_attractions.php",
success: function (response) {
jsnCities = JSON.parse(response);
lyrCities = L.geoJSON(jsnCities, { style: mylineStyle }).addTo(map);
},
error: function (xhr, status, error) {
alert("ERROR: " + error);
},
});
-
//Output of console.log() JSON response
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"MultiLineString","coordinates":[[[-10616750.46022,3451527.81212],[-10616193.63558,3451197.53649],[-10615872.3906,3451772.85539],[-10615154.94348,3451495.84996],[-10615283.44147,3451080.34194],[-10615626.10278,3450696.79619]]]},"properties":{"id":1}}]}
Ivan Sanchez找出了坐标不是规格要求的latlong。现在工作完美了!