0

I am trying to fetch a comma separated list of coordinates from a database and then show them on a Mapbox GL JS map. By using the following FeatureCollection, I am able to show a Line between coordinates:

const route = {
 'type': 'FeatureCollection',
 'features': [
 {
 'type': 'Feature',
 'geometry': {
 'type': 'LineString',
 'coordinates': [
 [0.05505231549481921,50.795082814869524], [0.054870226332090315,50.79614745729873]
 ]
 }
 }
 ]
 };

This simply draws a line between the two coordinates shown. However using a PHP script to fetch from a MySQL database a string of coordinates, I cannot get the line to show.

<?php
 $array["routeCoords"] = $pointArray; // [0.05505231549481921,50.795082814869524], [0.054870226332090315,50.79614745729873]
 return json_encode($array);

The problem I am facing is that the Mapbox FeatureCollection doesn't recognise the coordinates in the same way as if they were printed within the block itself.

URL of Output:

{"routeCoords":"[0.05505231549481921,50.795082814869524],[0.054870226332090315,50.79614745729873]"}

Jquery code to handle and display the coordinates:

const payload = await fetch(URL); //Url returns JSON data above.
const data = await payload.json();
let coords = [];
$.each(data.routeCoords, function(index, value){
 coords.push(value);
});
coords = JSON.stringify(coords);
const route = {
 'type': 'FeatureCollection',
 'features': [
 {
 'type': 'Feature',
 'geometry': {
 'type': 'LineString',
 'coordinates': [
 coords
 ]
 }
 }
 ]
};

Just getting started but the core of my product will store the LineString coordinates in a database and return them depending on a route, so it is dynamic. There will be other (non geographical) data returned in the same JSON payload so the above is simply cut down.

Hopefully an obvious mistake.

asked Oct 30, 2025 at 14:38
3
  • coords = JSON.stringify(coords) - why? In the first code, you passed an array of arrays for coordinates - now you are passing a string value. Commented Oct 30, 2025 at 14:55
  • Problem i'm facing is I'm getting an error in console: coord must be GeoJSON Point or an Array of numbers when I pass an array to the coordinates. For example now I have an array and each of the [Long, Lat] formatted Coordinates as individual values in the array, and trying to use that array variable in the Feature code throws that error, despite it being an array of Lon/Lat coords. Commented Oct 30, 2025 at 17:42
  • Recon I should probably create the whole Feature as JSON on the server side rather than just the coordinates and see how that goes... Commented Oct 30, 2025 at 17:59

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.