3

I have a PHP page containing the following code to get latitude and longitude from a user input postal code.

But when I tried to echo the latitude and longitude, nothing is shown.

<?php
    function getLnt($zip){
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&sensor=false";
    $result_string = file_get_contents($url);
    $result = json_decode($result_string, true);
    return $result['results'][0]['geometry']['location'];
    }

    getLnt("750341");
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <?php
         $val = getLnt('90001');
         echo "Latitude: ".$val['lat']."<br>";
         echo "Longitude: ".$val['lng']."<br>";
        ?>
    </body>
</html>
13
  • Have you var_dump($val)? Commented Oct 14, 2015 at 12:29
  • The google result of your zipcode 750341 returns the below content { "results" : [], "status" : "ZERO_RESULTS" }. So if google returns nothing I think we can't manipulate it. Commented Oct 14, 2015 at 12:31
  • I've tested your code and I get Latitude: 33.9697897 Longitude: -118.2468148 as output. Commented Oct 14, 2015 at 12:31
  • 1
    Do a var_dump($result_string); right after, $result_string = file_get_contents($url); what does it show ? Commented Oct 14, 2015 at 12:45
  • 1
    You got your answer :) Commented Oct 14, 2015 at 12:49

5 Answers 5

3

Please add key parameter in api request. Key value you need to replace with google API key.

$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($postal_code)."&sensor=false&key=googleapi";
    $result_string = file_get_contents($url);
    $result = json_decode($result_string, true);
    if(!empty($result['results'])){
        $zipLat = $result['results'][0]['geometry']['location']['lat'];
        $ziplng = $result['results'][0]['geometry']['location']['lng'];
    }
Sign up to request clarification or add additional context in comments.

Comments

2

I've tested your code and it works perfectly, my guess is that you've exceeded your daily quota for the Google Places API Web Service.
A quick solution is to apply for a key at:

https://console.developers.google.com/

Comments

1

I think your problem is on API call must be on HTTP So change http to https

<?php
    function getLnt($zip){
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&sensor=false";
    $result_string = file_get_contents($url);
    $result = json_decode($result_string, true);
    return $result['results'][0]['geometry']['location'];
    }

    getLnt("750341");
?>

Comments

1

Not sure when but in 2019, we can use components in instead of address to get more accurary result. eg: country is US and postal code is 41000

$url = "https://maps.googleapis.com/maps/api/geocode/json?components=" . urlencode('country:US|postal_code:41000') . "&key=YOUR_API_KEY";

Comments

0
    function getLnt($zip){
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&sensor=false&key=YOUR_KEY";
    $result_string = file_get_contents($url);
    $result = json_decode($result_string, true);
    return $result['results'][0]['geometry']['location'];
    }

  print_r(getLnt("462021"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.