I am trying to get results of spatial data from mysql into json form and I get this output for the same linestring:
`[[-105.00341892242432, 39.75383843460583],[-105.0008225440979, 39.751891803969535]] `
But I need to get this:
`[{" coordinates":[[-105.00341892242432, 39.75383843460583], [-105.0008225440979, 39.751891803969535]]}`
Could anyone help me with this?
Here is the code:
<?php
require_once("caroom_db.php");
header("Content-Type: application/json");
//$l1 = '';
$ll1 = array();
//$lt = array();
$json_response = array();
$ll =0;
//echo $l;
$jsonData = '[';
$num_point="SELECT NumPoints(geo_type) FROM line_park WHERE id='2'";
$query = mysqli_query($cn, $num_point) or die (mysqli_error($cn));
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$ll = $row["NumPoints(geo_type)"];
//echo $ll;
}
for ($i=1; $i<=$ll; $i++) {
$l1="SELECT X(PointN(geo_type,$i)), Y(PointN(geo_type,$i)) FROM line_park";
$query = mysqli_query($cn, $l1) or die (mysqli_error($cn));
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$ll1[0] = $row["X(PointN(geo_type,$i))"];
$ll1[1] = $row["Y(PointN(geo_type,$i))"];
array_push($json_response,$ll1);
}
}
echo json_encode($json_response);
fclose($cn);
?>
Thank you in advance!
Ok since my comment solved your problem I'm posting it as an answer so you can accept. Try to convert an array to json using php.net/manual/en/function.json-encode.php pushing your values into an array would be simpler.
To add coodrinates
just do string concat
$jsonData = json_encode($json_response);
$jsonData = '{"coordinates":' . $jsonData . '}';
Use another array key. For example,
json_encode(array('coordinates' => $response));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.