Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Geo Plugin Provider Laravel Package

geocoder-php/geo-plugin-provider

GeoPlugin provider for the Geocoder PHP library. Turns IP addresses into location data (country, region, city, coordinates) using GeoPlugin’s API. Easy drop-in provider for apps that need basic IP geolocation and locale-aware results.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require geocoder-php/geo-plugin-provider
    

    Require the Geocoder\Provider\GeoPluginProvider in your Laravel app:

    use Geocoder\Provider\GeoPluginProvider;
    use Geocoder\Geocoder;
    use Geocoder\HttpAdapter\Guzzle6Adapter;
    
  2. Basic Usage Initialize the provider with a Geocoder instance:

    $geocoder = new Geocoder();
    $geocoder->registerProvider(new GeoPluginProvider(new Guzzle6Adapter()));
    
  3. First Query Fetch coordinates for an address:

    $results = $geocoder->geocodeQuery('1600 Amphitheatre Parkway, Mountain View, CA');
    foreach ($results as $hit) {
        echo $hit->getLatitude() . ', ' . $hit->getLongitude();
    }
    
  4. Where to Look First


Implementation Patterns

Common Workflows

  1. Reverse Geocoding Convert coordinates to an address:

    $results = $geocoder->reverseQuery(37.422, -122.084);
    
  2. Batch Processing Use geocodeQueryBatch() for multiple addresses:

    $addresses = ['NYC', 'London', 'Tokyo'];
    $results = $geocoder->geocodeQueryBatch($addresses);
    
  3. Integration with Laravel Bind the provider to Laravel’s service container in AppServiceProvider:

    public function register()
    {
        $this->app->singleton(Geocoder::class, function ($app) {
            $geocoder = new Geocoder();
            $geocoder->registerProvider(new GeoPluginProvider(new Guzzle6Adapter()));
            return $geocoder;
        });
    }
    
  4. Caching Responses Cache results to avoid API rate limits:

    $cache = new \Geocoder\Cache\DoctrineCache();
    $geocoder->registerCache($cache);
    
  5. Error Handling Wrap queries in try-catch for API failures:

    try {
        $results = $geocoder->geocodeQuery('Invalid Address');
    } catch (\Geocoder\Exception\UnsupportedOperationException $e) {
        Log::error('GeoPlugin failed: ' . $e->getMessage());
    }
    

Laravel-Specific Tips

  • Use Facades for Cleaner Code Create a facade (e.g., GeoPlugin.php) to abstract the provider:

    namespace App\Facades;
    use Illuminate\Support\Facades\Facade;
    class GeoPlugin extends Facade { protected static function getFacadeAccessor() { return 'geocoder'; } }
    

    Then call GeoPlugin::geocodeQuery('...') in controllers/views.

  • Queue Long-Running Geocodes Offload geocoding to a queue job (e.g., GeocodeAddressJob) to avoid timeouts.

  • Store Results in Database Use Eloquent events or observers to save geocoded data:

    class AddressObserver {
        public function saved(Address $address) {
            if (empty($address->latitude)) {
                $results = GeoPlugin::geocodeQuery($address->full_address);
                $address->update(['latitude' => $results[0]->getLatitude(), ...]);
            }
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Rate Limits GeoPlugin has strict rate limits (e.g., 1,000 requests/day for free tier).

    • Fix: Cache aggressively or upgrade to a paid plan.
    • Workaround: Use a fallback provider (e.g., Geocoder\Provider\OpenStreetMapProvider) if limits are hit.
  2. Response Format Quirks GeoPlugin returns nested JSON. The provider flattens it, but some fields (e.g., geoplugin_city) may require manual parsing:

    $hit->getExtraProperties()['geoplugin_city']; // Access raw data if needed.
    
  3. HTTP Adapter Issues

    • Ensure guzzlehttp/guzzle is installed (required for Guzzle6Adapter).
    • Configure timeouts in the adapter:
      $adapter = new Guzzle6Adapter(['timeout' => 10]);
      
  4. Free Tier Limitations The free tier lacks accuracy for some regions. Test with your target addresses before production.

Debugging

  • Enable Debug Mode Set GEOCODER_DEBUG=1 in .env to log raw API responses:

    $geocoder->registerProvider(new GeoPluginProvider(new Guzzle6Adapter(), [], true)); // Enable debug.
    
  • Check HTTP Status Codes GeoPlugin returns 429 for rate limits. Handle it explicitly:

    catch (\Geocoder\HttpException\UnprocessableEntityException $e) {
        if ($e->getCode() === 429) {
            // Retry or switch provider.
        }
    }
    

Extension Points

  1. Custom Response Mapping Override the provider’s getGeocodedPlace() method to map GeoPlugin’s JSON to your schema:

    class CustomGeoPluginProvider extends GeoPluginProvider {
        protected function getGeocodedPlace($data) {
            $place = parent::getGeocodedPlace($data);
            $place->setCustomProperty('timezone', $data['geoplugin_timezone']);
            return $place;
        }
    }
    
  2. Add Headers for API Key If using a paid plan, inject headers:

    $adapter = new Guzzle6Adapter(['headers' => ['X-API-Key' => config('services.geoplugin.key')]]);
    
  3. Mock for Testing Use Geocoder\Provider\MockProvider in tests:

    $geocoder = new Geocoder();
    $geocoder->registerProvider(new MockProvider());
    $geocoder->registerProvider(new GeoPluginProvider($adapter)); // Fallback.
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor