PDOStatement 到 Geojson

PDOStatement to Geojson

为了在 leaflet 中使用它,我需要将从 Mysql 查询的数据转换为 PDOgeojson 格式。

我找到了 here 一个用 json_encode()json 做的解决方案,但我找不到类似的 geojson_encode() 函数或算法 php.

那么有解决办法吗?

我通过阅读 this issue

解决了我的问题

要将 Mysql 中的查询数据转换为 geojson,只需尝试以下代码:

$geojson = array(
    'type'      => 'FeatureCollection',
   'features'  => array()
);

$reponses=$bdd->query('SELECT * FROM `nyc_taxi_data_2014` LIMIT 0,30 ');

    while ($data=$reponses->fetch())
    {
        $marker = array(
            'type' => 'Feature',
            'features' => array(
                'type' => 'Feature',
                'properties' => array(
                    'pickup_time' => "".$data['pickup_datetime']

                    ),
                "geometry" => array(
                    'type' => 'Point',
                    'coordinates' => array( 
                                    $data['pickup_longitude'],
                                    $data['pickup_latitude']
                    )
                )
            )
          );

    array_push($geojson['features'], $marker['features']);
    }

echo json_encode($geojson);