→ Proximity

Overview

The proximity facet lets you find results near a specified location.

facet-proximity

Features

  • Includes a geolocate button to pinpoint your current location
  • Uses Google Maps Geocoding API to turn coordinates into an address
  • Uses Google Places Autocomplete
  • Includes a radius chooser
IMPORTANT: For the geolocation button to work in Chrome, the page needs to be HTTPS-enabled. Learn more »

How it Works

The proximity autocomplete box is powered by Google. When you select a location, Google returns the latitude, longitude of that location’s central point.

IMPORTANT: If you search for “Italy” with a 100 kilometer radius, the proximity facet will not automatically return all matches in Italy.

Instead, it will return matches within 100 kilometers from Italy’s center, which could include matches from other countries as well.

Usage

The Data Source should be a custom field containing a comma-separated latitude, longitude.

Listify users: set the facet’s Data Source to geolocation_lat.

If your posts are not geocoded, try the Address Geocoder plugin. If you go this route, then choose martygeocoderlatlng for the data source.

Available Options

Name Description
Unit of measurement Miles or kilometers

Using a Google Maps API Key

For high traffic sites, you may need an API key to bypass usage limits.

To use a Google Maps API key, add the following code to your site’s wp-config.php, replacing my_api_key with your actual key.

define( 'GMAPS_API_KEY', 'my_api_key' );

Get an API key »

Customize the Radius Options

The first array element is the default radius (feel free to change).

function mysite_radius_options( $options ) {
    return array( 10, 25, 50, 100, 250 );
}
add_filter( 'facetwp_proximity_radius_options', 'mysite_radius_options' );

Limit Autocomplete to a Specific Country

Force the autocomplete to show only choices in Germany (country code = “de”).

function fwp_proximity_specific_country( $options ) {
    $options['componentRestrictions'] = array(
        'country' => 'de',
    );
    return $options;
}
add_filter( 'facetwp_proximity_autocomplete_options', 'fwp_proximity_specific_country' );

Limit Autocomplete to Showing Cities

function fwp_proximity_cities( $options ) {
    $options['types'] = ['(cities)'];
    return $options;
}
add_filter( 'facetwp_proximity_autocomplete_options', 'fwp_proximity_cities' );