Home > Software engineering >  Extracting in PHP, Long and Lat from an address via API
Extracting in PHP, Long and Lat from an address via API

Time:12-28

I still try to extract Long and Lat from an address through the data.gouv API. I've got that code:

function extractLatLngFromAdd($address) {
$address = "paris";         
    $geodata = json_decode ('https://api-adresse.data.gouv.fr/search/?q='. $address ); 

    // extract latitude and longitude
    $result['latitude']  = $geodata->features[0]->geometry->coordinates[1];
    $result['longitude'] = $geodata->features[0]->geometry->coordinates[0];

return $result;
}
 
print_r(extractLatLngFromAdd($address));

Then, with this function, I hope extracting the array below:

$lab = array(
    "repartition" => array(
        array( 
            "latLng" => array( extractLatLngFromAdd('Paris') ),
            "name" => "Paris",
            "code" => "FR-75"
        ),
        array( 
            "latLng" => array( extractLatLngFromAdd('grenoble') ),
            "name" => "Grenoble",
            "code" => "FR-38"
        )
    )
);

echo json_encode($lab);

And the result is:

Array ( [latitude] => [longitude] => ) {"repartition":[{"latLng":[{"latitude":null,"longitude":null}],"name":"Paris","code":"FR-75"},{"latLng":[{"latitude":null,"longitude":null}],"name":"Grenoble","code":"FR-38"}]}

The latLng should be:

$lab = array(
        "repartition" => array(
            array( 
                "latLng" => array( 2.347, 48.859 ),
                "name" => "Paris",
                "code" => "FR-75"
            ),
            array( 
                "latLng" => array( 5.7243, 45.182081 ),
                "name" => "Grenoble",
                "code" => "FR-38"
            )
        )
    );

like that:

{"repartition":[{"latLng":[2.347,48.859],"name":"Paris","code":"FR-75"}

How to retrieve latitude and longitude from this function and how could I retrieve it in the $lab arrays?..

Thanks for your help.

CodePudding user response:

You forgot to call the external API with file_get_contents() (json_decode() can't do that alone). And in your $lab declaration, you don't need to put result in extra array, the result is already an array.

<?php

function extractLatLngFromAdd($address)
{
    $geodata = file_get_contents('https://api-adresse.data.gouv.fr/search/?q=' . $address);
    $geodata = json_decode($geodata);

    // extract latitude and longitude
    $result['latitude']  = $geodata->features[0]->geometry->coordinates[1];
    $result['longitude'] = $geodata->features[0]->geometry->coordinates[0];

    return $result;
}

$lab = array(
    "repartition" => array(
        array(
            "latLng" => extractLatLngFromAdd('Paris'),
            "name" => "Paris",
            "code" => "FR-75"
        ),
        array(
            "latLng" => extractLatLngFromAdd('grenoble'),
            "name" => "Grenoble",
            "code" => "FR-38"
        )
    )
);

print_r($lab);

Gives :

Array
(
    [repartition] => Array
        (
            [0] => Array
                (
                    [latLng] => Array
                        (
                            [latitude] => 48.859
                            [longitude] => 2.347
                        )
                    [name] => Paris
                    [code] => FR-75
                )
            [1] => Array
                (
                    [latLng] => Array
                        (
                            [latitude] => 45.182081
                            [longitude] => 5.7243
                        )
                    [name] => Grenoble
                    [code] => FR-38
                )
        )
)
  • Related