0

I am working on a project in CodeIgniter and I require lat and lng of a specific location. The code at the bottom was working fine but now $geo['status'] is showing QUERY_OVER_LIMIT. How to solve it?

I also get the following warning:

undefined offset:0 error in $latlng['lat']= $geo['results'][0]['geometry']['location']['lat']; $latlng['lng']= $geo['results'][0]['geometry']['location']['lat'];

Thanks in advance. Here's my code:

$address = $this->input->post('key');
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geoco/json?address='.urlencode($address).'&sensor=false');
$geo = json_decode($geo, true);

if ($geo['status'] = 'OK') {
    $latlng['lat']= $geo['results'][0]['geometry']['location']['lat'];
    $latlng['lng']= $geo['results'][0]['geometry']['location']['lat'];
    echo json_encode($latlng);
} else {
    echo 0;
}   
Zeke
  • 1,281
  • 1
  • 18
  • 26
Anonymous
  • 61
  • 10
  • 1
    Possible duplicate of [OVER\_QUERY\_LIMIT while using google maps](https://stackoverflow.com/questions/3529746/over-query-limit-while-using-google-maps) – John Corry Jan 19 '18 at 14:17
  • You exceeded the limit! There's no error, it's just that you used more than you're allowed to. [Learn more in this page...](https://developers.google.com/maps/premium/previous-licenses/articles/usage-limits) – Zeke Jan 19 '18 at 14:18
  • And to add on to what everyone else has said see: https://developers.google.com/maps/documentation/javascript/geocoding which shows you are limited to 2500 requests a day with no more than 50 requests a second. – IsThisJavascript Jan 19 '18 at 14:19
  • Thank you so much ! Is there any solution for that @IsThisJavascript – Anonymous Jan 19 '18 at 14:22
  • Add a count to your loop, once you reach 50, sleep for a second then carry on looping. – IsThisJavascript Jan 19 '18 at 14:24
  • but the main thing is I am not using any kind of loop here – Anonymous Jan 19 '18 at 14:26
  • Do you believe you've hit the limit of 2500 requests a day? – IsThisJavascript Jan 19 '18 at 14:32
  • Obviously not that's why I am confused – Anonymous Jan 19 '18 at 14:58
  • By the way, shouldn't `$latlng['lng']= $geo['results'][0]['geometry']['location']['lat'];` be `$latlng['lng']= $geo['results'][0]['geometry']['location']['lng'];`? I mean, both assignments take `lat`, but the second should take `lng`, am I right? – Zeke Jan 19 '18 at 17:31
  • Wait, I’m confused, what worked? What I said shouldn’t have fixed anything related to the limit... – Zeke Jan 20 '18 at 07:18

1 Answers1

0

First, your check for $geo['status'] = 'OK' will always be 'OK' because you have used a single "=" instead of "==".

This means that even if the google response was something other than what you'd expect, you're still proceeding into that code block.

<?php

$row = [
    'address' => '1315 10th St.',
    'city'    => 'Sacramento',
    'state'   => 'CA',
    'zip'     => '95814'
];

$address = str_replace(' ', '+', $row['address'] ) . ',' 
    . str_replace(' ', '+', $row['city'] ) . ',' 
    . str_replace(' ', '+', $row['state'] ) . ',' 
    . str_replace(' ', '+', $row['zip'] );

function get_gps_coordinates_of_street_address( $address )
{
    $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' 
                    . $address;

    if( $response = @file_get_contents( $url ) )
    {
        $geo = json_decode( $response, TRUE );

        if( $geo['status'] == 'OK' )
        {
            return $geo['results'][0]['geometry']['location']['lat'] . ',' . $arr['results'][0]['geometry']['location']['lng'];
        }
        else
        {
            return 'GPS coordinates were not available for supplied ADDRESS';
        }

    }
    else
    {
        return 'GPS coordinates were not available because connection failed or malformed request';
    }
}

var_dump( get_gps_coordinates_of_street_address( $address ) );
Brian Gottier
  • 4,522
  • 3
  • 21
  • 37