I have a script that submits parameters via JS $.ajax and on my PHP page I query db and return matched data.
echo json_encode(array("matches" => $match));
It works fine, but now I need to add coordinates date as lat/lon pairs. So I am able to get this via a loop but I'm not sure how to return it back to my script.
$crds = array();
foreach ($data as $row) {
$lat = $row['lat'];
$lon = $row['lon'];
$key = $row['key'];
$crds[$key] = $lat.'|'.$lon;
...
}
echo json_encode(array("coords" => $crds));
When the data comes back to my script I can see in response:
{"coords":["44.76315|-101.8113","41.76313|-111.8993","40.76299|-112.8994"]}
I wasn't sure how to format it so I joined it with a pipe which is probably a bad idea.Also in my script I need to loop through it to output an array that will look something like this:
markerArray.push(L.marker([44.76315,-101.8113]));
markerArray.push(L.marker([41.76313,-111.8993]));
markerArray.push(L.marker([40.76299,-112.8994]));
Feel like I'm not making any progress and am making bad code choices... Need some help to untangle this. Thanks.
The data already looks like the correct structure, so just respond with it as-is:
echo json_encode(array("coords" => $data));
Then in js loop over it to add each one to markerArray (data being the ajax response).
data.coords.forEach((coord) => markerArray.push(L.marker([coord.lat,coord.lon]));