3

This is my code to get address off particular lat long dynamically from db.If i used this static value it works fine. but if i used $latlong this variable to get dynamic values. it shows error. what's the solution for this. please help me with the same.

Notice: Undefined offset: 0 in C:\xampp\htdocs\demo_calLatLong\calLatLong.php on line 21 Notice: Trying to get property of non-object in C:\xampp\htdocs\demo_calLatLong\calLatLong.php on line 21

<table class="table table-bordered">
        <thead>
            <tr>         
              <th>Client Name</th>                                            
            </tr>
        </thead>
        <?php 
            include 'db.php';
            $sql = 'SELECT  *  FROM `location`';            
            $travel = mysqli_query($conn,$sql);     
        while ($row = mysqli_fetch_array($travel)) 
        {?>         
        <tbody>
            <tr class="active">
            <?php
               $latlong = $row[1].','.$row[2];
               $geocode=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=19.0978,22.8972&sensor=false',true));            
            ?>  
            <td><?php echo $geocode->results[0]->formatted_address;?></td>                                
            </tr>                               
        </tbody>
    <?php }?>
    </table>
mahadev sutar
  • 171
  • 2
  • 16

1 Answers1

0

You are trying to access url not a file, Use CURL instead to access file direct.

Replace

        <?php
           $latlong = $row[1].','.$row[2];
           $geocode=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=19.0978,22.8972&sensor=false',true));            
        ?> 

To

<?php
    $url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$row[1],$row[2]&sensor=false";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $geocode = json_decode($output);
?>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • if i used your code with it displays data but i want formatted address. so i used results[0]->formatted_address;?> . but using this it givese me error Notice: Trying to get property of non-object in C:\xampp\htdocs\demo_calLatLong\calLatLong.php on line 27 – mahadev sutar Mar 14 '17 at 06:15
  • what url you are getting ? share to try ? – Niklesh Raut Mar 14 '17 at 06:17
  • Also share `print_r($row);` may be you are getting wrong latlong, you can try with static value as in your question. – Niklesh Raut Mar 14 '17 at 06:19
  • if i used static value with print_r it shows same 5 outputs as like var_dump on dynamic values. can't get what's wrong with my code. i am too near to my solution please help with the same. – mahadev sutar Mar 14 '17 at 06:26
  • Bhai, share `print_r($row);` ? may be you need to use `$row[0]` and `$row[1]` instead of `$row[1]` and `$row[2]`. – Niklesh Raut Mar 14 '17 at 06:30
  • no bro its $row[1] and and $row[2] only. problem is using var_dump it display address. and with echo it display error. {Trying to get property of non-object} $geocode->results[0]->formatted_address – mahadev sutar Mar 14 '17 at 06:34
  • share what you are getting in `$geocode->results[0]` ? if nothing share `$geocode` ? – Niklesh Raut Mar 14 '17 at 06:35
  • bhai, It's your local not mine. just comment ouput or update in your question. – Niklesh Raut Mar 14 '17 at 06:40
  • Notice: Trying to get property of non-object in echo $geocode->results[0]->formatted_address; – mahadev sutar Mar 14 '17 at 06:41
  • I am going to die. I said value of `$geocode->results[0]` or `$geocode` ? – Niklesh Raut Mar 14 '17 at 06:43
  • Undefined offset: 0 on echo $geocode[0]; Array to string conversion on $geocode; – mahadev sutar Mar 14 '17 at 06:46
  • on print $geocode `Array of data` on $geocode->results[0] `Trying to get property of non-object in` – mahadev sutar Mar 14 '17 at 06:50
  • bro im using `print_r` only. don't be panic. it shows my data in array. Array ( [results] => Array ( [0] => Array ( [address_components] => Array ( [0] => Array ( [long_name] => Ennedi [short_name] => Ennedi [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [1] => Array ( [long_name] => Chad [short_name] => TD [types] => Array ( [0] => country [1] => political ) ) ) [formatted_address] => Ennedi, Chad [geometry] => Array ( [bounds] => Array ( [northeast] => Array ( [lat] => 21.0341929 [lng] => 24.002661 ) [southwest] – mahadev sutar Mar 14 '17 at 06:56
  • I was only asking this from last 2-3 comment,I think you haven't copy last line of my code `$geocode = json_decode($output);` or just add `true` used `$geocode = json_decode($output,ture);` , remove `true` as second param to get object instead array. Otherwise you have to use simply `$geocode['results'][0]['formatted_address']` – Niklesh Raut Mar 14 '17 at 07:00
  • I deserve jalebi and peda from your side to recover . I nearly gone to die. – Niklesh Raut Mar 14 '17 at 07:06