Overview

Namespaces

  • None
  • WPGMZA
    • Integration
    • Selector

Classes

  • WPGMZA\AdminMarkerDataTable
  • WPGMZA\AjaxTable
  • WPGMZA\AutoLoader
  • WPGMZA\Crud
  • WPGMZA\Database
  • WPGMZA\DataTable
  • WPGMZA\Distance
  • WPGMZA\DOMDocument
  • WPGMZA\DOMElement
  • WPGMZA\Factory
  • WPGMZA\GDPRCompliance
  • WPGMZA\GlobalSettings
  • WPGMZA\GoogleGeocoder
  • WPGMZA\GoogleMap
  • WPGMZA\GoogleMapsAPILoader
  • WPGMZA\GoogleMapsLoader
  • WPGMZA\Integration\Gutenberg
  • WPGMZA\Integration\WPMigrateDB
  • WPGMZA\LatLng
  • WPGMZA\Map
  • WPGMZA\MapsEngineDialog
  • WPGMZA\Marker
  • WPGMZA\MarkerDataTable
  • WPGMZA\MarkerFilter
  • WPGMZA\ModalDialog
  • WPGMZA\NominatimGeocodeCache
  • WPGMZA\OLLoader
  • WPGMZA\Plugin
  • WPGMZA\Query
  • WPGMZA\QueryFragment
  • WPGMZA\RestAPI
  • WPGMZA\ScriptLoader
  • WPGMZA\Selector\AttributeSelector
  • WPGMZA\Selector\Parser
  • WPGMZA\Selector\PseudoSelector
  • WPGMZA\Selector\Selector
  • WPGMZA\Selector\Token
  • WPGMZA\Selector\Tokenizer
  • WPGMZA\Selector\TokenStream
  • WPGMZA\Selector\XPathConverter
  • WPGMZA\Strings
  • WPGMZA\Table

Exceptions

  • WPGMZA\Selector\ConvertException
  • WPGMZA\Selector\ParseException

Functions

  • WPGMZA\create_marker_instance_delegate
  • WPGMZA\query_nominatim_cache
  • WPGMZA\Selector\trace
  • WPGMZA\store_nominatim_cache
  • wpgmza_backwards_compat_get_all_circle_data
  • wpgmza_backwards_compat_get_all_rectangle_data
  • wpgmza_check_admin_head_backwards_compat_v6
  • wpgmza_check_map_editor_backwards_compat_v6
  • wpgmza_check_pro_compat_required_v6
  • wpgmza_check_user_backwards_compat_v6
  • Overview
  • Namespace
  • Class
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 
<?php

namespace WPGMZA;

/**
 * Server side Google Maps geocoding.
 */
class GoogleGeocoder
{
    /**
     * @var The Google API key to be used for geocoding
     */
    public $apiKey;
    
    /**
     * Constructor.
     * @param string $apiKey The Google Maps API key to use for geocoding
     */
    public function __construct($apiKey)
    {
        $this->apiKey = $apiKey;
    }
    
    /**
     * Converts an address to a latitude longitude pair.
     * @param string $address The address to geocode
     * @throws \Exception cURL must be enabled to use this feature
     * @throws \Exception Failed to parse JSON response
     * @throws \Exception Failed to geocode address for reason given by Google
     * @return object An object with the lat and lng of the first coordinate pair returned by Google
     */
    public function getLatLngFromAddress($address)
    {
        if(!function_exists('\curl_init'))
            throw new \Exception('cURL must be enabled to use this feature');
        
        $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&key=' . urlencode($this->apiKey) . '&sensor=false';
        
        $ch = curl_init($url);
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
        
        $result = curl_exec($ch);
        
        $json = json_decode($result);

        do_action( 'wpgmza_google_maps_geocoded_result', $json );
        
        if(!$json)
        {
            $urlWithKeyHidden = preg_replace('/key=[A-Za-z0-9_\-]+/', '[api key]', $url);
            throw new \Exception("Failed to parse JSON response from $urlWithKeyHidden: " . $result . " (cURL error: " . curl_error($ch) . ")");
        }
        
        if(!property_exists($json, 'results'))
            throw new \Exception('Failed to geocode address "'.$address.'": ' . print_r($json, true));
        
        if($json->status != "OK")
            throw new \Exception('Failed to geocode address: "' . $address . '"');
        
        if(empty($json->results))
            return false;
        
        $result = $json->results[0];
        
        $location = $result->geometry->location;

        do_action( 'wpgmza_google_maps_geocoded_result_location', $location );
        
        return (object)array(
            'lat' => $location->lat, 
            'lng' => $location->lng
        );
    }
}

?>
API documentation generated by ApiGen