PHP code example of sslah / location-finder

1. Go to this page and download the library: Download sslah/location-finder 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/ */

    

sslah / location-finder example snippets


use LocationFinder;

// Search for locations
$results = LocationFinder::search('ankara');

// Geocode an address to get coordinates
$location = LocationFinder::geocode('Kızılay, Ankara');

// Reverse geocode coordinates to get address
$address = LocationFinder::reverseGeocode(39.9208, 32.8541);

use Sslah\LocationFinder\Services\GeocodingService;

class LocationController
{
    public function __construct(private GeocodingService $locationFinder)
    {
    }

    public function search(Request $request)
    {
        $results = $this->locationFinder->search($request->query);
        return response()->json($results);
    }
}

// config/location-finder.php
return [
    'service' => [
        'provider' => 'nominatim',
        'base_url' => 'https://nominatim.openstreetmap.org',
        'user_agent' => 'Laravel Location Finder',
        'timeout' => 10,
    ],
    'search' => [
        'min_chars' => 3,
        'max_results' => 10,
        'country_code' => 'tr',
        'language' => 'tr',
    ],
    'cache' => [
        'enabled' => true,
        'ttl' => 3600,
    ],
];

// In your AppServiceProvider
use Sslah\LocationFinder\Services\GeocodingService;

public function boot()
{
    $this->app->bind(GeocodingService::class, function ($app) {
        return new GeocodingService([
            'country_code' => 'us',
            'language' => 'en',
            'max_results' => 5,
        ]);
    });
}

use Sslah\LocationFinder\Services\GeocodingService;

class MyController extends Controller
{
    public function search(Request $request, GeocodingService $geocoding)
    {
        $results = $geocoding->search($request->input('query'));
        
        return response()->json($results);
    }
}
bash
php artisan location-finder:install