Geocode an address with Thinwrap (PHP)
Convert an address to coordinates via the Thinwrap unified Geocoding facade in PHP — same call shape for Google, Mapbox, HERE, ESRI, and TomTom.
1. Install
composer require thinwrap/location2. Send the call
use Thinwrap\Location\Geocoding;
use Thinwrap\Location\Config\GoogleConfig;
use Thinwrap\Location\DTO\Geocoding\GeocodeOptions;
use Thinwrap\Location\DTO\Geocoding\ReverseGeocodeOptions;
use Thinwrap\Location\DTO\LatLng;
use Thinwrap\Location\Enum\LocationProviderId;
$geocoding = new Geocoding(
LocationProviderId::Google,
new GoogleConfig(apiKey: getenv('GOOGLE_MAPS_API_KEY')),
);
$result = $geocoding->geocode(new GeocodeOptions(
address: '1600 Amphitheatre Parkway, Mountain View, CA',
countryFilter: ['US'], // hard ISO 3166-1 alpha-2 filter
));
// Pick the top candidate; ->candidates is non-empty on success.
$top = $result->candidates[0];
echo $top->location->lat, ', ', $top->location->lng, PHP_EOL;
// Reverse-geocode + autocomplete are on the same facade:
$reverse = $geocoding->reverseGeocode(new ReverseGeocodeOptions(
location: new LatLng(37.4221, -122.0841),
));
echo $reverse->candidates[0]->formattedAddress ?? '(no match)', PHP_EOL;
Same code works for all supported providers — just change the provider ID.