gpt4 book ai didi

javascript - 传单 geoJSON 无效,但它确实有效

转载 作者:行者123 更新时间:2023-12-01 02:22:38 24 4
gpt4 key购买 nike

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

map 加载

     $.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"
}
}

最佳答案

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/

关于javascript - 传单 geoJSON 无效,但它确实有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com