Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Returning coordinate pairs with data via json

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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]));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda