1

I am trying get coordinate from addresses, but having an error which is called undefined offset. Actually array is not null and when I try to get coordinate one by one, I can get grab it without a problem. But when I try get all at once error pops up.

    $addr = [];
    foreach($newHtml as $link){

        $htm = pageContent($link);
        $paths = new \DOMXPath($htm);
        $routes = $paths->query("//body/div[@class='p-main']//table/tr[4]/td");
        foreach ($routes as $route) {
            $addr = trim($route->nodeValue);
        }

        $address = urlencode($addr);
        $response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
        $result = json_decode($response);

        $lat[] = trim($result->results[0]->geometry->location->lat);
        $lng[] = trim($result->results[0]->geometry->location->lng);
    }

    var_dump($lat, $lng);

Response gives this;

["results"]=> array(1) { [0]=> object(stdClass)#696 (6) { ["address_components"]=> array(8) { [0]=> object(stdClass)#684 (3) { ["long_name"]=> string(9) "D21" ["short_name"]=> string(9) "D21" ["types"]=> array(1) { [0]=> string(7) "premise" } } [1]=> object(stdClass)#709 (3) { ["long_name"]=> string(1) "1" ["short_name"]=> string(1) "1" ["types"]=> array(3) { [0]=> string(9) "political" [1]=> string(11) "sublocality" [2]=> string(19) "sublocality_level_4" } } [2]=> object(stdClass)#714 (3) { ["long_name"]=> string(7) "5 Chome" ["short_name"]=> string(7) "5 Chome" ["types"]=> array(3) { [0]=> string(9) "political" [1]=> string(11) "sublocality" [2]=> string(19) "sublocality_level_3" } } [3]=> object(stdClass)#698 (3) { ["long_name"]=> string(8) "Furuedai" ["short_name"]=> string(8) "Furuedai" ["types"]=> array(3) { [0]=> string(9) "political" [1]=> string(11) "sublocality" [2]=> string(19) "sublocality_level_2" } } [4]=> object(stdClass)#680 (3) { ["long_name"]=> string(5) "Suita" ["short_name"]=> string(5) "Suita" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [5]=> object(stdClass)#701 (3) { ["long_name"]=> string(16) "Osaka Prefecture" ["short_name"]=> string(16) "Osaka Prefecture" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [6]=> object(stdClass)#695 (3) { ["long_name"]=> string(5) "Japan" ["short_name"]=> string(2) "JP" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } [7]=> object(stdClass)#699 (3) { ["long_name"]=> string(8) "565-0874" ["short_name"]=> string(8) "565-0874" ["types"]=> array(1) { [0]=> string(11) "postal_code" } } } ["formatted_address"]=> string(69) "5 Chome-1-D21 Furuedai, Suita, Osaka Prefecture 565-0874, Japan" ["geometry"]=> object(stdClass)#690 (3) { ["location"]=> object(stdClass)#694 (2) { ["lat"]=> float(34.8097093) ["lng"]=> float(135.5119112) } ["location_type"]=> string(7) "ROOFTOP" ["viewport"]=> object(stdClass)#689 (2) { ["northeast"]=> object(stdClass)#688 (2) { ["lat"]=> float(34.811058280291) ["lng"]=> float(135.51326018029) } ["southwest"]=> object(stdClass)#692 (2) { ["lat"]=> float(34.808360319709) ["lng"]=> float(135.51056221971) } } } ["place_id"]=> string(27) "ChIJUY4O7kP7AGARu_UB4lJPFWU" ["plus_code"]=> object(stdClass)#687 (2) { ["compound_code"]=> string(30) "RG56+VQ Suita, 大阪府 Japan" ["global_code"]=> string(11) "8Q6QRG56+VQ" } ["types"]=> array(2) { [0]=> string(13) "establishment" [1]=> string(17) "point_of_interest" } } } ["status"]=> string(2) "OK"

  • Use print_r($newHtml); to inspect the array $newHtml, you will see that key 0 does not exist there. It will return NULL and throw that error. – MD. Jubair Mizan Dec 10 '18 at 07:09
  • 1
    You should check that `$response` and `$result` are `false` which would indicate that either of those operations have failed. If they are OK - can you add what `$response` gives in the question. – Nigel Ren Dec 10 '18 at 07:09
  • I added what response gives in the question @NigelRen –  Dec 10 '18 at 07:17
  • As your doing this in a loop - is it only running once? – Nigel Ren Dec 10 '18 at 07:20

2 Answers2

0

This line causes an error:

$result->results[0]

You need to check if $result->results has 0 key.

UPDATE:

To troubleshoot your problem you need to catch response that you do not expect:

$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
$result = json_decode($response);

try {
    $lat[] = trim($result->results[0]->geometry->location->lat);
    $lng[] = trim($result->results[0]->geometry->location->lng);
} catch (\Exeption $e) {
    var_dump($result);
}

When an exception occurs, you will see the response that causes your problem, and then you need to add a response format check based on what you received.

For example if you receive something like this:

{"results":[], "status": "ZERO_RESULTS"}

Your code should looks like this:

$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");

$result = json_decode($response);

if(!empty($result->results)){
    $lat[] = trim($result->results[0]->geometry->location->lat);
    $lng[] = trim($result->results[0]->geometry->location->lng);
}

See also https://developers.google.com/maps/documentation/geocoding/intro

0

try to change

    $locations = json_decode($response, true);

    if(is_array($locations)) {
   $lat =   $locations['results'][0]['geometry']['location']['lat'];
   $lng =   $locations['results'][0]['geometry']['location']['lng'];

  }

json_decode -->will return object array

json_decode with second parameter as true return as array

Shibon
  • 1,552
  • 2
  • 9
  • 20