【问题标题】:Add GeoJson to Google Maps API - Uncaught not a Feature or FeatureCollection将 GeoJson 添加到 Google Maps API - Uncaught not a Feature 或 FeatureCollection
【发布时间】:2019-07-21 01:34:20
【问题描述】:

我尝试将 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];
}
?>

返回的是一个非常好的 GeoJson,它与 QGis 或任何其他 GIS 软件一起工作:-)

在另一个文件中,我有一个 jQuery/AJAX 技术来调用我的 PHP 文件,这是摘录:

var data = "";
$.ajax({
  type: "POST",
  url: 'file.php',
  data: data,
    success: function (data) {
    alert(data);
    var geojson = map.data.addGeoJson(data);
}})

但是成功并不在这里,因为我有这个消息:

如果我尝试像 var geojson = map.data.addGeoJson({Featurecollection...}); 这样直接 cpoy/粘贴 PHP 的 GeoJson 结果,那就行了!

这是我的 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"
            }
        }
    ]
}

你明白为什么这不起作用吗?

非常感谢。

【问题讨论】:

    标签: php jquery google-maps-api-3 geojson


    【解决方案1】:

    我找到了解决方案,我的 GeoJson 是 string 类型并且代码需要 object 所以我用这个操作进行了转换var geojson = map.data.addGeoJson(JSON.parse(data));

    就是这样:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-29
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2023-01-30
      相关资源
      最近更新 更多