1001pharmacies/geolocation-bundle
Installation:
composer require 1001pharmacies/geolocation-bundle
Add to config/bundles.php:
return [
// ...
Meup\GeoLocationBundle\MeupGeoLocationBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php artisan vendor:publish --tag=meup-geolocation-config
Edit config/meup_geolocation.php to enable your preferred provider (e.g., google, nominatim) and set API keys.
First Use Case: Locate coordinates from an address:
use Meup\GeoLocationBundle\Factory\AddressFactory;
use Meup\GeoLocationBundle\Locator\LocatorInterface;
$addressFactory = $this->container->get(AddressFactory::class);
$locator = $this->container->get(Locator::class);
$address = $addressFactory->create()->setFullAddress('1600 Amphitheatre Parkway, Mountain View, CA');
$coordinates = $locator->locate($address);
// Output: Latitude/Longitude (e.g., 37.4220, -122.0841)
Address Creation & Validation:
Use AddressFactory to create structured addresses:
$address = $addressFactory->create()
->setStreet('123 Main St')
->setPostalCode('90210')
->setCity('Los Angeles')
->setCountry('US');
Validate with:
if (!$address->isValid()) {
throw new \InvalidArgumentException('Invalid address');
}
Provider-Specific Logic:
config/meup_geolocation.php under providers to enable fallback logic:
providers:
- { id: google, enabled: true }
- { id: nominatim, enabled: true, fallback: true }
cache settings to avoid hitting API limits:
cache:
enabled: true
ttl: 3600 # Cache results for 1 hour
Reverse Geocoding: Convert coordinates to addresses:
$reverseLocator = $this->container->get('meup_geo_location.reverse_locator');
$address = $reverseLocator->locate(37.4220, -122.0841);
Batch Processing: Process multiple addresses efficiently:
$batchLocator = $this->container->get('meup_geo_location.batch_locator');
$results = $batchLocator->locate([
$address1, $address2, $address3
]);
Meup\GeoLocationBundle\Form\Type\AddressType for user input.Meup\GeoLocationBundle\Doctrine\Types\CoordinatesType for storing lat/lng in databases.meup_geolocation.locate):
$dispatcher->addListener('meup_geolocation.locate', function ($event) {
// Pre/post-process locate requests
});
API Key Management:
.env):
MEUP_GEOLOCATION_GOOGLE_API_KEY=your_key_here
php artisan cache:clear) after changes.Rate Limits & Quotas:
$locator->setRetryStrategy(new \Meup\GeoLocationBundle\Retry\ExponentialBackoff());
Time Zone & Caching:
ttl: 3600) may cause stale data. Adjust based on use case (e.g., shorter TTL for dynamic data).Provider-Specific Quirks:
components (e.g., street_number, route) for better accuracy.https://nominatim.openstreetmap.org/ for testing; switch to a dedicated instance for production.$coordinates->toWgs84();
Dependency Injection:
LocatorInterface, ReverseLocatorInterface) instead of concrete services.Enable Logging:
# config/meup_geolocation.php
logging: true
Logs appear in var/log/meup_geolocation.log.
Validate Addresses:
Use Address::isValid() to catch malformed inputs early. For custom validation, extend Meup\GeoLocationBundle\Validator\AddressValidator.
Mock Providers for Testing: Create a custom provider (see Custom Provider Docs) to return mock responses:
class MockProvider implements ProviderInterface {
public function locate(Address $address) {
return new Coordinates(0, 0); // Mock response
}
}
Custom Providers:
Implement Meup\GeoLocationBundle\Provider\ProviderInterface and register via config:
providers:
- { id: custom, class: App\Provider\CustomProvider, enabled: true }
Address Components:
Extend Meup\GeoLocationBundle\Model\Address to add custom fields (e.g., setFloor, setUnit).
Coordinate Transformations:
Add custom transformations by implementing Meup\GeoLocationBundle\Transformer\CoordinateTransformerInterface.
Event Subscribers:
Listen for meup_geolocation.locate or meup_geolocation.reverse_locate to intercept or modify requests/responses.
How can I help you explore Laravel packages today?