Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call to undefined method Geocoder\Model\Address::getLatitude() #412

Closed
stevevance opened this issue Mar 9, 2015 · 4 comments
Closed

Call to undefined method Geocoder\Model\Address::getLatitude() #412

stevevance opened this issue Mar 9, 2015 · 4 comments
Milestone

Comments

@stevevance
Copy link
Contributor

Occasionally my normally-working script will display this error:
Fatal error: Call to undefined method Geocoder\Model\Address::getLatitude()

I'm using the dev-master version in Composer, but this didn't happen before today (I didn't modify my geocoder, though).

The error seems to occur randomly, about 1 in 20 times. Since it happens at that frequency I just get the feeling that some other function is trying to run and it couldn't wait for the Geocoder to finish. Upon refreshing the page the Geocoder and the rest of the script loads normally – every time.

If anyone has an idea on how to suppress the error so that the user doesn't get Fatal error I'd like to know some good ways to do that. I thought try {} catch() {} was supposed to do that.

The full code is

$geocoder = new \Geocoder\ProviderAggregator();
            $adapter  = new \Ivory\HttpAdapter\CurlHttpAdapter();

            // a chain means it goes through the list until it can find a result
            $chain = new \Geocoder\Provider\Chain([
                new \Geocoder\Provider\BingMaps($adapter, $keys['bing']),
                new \Geocoder\Provider\Nominatim($adapter, "http://nominatim.openstreetmap.org/search"),
                new \Geocoder\Provider\MapQuest($adapter, $keys['mapquest'], true),
                new \Geocoder\Provider\ArcGISOnline($adapter),
                //new \Geocoder\Provider\GeocoderCaProvider($adapter, $geocoderCaKey), // geocoder-extra hasn't been updated
            ]);

$geocoder->registerProvider($chain);

try {
        $geocode = $geocoder->geocode($q);
        //($debug ? print_r2($geocode) : null);

        if($version >= 3) {
            $results['count'] = $geocode->count();
        } else {
            $results['count'] = 1;
        }

        if($results['count'] > 0) {
            $results['chain'] = true;
            $results['lat'] = $geocode->first()->getLatitudee();
            $results['lng'] = $geocode->first()->getLongitude();
            $results['zipcode'] = $geocode->first()->getPostalCode();
            $results['address_raw'] = trim($geocode->first()->getStreetNumber()." ".$geocode->first()->getStreetName() );
            $results['address'] = ucwords(strtolower(streetRename( $results['address_raw'] )));
            $results['street_number'] = $geocode->first()->getStreetName();
            $results['city'] = $geocode->first()->getLocality();
            $results['state'] = $geocode->first()->getAdminLevels()->get(1)->getName();
            $results['county'] = $geocode->first()->getAdminLevels()->get(2)->getName();
        } else {
            $results = false;
        }
} catch (Exception $e) {
        ($debug ? print_r("Geocoder had an exception") : null );
        ($debug ? print_r2($e->getMessage()) : null );
        $results = false;
}
@willdurand willdurand added this to the 3.0.0 milestone Mar 9, 2015
@willdurand
Copy link
Member

Thanks for the feedback.

@willdurand
Copy link
Member

Hard to debug. It seems that, sometimes, your collection of results is empty, hence the null value returned by first(). I fixed the code to avoid such a situation.

@vizath
Copy link

vizath commented Apr 28, 2015

It happened on our side with ->geocode('')

This change broke backward compatibility from alpha to 3.0.0.
I don't know if BC was in the scope of alpha, simply a note to be careful...

@willdurand
Copy link
Member

@vizath yup I am aware of the BC break, could not do anything else unfortunately...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants