首页 > 解决方案 > 到 Geojson 的 PDO 语句

问题描述

为了在传单中使用它,我需要将我从Mysql查询到的PDO数据转换为geojson格式。

我在这里找到了一个解决方案来为json执行此操作json_encode(),但我找不到geojson_encode()使用 php 的类比函数或算法。

那么有解决方案吗?

标签: phpjsongeojson

解决方案


我通过阅读这个问题解决了我的问题

要将您从 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);

推荐阅读