PHP code example of will-belo / geo-location-service

1. Go to this page and download the library: Download will-belo/geo-location-service library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

will-belo / geo-location-service example snippets


    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class MyAddress extends Model
    {
        protected $table = 'addresses'; // Nome da tabela

        // Relacionamento com a entidade associada, como uma loja
        public function relatedEntity()
        {
            return $this->hasOne(\App\Models\Store::class); // Modelo que você deseja associar
        }
    }

    use GeoLocationService\Services\GeocodingService;

    $geoService = new GeocodingService();
    $latitude = -23.550520; // Exemplo de latitude
    $longitude = -46.633308; // Exemplo de longitude

    $nearestEntity = $geoService->findNearestAddress($latitude, $longitude);

    if ($nearestEntity) {
        echo "Entidade mais próxima: " . $nearestEntity->relatedEntity->name;
    } else {
        echo "Nenhuma entidade encontrada próxima.";
    }

    use GeoLocationService\Services\GeoCodingAPI;

    $geoAPI = new GeoCodingAPI();
    $address = "São Paulo, Brasil";

    $coordinates = $geoAPI->getCoordinates($address);

    if ($coordinates) {
        echo "Latitude: " . $coordinates['lat'];
        echo "Longitude: " . $coordinates['lng'];
    } else {
        echo "Endereço não encontrado.";
}
config/cache.php