Search code examples
phpfunctiongoogle-mapsmapslatitude-longitude

Get lat and long


I'm building a function to get the latitude and the longitude from a google maps link. I got it working how it should work works how it should:

public function getLocaties(){
    $locations = $this->getObjects(68);

    foreach ($locations as $location) {
        $latAndLong = $this->getLatAndLong($locations ['data']['link']);
        $location['data']['latiude'] = $latAndLong['lat'];
        $location['data']['longtitude'] = $latAndLong['long'];
        var_dump($locatie);
    }

    return $locaties;
}

Now the var_dump prints the following:

`array(4) {
  ["id"]=>
  string(2) "69"
  ["type_id"]=>
  string(2) "24"
  ["parent_id"]=>
  string(2) "68"
  ["data"]=>
  array(13) {
    ["object_name"]=>
    string(28) "Hajé restaurant de Taveerne"
    ["object_createdate"]=>
    string(10) "1584437106"
    ["object_createuser"]=>
    string(3) "{4}"
    ["object_read"]=>
    string(3) "{4}"
    ["object_write"]=>
    string(3) "{4}"
    ["object_delete"]=>
    string(3) "{4}"
    ["object_update"]=>
    string(3) "{4}"
    ["object_display"]=>
    string(4) "tree"
    ["object_priority"]=>
    string(1) "1"
    ["data_html"]=>
    string(80) "<p>Haj&eacute; de Taveerne Oostvaardersdijk 01-09 8244 PA Lelystad Nederland</p>"
    ["link"]=>
    string(162) "https://www.google.com/maps/place/Haj%C3%A9+restaurant+de+Taveerne/@52.522116,5.437504,12z/data=!4m5!3m4!1s0x0:0x2a85d0586811bc85!8m2!3d52.522116!4d5.437504?hl=nl"
    ["latiude"]=>
    array(1) {
      [0]=>
      string(9) "52.522116"
    }
    ["longtitude"]=>
    array(1) {
      [0]=>
      string(8) "5.437504"
    }
  }
}`

Which is perfect but when i call the function in my index.php it removes the lat and the long from the array:

$locations = $blokken->getLocaties();
    var_dump($locations);

the var_dump prints he following:

    `array(1) {
  [0]=>
  array(4) {
    ["id"]=>
    string(2) "69"
    ["type_id"]=>
    string(2) "24"
    ["parent_id"]=>
    string(2) "68"
    ["data"]=>
    array(11) {
      ["object_name"]=>
      string(28) "Hajé restaurant de Taveerne"
      ["object_createdate"]=>
      string(10) "1584437106"
      ["object_createuser"]=>
      string(3) "{4}"
      ["object_read"]=>
      string(3) "{4}"
      ["object_write"]=>
      string(3) "{4}"
      ["object_delete"]=>
      string(3) "{4}"
      ["object_update"]=>
      string(3) "{4}"
      ["object_display"]=>
      string(4) "tree"
      ["object_priority"]=>
      string(1) "1"
      ["data_html"]=>
      string(80) "<p>Haj&eacute; de Taveerne Oostvaardersdijk 01-09 8244 PA Lelystad Nederland</p>"
      ["link"]=>
      string(162) "https://www.google.com/maps/place/Haj%C3%A9+restaurant+de+Taveerne/@52.522116,5.437504,12z/data=!4m5!3m4!1s0x0:0x2a85d0586811bc85!8m2!3d52.522116!4d5.437504?hl=nl"
    }
  }
}`

I'm a bit confused as to why I'm not getting the lat and the long in my index


Solution

  • When you're iterating over $locations, the $location variable is enclosed to the loop - you're not writing back to the $locations.

    Try adding a reference to the $location

    public function getLocaties(){
        $locations = $this->getObjects(68);
    
        foreach ($locations as &$location) {
            $latAndLong = $this->getLatAndLong($locations ['data']['link']);
            $location['data']['latiude'] = $latAndLong['lat'];
            $location['data']['longtitude'] = $latAndLong['long'];
            var_dump($locatie);
        }
    
        return $locaties;
    }