Tribe__Events__Pro__Geo_Loc::geocode_address( string $address, int $venue_id, array $pieces = array() )

Attempts to perform the geocode resolution of an address.


Parameters

$address

(string) (Required) The formatted address.

$venue_id

(int) (Required) The post ID of the Venue, if any, the address of which is currently being resolved.

$pieces

(array) (Optional) An array containing the address pieces for this Venue.

Default value: array()


Top ↑

Return

(array|false) Either an array containing the Venue latitude and longitude coordinates in the shape [ 'lat' => <lat>, 'lng' => <lng> ] or false if the address could not be resolved to a set of coordinates.


Top ↑

Source

File: src/Tribe/Geo_Loc.php

	public function geocode_address( $address, $venue_id = 0, $pieces = array() ) {
		/**
		 * Allows customizing whether the Google Maps Geocode API will be used for geocoding addresses.
		 *
		 * @since 4.7
		 *
		 * @param boolean $geocode_addresses Whether the Geocode Address API is enabled for geocoding addresses.
		 * @param int     $venue_id          Venue post ID.
		 * @param string  $address           Address string that will be used for geocoding.
		 */
		$geocode_addresses = apply_filters( 'tribe_events_pro_geocode_addresses', true, $venue_id, $address );

		if ( false === $geocode_addresses ) {
			return false;
		}

		/**
		 * Allows filtering the geocode resolution completely to use a custom solution.
		 *
		 * Returning a non `null` value here will bail out of the geocode resolution solution
		 * implemented by the plugin completely.
		 *
		 * @since 4.7
		 *
		 * @param array  $result   An array specifying the latitude and longitude coordinates for
		 *                         the address or `false` to indicate a failure in resolving the
		 *                         address to a set of coordinates; returning a non `null` value
		 *                         here will make the function return the filtered value.
		 * @param string $address  The formatted address string.
		 * @param int    $venue_id The Venue post ID.
		 * @param array  $pieces   The Venue address pieces.
		 */
		$result = apply_filters( 'tribe_events_pro_geocode_resolved', null, $address, $venue_id, $pieces );
		if ( null !== $result ) {
			return $result;
		}

		$api_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode( $address );
		$api_key = tribe_get_option( 'google_maps_js_api_key' );

		if ( ! empty( $api_key ) && is_string( $api_key ) ) {
			$api_url = add_query_arg( array( 'key' => $api_key ), $api_url );
		}

		/**
		 * Allows customizing the Google Maps Geocode API URL for a venue's address, which URL is
		 * used to validate the address as one that can be plotted on a Google Map.
		 *
		 * @param string $api_url The Google Maps Geocode API URL for this venue's address.
		 */
		$api_url = apply_filters( 'tribe_events_pro_geocode_request_url', $api_url );

		$data = wp_remote_get( $api_url );

		if ( is_wp_error( $data ) || ! isset( $data['body'] ) ) {
			tribe( 'logger' )->log_warning( sprintf(
				_x( 'Geocode request failed ($1%s - $2%s)', 'debug geodata', 'tribe-events-calendar-pro' ),
				is_wp_error( $data ) ? $data->get_error_code() : _x( 'empty response', 'debug geodata' ),
				$api_url
			),
				__METHOD__
			);

			return false;
		}

		$data_arr = json_decode( $data['body'] );

		if ( isset( $data_arr->status ) && 'OVER_QUERY_LIMIT' === $data_arr->status ) {
			if ( $this->over_query_limit_displayed ) {
				return false;
			}

			set_transient( 'tribe-google-over-limit', 1, time() + MINUTE_IN_SECONDS );

			$this->over_query_limit_displayed = true;

			return false;
		}

		if ( empty( $data_arr->results[0] ) ) {
			return false;
		}

		$result     = array();
		$geo_result = $data_arr->results[0];

		if ( isset( $geo_result->geometry->location->lat ) ) {
			$result['lat'] = (string) $geo_result->geometry->location->lat;
		}

		if ( isset( $geo_result->geometry->location->lng ) ) {
			$result['lng'] = (string) $geo_result->geometry->location->lng;
		}

		/**
		 * Allows further processing of geodata for Venue.
		 *
		 * @since 4.4.31
		 *
		 * @param int    $venueId    Venue ID.
		 * @param object $geo_result Geo result object.
		 * @param array  $pieces     User provided address pieces.
		 */
		do_action( 'tribe_geoloc_save_venue_geodata', $venue_id, $geo_result, $pieces );

		return $result;
	}

Top ↑

Changelog

Changelog
Version Description
4.7 Introduced.