【问题标题】:Leaflet geoJSON not valid however it is传单 geoJSON 无效,但它是
【发布时间】:2018-03-03 17:42:27
【问题描述】:

我正在尝试将一些 PHP / Mysql 生成的 JSON 数据添加到传单地图上,但是它正在抛出 leaflet.js:5 Uncaught Error: Invalid GeoJSON object。但是,在 JSONLint 上进行验证后,它说 geojson 是有效的,任何建议都将不胜感激,谢谢。我似乎无法弄清楚问题可能是什么。

地图加载

     $.ajax({
type: "POST",
url: 'results.json',
dataType: 'json',
success: function (response) {
    geojsonLayer = L.geoJson(response).addTo(mymap);
    mymap.fitBounds(geojsonLayer.getBounds());
    $("#info").fadeOut(500);
}
});

GeoJSON 生成

<?php
header('Content-type: text/plain');
$username = "root";
$password = "";
$hostname = "localhost";    
$database = "c9";
// Opens a connection to a mySQL server
$connection=mysql_connect ($hostname, $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}
// json output - insert table name below after "FROM"
$query = 'SELECT * FROM crimes';
$dbquery = mysql_query($query);
if(! $dbquery )
{
  die('Could not get data: ' . mysql_error());
}
// Parse the dbquery into geojson 
// ================================================
// ================================================
// Return markers as GeoJSON
$geojson = array(
    'type'      => 'FeatureCollection',
 );
while($row = mysql_fetch_assoc($dbquery)) {
    $feature = array(
        'category' => $row['crime'],
      'location' => array(
        'coordinates' => array((float)$row['Latitude'],(float)$row['Longitude'])
            ),
        'streetname' => $row['streetname'],
        'crimeID' => $row['crimeID'],
        'resolution' => $row['resolution'],
        'outcomedate' => $row['outcomedate']

        );
    array_push($geojson, $feature);
};
mysql_close($connection);
// // Return routing result
    header("Content-Type:application/json",true);
    echo json_encode($geojson);
    $fp = fopen('results.json', 'w');
fwrite($fp, json_encode($geojson));
fclose($fp);
header("Location: ../add_crime.php");
?>

输出:

{
    "type": "FeatureCollection",
    "0": {
        "category": "antisocialbehaviour",
        "location": {
            "coordinates": [53.8008, 1.5491]
        },
        "streetname": "street test",
        "crimeID": "1",
        "resolution": "localres",
        "outcomedate": "2015-05-05"
    },
    "1": {
        "category": "Arson",
        "location": {
            "coordinates": [53.8008, 1.5491]
        },
        "streetname": "60 st wilfrids grove",
        "crimeID": "2",
        "resolution": "Offender deprived of property",
        "outcomedate": "2015-05-05"
    },
    "2": {
        "category": "Arson",
        "location": {
            "coordinates": [53.8008, 1.5491]
        },
        "streetname": "60 st wilfrids grove",
        "crimeID": "3",
        "resolution": "Offender deprived of property",
        "outcomedate": "2015-05-05"
    },
    "3": {
        "category": "Arson",
        "location": {
            "coordinates": [53.8008, 1.5491]
        },
        "streetname": "Rookwood Road",
        "crimeID": "4",
        "resolution": "Offender deprived of property",
        "outcomedate": "2015-05-05"
    }
}

【问题讨论】:

    标签: javascript leaflet geojson


    【解决方案1】:

    GeoJSON 格式是一种特殊的 JSON 格式。

    它有它的own specification,您展示的示例数据不符合该规范。

    您可以在此处查看符合标准的 GeoJSON 对象的示例:https://www.rfc-editor.org/rfc/rfc7946#section-1.5

    {
       "type": "FeatureCollection",
       "features": [{
           "type": "Feature",
           "geometry": {
               "type": "Point",
               "coordinates": [102.0, 0.5]
           },
           "properties": {
               "prop0": "value0"
           }
       }, {
           "type": "Feature",
           "geometry": {
               "type": "LineString",
               "coordinates": [
                   [102.0, 0.0],
                   [103.0, 1.0],
                   [104.0, 0.0],
                   [105.0, 1.0]
               ]
           },
           "properties": {
               "prop0": "value0",
               "prop1": 0.0
           }
       }, {
           "type": "Feature",
           "geometry": {
               "type": "Polygon",
               "coordinates": [
                   [
                       [100.0, 0.0],
                       [101.0, 0.0],
                       [101.0, 1.0],
                       [100.0, 1.0],
                       [100.0, 0.0]
                   ]
               ]
           },
           "properties": {
               "prop0": "value0",
               "prop1": {
                   "this": "that"
               }
           }
       }]
    }
    

    您有几个专用的 GeoJSON linting 工具,例如http://geojsonlint.com/

    【讨论】:

    • 感谢您的回复,我将如何通过 PHP 生成这个我正在努力调整代码,谢谢。
    • 针对这个特定问题使用 PHP 标签提出一个新问题,您很可能会得到更好的答案。
    猜你喜欢
    • 1970-01-01
    • 2015-08-11
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多