我正在尝试将一些PHP / Mysql生成的JSON数据添加到传单地图上,但是它正在抛出leaflet.js:5未捕获的错误:无效的GeoJSON对象。然而,在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");
?>
OUTPUT:
{
"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://tools.ietf.org/html/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/