I'm trying to add some PHP / Mysql generated JSON data onto a leaflet map however it is throwing leaflet.js:5 Uncaught Error: Invalid GeoJSON object. however upon validation on JSONLint it says the geojson is valid any advice would be much appreciated thanks. I can't seem to figure out what the problem may be.
Map Load
$.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 generation
<?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"
}
}