Php 将GeoJson添加到Google地图API-未捕获不是功能或特性集合

Php 将GeoJson添加到Google地图API-未捕获不是功能或特性集合,php,jquery,google-maps-api-3,geojson,Php,Jquery,Google Maps Api 3,Geojson,我尝试将GeoJson层(使用PHP从Postgres/PostGIS生成)添加到我的Google Maps API中 为此,我有一个用于生成json的PHP文件: <?php $dbconn = pg_connect("host=**** port=**** dbname=**** user=**** password=****"); $result = pg_query($dbconn, "SELECT json_build_object('type','FeatureCollecti

我尝试将GeoJson层(使用PHP从Postgres/PostGIS生成)添加到我的Google Maps API中

为此,我有一个用于生成json的PHP文件:

<?php
$dbconn = pg_connect("host=**** port=**** dbname=**** user=**** password=****");
$result = pg_query($dbconn, "SELECT json_build_object('type','FeatureCollection','name','Postes techniques','crs',json_build_object('type','name','properties',json_build_object('name','urn:ogc:def:crs:OGC:1.3:CRS84')),'features',(SELECT jsonb_agg(json_build_object('type','Feature','properties',json_build_object('libelle',libelle),'geometry',(ST_AsGeoJSON(ST_Transform(geom,4326))::json))) FROM test)) AS GeoJson");
if (!$result) {
  echo "Error";
  exit;
}
while ($row = pg_fetch_row($result)) {
  echo $row[0];
}
?>
但成功并不在这里,因为我有这样一个信息:

如果我尝试直接cpoy/paste PHP的GeoJson结果,比如
var GeoJson=map.data.addGeoJson({Featurecollection…})这就是工作

这是我的GeoJson的摘录:

{
    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -0.975467289719626,
                    0.55607476635514
                ]
            },
            "properties": {
                "libelle": "test 1"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    0.400700934579439,
                    -0.161214953271028
                ]
            },
            "properties": {
                "libelle": "test 2"
            }
        }
    ]
}
你明白为什么这样不行吗


非常感谢。

我找到了解决方案,我的GeoJson是字符串类型,代码需要对象,因此我使用此操作进行转换
var GeoJson=map.data.addGeoJson(JSON.parse(data))

仅此而已:-)

{
    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -0.975467289719626,
                    0.55607476635514
                ]
            },
            "properties": {
                "libelle": "test 1"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    0.400700934579439,
                    -0.161214953271028
                ]
            },
            "properties": {
                "libelle": "test 2"
            }
        }
    ]
}