array ( array( "latLng" => array( xxx, xxx ), "name" => "Paris", "code" => "FR-75" /> array ( array( "latLng" => array( xxx, xxx ), "name" => "Paris", "code" => "FR-75" /> array ( array( "latLng" => array( xxx, xxx ), "name" => "Paris", "code" => "FR-75"/>

Public API how to use in php/json /jquery files

53 Views Asked by At

Here is the structure of my php file:

$lab = array(
"repartition" => array (
    array( 
        "latLng" => array( xxx, xxx ),
        "name" => "Paris",
        "code" => "FR-75"
        )
    ),
    array (
        "latLng" => array( xxx, xxx ),
        "name" => "Grenoble",
        "code" => "FR-38"
    )
)

I just wondering if I can retrieve the LatLng array from an address public API.

https://api-adresse.data.gouv.fr/search/?q=paris&limit=1

In the vectorMap, I was able to retrieve the appropriate values as below, but it's on MarkerClick:

onMarkerClick: function(event, index) {
    var city= data2[0].repartition[index].name;
    //var latt = data2[0].repartition[index].latLng[0];
    //var long = data2[0].repartition[index].latLng[1];

    $.getJSON("https://api-adresse.data.gouv.fr/search/?q=" + city+ "&limit=1", function (dt){
      var newlatt = dt.features[0].geometry.coordinates[1];
      var newlong = dt.features[0].geometry.coordinates[0];     
  })

But I wasn't able to retrieve it on the markers value. Markers is coded like that:

markers: data2[0].repartition,

My best way then is to create a function in php, but I don't know how to create it and retrieve the values in "latLng" => array( xxx, xxx ).

Thanks in advance for your help.

1

There are 1 best solutions below

1
Jaffar Esmaili On

The best way use restful api in php :

$postRequest    = file_get_contents("php://input");
$receiveRequest = json_decode(stripslashes(isset($_REQUEST['data']) ? $_REQUEST['data'] : $postRequest));
switch ($receiveRequest->type) {
    case 'repartition':
        $lab = array(
"repartition" => array (
    array( 
        "latLng" => array( xxx, xxx ),
        "name" => "Paris",
        "code" => "FR-75"
        )
    ),
    array (
        "latLng" => array( xxx, xxx ),
        "name" => "Grenoble",
        "code" => "FR-38"
    )
);
echo json_encode($lab['repartition']);
    
        break;
}