arraypress / google-geocoding
A PHP library for integrating with the Google Geocoding API in WordPress, providing address to coordinate conversion and structured address information. Features WordPress transient caching and WP_Error support.
dev-main
2024-12-21 17:19 UTC
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2024-12-21 17:19:59 UTC
README
A PHP library for integrating with the Google Geocoding API in WordPress, providing address to coordinate conversion and structured address information. Features WordPress transient caching and WP_Error support.
Features
- πΊοΈ Address Geocoding: Convert addresses to coordinates
- π Reverse Geocoding: Convert coordinates to addresses
- π Address Components: Access structured address information
- β‘ WordPress Integration: Native transient caching and WP_Error support
- π‘οΈ Type Safety: Full type hinting and strict types
- π Response Parsing: Clean response object for easy data access
- π Global Support: Works with addresses worldwide
- π Viewport Information: Access location viewport bounds
- π’ Business Detection: Identify business locations
- π Detailed Components: Access neighborhood and sublocality information
- π Multiple Result Types: Handle various location types
- β¨ Plus Code Support: Access global and compound plus codes
Requirements
- PHP 7.4 or later
- WordPress 5.0 or later
- Google Geocoding API key
Installation
Install via Composer:
composer require arraypress/google-geocoding
Basic Usage
use ArrayPress\Google\Geocoding\Client; // Initialize client with your API key $client = new Client( 'your-google-api-key' ); // Forward geocoding (Address to Coordinates) $result = $client->geocode( '1600 Amphitheatre Parkway, Mountain View, CA' ); if ( ! is_wp_error( $result ) ) { $coordinates = $result->get_coordinates(); echo "Latitude: {$coordinates['latitude']}\n"; echo "Longitude: {$coordinates['longitude']}\n"; } // Reverse geocoding (Coordinates to Address) $result = $client->reverse_geocode( 37.4220, - 122.0841 ); if ( ! is_wp_error( $result ) ) { echo $result->get_formatted_address(); }
Extended Examples
Getting Structured Address Components
$result = $client->geocode( '1600 Amphitheatre Parkway, Mountain View, CA' ); if ( ! is_wp_error( $result ) ) { $address = $result->get_structured_address(); // Basic Components $street = $result->get_street_number() . ' ' . $result->get_street_name(); $city = $result->get_city(); $state = $result->get_state(); $state_code = $result->get_state_short(); $postal = $result->get_postal_code(); $country = $result->get_country(); $country_code = $result->get_country_short(); // Additional Components $neighborhood = $result->get_neighborhood(); $sublocality = $result->get_sublocality(); $sublocality_level_1 = $result->get_sublocality_level_1(); }
Working with Business Locations
$result = $client->geocode( '123 Business Street' ); if ( ! is_wp_error( $result ) ) { if ( $result->is_business_location() ) { echo "This is a business location\n"; } // Get all place types $types = $result->get_types(); // Get specific components by types $business_components = $result->get_address_components_by_types( [ 'establishment', 'point_of_interest' ] ); }
Handling Plus Codes and Location Types
$result = $client->geocode( '1600 Amphitheatre Parkway, Mountain View, CA' ); if ( ! is_wp_error( $result ) ) { // Plus Codes $plus_code = $result->get_plus_code(); echo "Compound Code: " . $result->get_plus_code_compound() . "\n"; echo "Global Code: " . $result->get_plus_code_global() . "\n"; // Location Type $location_type = $result->get_location_type(); // ROOFTOP, RANGE_INTERPOLATED, etc. // Viewport Information $viewport = $result->get_viewport(); if ( $viewport ) { echo "Northeast: {$viewport['northeast']['lat']}, {$viewport['northeast']['lng']}\n"; echo "Southwest: {$viewport['southwest']['lat']}, {$viewport['southwest']['lng']}\n"; } }
Handling Responses with Caching
// Initialize with custom cache duration (1 hour = 3600 seconds) $client = new Client( 'your-api-key', true, 3600 ); // Results will be cached $result = $client->geocode('1600 Amphitheatre Parkway, Mountain View, CA'); // Clear specific cache $client->clear_cache('geocode_1600 Amphitheatre Parkway, Mountain View, CA'); // Clear all geocoding caches $client->clear_cache();
Working with Multiple Results
$result = $client->geocode( 'Springfield' ); if ( ! is_wp_error( $result ) ) { // Get all results $all_results = $result->get_results(); // Iterate over results with a callback $locations = $result->iterate_results( function ( $location ) { return [ 'address' => $location['formatted_address'], 'coordinates' => $location['geometry']['location'] ]; } ); }
API Methods
Client Methods
geocode( $address )
: Convert address to coordinatesreverse_geocode( $lat, $lng )
: Convert coordinates to addressclear_cache( $identifier = null )
: Clear cached responses
Response Methods
Basic Information
get_all()
: Get complete raw response dataget_first_result()
: Get first result from responseget_results()
: Get all results from responseget_status()
: Get API response status
Address Information
get_formatted_address()
: Get full formatted addressget_structured_address()
: Get all components in structured formatis_partial_match()
: Check if result is a partial matchis_business_location()
: Check if location is a business/POI
Geographic Information
get_coordinates()
: Get latitude/longitude arrayget_latitude()
: Get latitudeget_longitude()
: Get longitudeget_viewport()
: Get viewport boundsget_location_type()
: Get location type (ROOFTOP, etc.)get_types()
: Get all place types
Plus Codes
get_plus_code()
: Get plus code informationget_plus_code_compound()
: Get compound plus codeget_plus_code_global()
: Get global plus code
Place Information
get_place_id()
: Get Google Place ID
Address Components
get_street_number()
: Get street numberget_street_name()
: Get street nameget_neighborhood()
: Get neighborhoodget_sublocality()
: Get sublocalityget_sublocality_level_1()
: Get sublocality level 1get_city()
: Get city/localityget_county()
: Get countyget_state()
: Get state/provinceget_state_short()
: Get state/province codeget_postal_code()
: Get postal codeget_country()
: Get countryget_country_short()
: Get country code
Component Helpers
get_address_component( $type )
: Get specific address componentget_address_component_short( $type )
: Get specific address component short nameget_address_components()
: Get all address componentsget_address_components_by_types( array $types )
: Get components matching multiple types
Result Processing
iterate_results( callable $callback )
: Process all results with a callback
Use Cases
- Address Validation: Verify and standardize addresses
- Coordinate Lookup: Get coordinates for addresses
- Location Services: Support location-based features
- Address Parsing: Extract address components
- Geographic Analysis: Analyze location data
- Map Integration: Support for mapping features
- Address Autocomplete: Base for address lookup systems
- Business Location Detection: Identify commercial locations
- Neighborhood Analysis: Access detailed area information
- Multiple Result Handling: Process ambiguous locations
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GPL-2.0-or-later License.