尝试使用PHP格式化GeoJSON (多边形)几何部分的数据
$sql = "SELECT ST_AsGeoJSON(`geom`) as Geo FROM `usa` WHERE 1";返回如下的记录:
{"type": "Polygon", "coordinates": [[[-101.4073933...到目前为止,我的代码尝试了格式化几何图形块的3种不同的方式,都失败了。
while ($res = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$msg [] = array(
'type' => 'Feature',
'geometry' =>
$res['Geo'],
/// ERROR "geometry" member should be object, but is an String instead
//"geometry":"{\"type\": \"Polygon\", \"coordinates\": [[[-104.05361517875079, 41.698
'geometry' => array(
$res['Geo'],
), ///ERROR "geometry" member should be object, but is an Array instead
//"geometry":["{\"type\": \"Polygon\", \"coordinates\": [[[-104.05361517875079, 41.6
'geometry' => array(
'type' => $res['Geo'].['type'],
'coordinates' => $res['Geo'].['coordinates'],
),
//"geometry":{"type":"{\"type\": \"Polygon\", \"coordinates\": [[[[-122.4020155875262, 48.22521它最终看起来应该是这样的:
"geometry": { "type": "Polygon", "coordinates": [[[ -73.3450469发布于 2018-01-29 13:52:58
顾名思义,ST_AsGeoJSON返回JSON。这意味着$res['Geo']是一个字符串,一个表示GeoJSON对象的JSON字符串。如果将该字符串放入PHP数组(稍后为json_encode ),则将生成字符串的JSON编码。相反,您需要的是数组或对象,然后可以使用json_encode,它将是JSON中的对象,而不是字符串。
因此,JSON-解码从数据库获取PHP对象的值:
$msg[] = array(
'type' => 'Feature',
'geometry' => json_decode($res['Geo']),
...
);稍后,当JSON编码$msg时,它将如下所示:
"geometry": {"type": "Polygon", "coordinates": ...}https://stackoverflow.com/questions/48426813
复制相似问题