geocoder-php / Geocoder

The most featured Geocoder library written in PHP.
https://geocoder-php.org
MIT License
3.94k stars 515 forks source link

Call to undefined method Geocoder\StatefulGeocoder::getLatitude() #976

Closed ghost closed 5 years ago

ghost commented 5 years ago

Im trying to put addressess in an array, and making a conditional for the geocoding of those locations which where gave me. How could I fetch them from the BD and iterate and display their lat and long?


<?php include('conecta.php');?>
<?php
require_once('vendor/autoload.php');
ini_set('display_errors', 'On');
use GuzzleHttp\Client as GuzzleClient;
use Geocoder\Provider\GoogleMaps;
use Http\Adapter\Guzzle6\Client;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;

$apikey = '';

$httpClient = new \Http\Adapter\Guzzle6\Client();
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, 'Brazil', $apikey);
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'BR');
$enderecos = array(
    array(
        'endereco' => 'RUA CAIGANGUES N:136 '
    ),
    array(
        'endereco' => 'R:DA AURORA N45',
    ),
    array(
        'endereco' => 'R:LUIZ LONE N110',
    ),
    array(
        'endereco' => ' R:JOAO DA LAPRIA N543 ',
    )
);

foreach ($enderecos as $key=>$value) {
try {
$geocode = $geocoder->geocode($value['endereco']);
$lat = $geocoder->getLatitude();
$long = $geocoder->getLongitude();
} catch(Exception $e) {
  echo $e->getMessage();
}
}
atymic commented 5 years ago

You are calling getLatitude() on $geocoder (the actual geocoder) rather than the result ($geocode in your case).

Use:

$lat = $geocode->getLatitude();
$long = $geocode->getLongitude();
ghost commented 5 years ago

You are calling getLatitude() on $geocoder (the actual geocoder) rather than the result ($geocode in your case).

Use:

$lat = $geocode->getLatitude();
$long = $geocode->getLongitude();
<?php
require_once('vendor/autoload.php');
ini_set('display_errors', 'On');
use GuzzleHttp\Client as GuzzleClient;
use Geocoder\Provider\GoogleMaps;
use Http\Adapter\Guzzle6\Client;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;

$apikey = 'AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s';

$httpClient = new \Http\Adapter\Guzzle6\Client();
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, 'Brazil', $apikey);
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'BR');
$enderecos = array(
    array(
        'endereco' => 'RUA CAIGANGUES N:136 '
    ),
    array(
        'endereco' => 'R:DA AURORA N45',
    ),
    array(
        'endereco' => 'R:LUIZ LONE N110',
    ),
    array(
        'endereco' => ' R:JOAO DA LAPRIA N543 ',
    )
);

foreach ($enderecos as $key=>$value) {
try {
$geocode = $geocoder->geocode($value['endereco']);
$lat = $geocode->getLatitude();
$long = $geocode->getLongitude();
} catch(Exception $e) {
  echo $e->getMessage();
}
}

Im still getting the error: Uncaught Error: Call to undefined method Geocoder\Model\AddressCollection::getLatitude()

jbelien commented 5 years ago

Please @maschinedreams , read the documentation : https://github.com/geocoder-php/Geocoder#statefulgeocoder

ghost commented 5 years ago

Please @maschinedreams , read the documentation : https://github.com/geocoder-php/Geocoder#statefulgeocoder

I dont know how to do this, I just need to get all their coordinates, can you hhelp me?

jbelien commented 5 years ago
use Geocoder\Query\GeocodeQuery;

// configure your provider
$provider = // ...
$geocoder = new \Geocoder\StatefulGeocoder($provider);

$results = $geocoder->geocodeQuery(GeocodeQuery::create('London'));
foreach ($results as $result) {
    $lat = $result->getLatitude();
    $lng = $result->getLongitude();
}

or something like that. or using a dumper.