5 months ago
<?php
/**
** Facet automatically indexes latitude and longitude from an ACF map field
** This will index other data saved in other sub fields
** Note that these fields are optional and may not exist
** See ACF docs for more - https://www.advancedcustomfields.com/resources/google-map/#template-usage
**/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'my_facet' == $params['facet_name'] ) { // change my_facet to name of your facet
$post_id = $params['post_id'];
$acf = str_replace( 'acf/', '', $params['facet_source'] );
$location = get_field( $acf, $post_id );
$value = $location['city'] ?? '';
if ( '' != $value ) {
$params['facet_value'] = $value;
$params['facet_display_value'] = $value;
} else {
$params['facet_value'] = ''; // don't index
}
}
return $params;
}, 10, 2 );
6 months ago
<?php
add_action('facetwp_scripts', function () {
?>
<script>
(function($) {
document.addEventListener('facetwp-refresh', function() {
if ( 'undefined' != typeof FWP_MAP && true === FWP_MAP.is_filtering) {
$('.facetwp-location').val(''); // reset the value of the input field
$('.facetwp-lat').val(''); // reset hidden field
$('.facetwp-lng').val(''); // reset hidden field
$('.facetwp-radius option').each(function () {
if (this.defaultSelected) {
this.selected = true;
return false;
} // reset default radius if it is a dropdown
});
FWP.facets['my_proximity_facet'] = []; // change 'my_proximity_facet' to name of your Proximity facet
}
});
})(fUtil);
</script>
<?php
}, 100);
7 months ago
<?php
/** turn off map filtering when resetting the map facet **/
add_action('facetwp_scripts', function () {
?>
<script>
(function($) {
FWP.hooks.addAction('facetwp/reset', function() {
$.each(FWP.facet_type, function(type, name) {
if ('map' === type) {
var $button = $('.facetwp-map-filtering');
$button.text(FWP_JSON['map']['filterText']);
FWP_MAP.is_filtering = false;
$button.toggleClass('enabled');
}
});
});
})(fUtil);
</script>
<?php
}, 100);
10 months ago
<style>
.gm-style-iw-c,
.gm-style-iw-tc {
display: none;
}
</style>
10 months ago
<?php
add_filter( 'facetwp_render_output', function( $output, $params ) {
if ( FWP()->ajax->is_preload && isset( $output['settings']['map']['locations'] ) ) {
$output['settings']['map']['locations'] = [];
}
return $output;
}, 11, 2 );
1 year ago
<?php
/**
** Adds marker click action to add a circle radius around clicked marker
** Clicking another marker clears the previous circle
** Note: facetwp_scripts action requires FacetWP v4.2.2+
** More info: https://facetwp.com/help-center/facets/facet-types/map/advanced-map-customizations/#display-a-circle-or-other-shape-around-clicked-markers
**/
add_action( 'facetwp_scripts', function() {
?>
<script>
(function($) {
FWP.hooks.addAction('facetwp_map/marker/click', function(marker) {
// Clear existing marker if it exists
if ( 'undefined' !== typeof FWP_MAP.circ )
FWP_MAP.circle.setMap(null); // Clear circle from map
FWP_MAP.circle = null; // Remove circle instance entirely
// var post_id = marker.post_id; // The marker's post_id could be used to apply this code only to certain markers.
var lat = marker.position.lat();
var lng = marker.position.lng();
FWP_MAP.circ = new google.maps.Circle({
center: {
lat: lat,
lng: lng
},
radius: 25000, // Circle radius in meters
fillColor: '#FF0000', // Circle coloring
fillOpacity: 0.35, // Circle opacity
strokeColor: "#FF0000", // Circle outline color
strokeOpacity: 0.8, // Circle outline opacity
strokeWeight: 2, // Circle outline thickness
map: FWP_MAP.map
});
// Optional: when an infoWindow is closed, clear the circle from the map
google.maps.event.addListener(FWP_MAP.infoWindow, 'closeclick', function() {
FWP_MAP.circle.setMap(null);
});
// Optional: when the map is clicked anywhere, clear the circle from map
google.maps.event.addListener(FWP_MAP.map, 'click', function(event) {
FWP_MAP.circle.setMap(null);
});
});
})(fUtil);
</script>
<?php
}, 100 );
2 years ago
<?php
/** basic check to keep only one map marker per location
** assumes lat is unique without having to check lng also
**/
Class FWPMapDulicateChecker
{
public $locations = [];
function __construct() {
add_filter( 'facetwp_map_marker_args', function( $args, $post_id ) {
$lat = $args['position']['lat'];
if ( false === array_search( $lat, $this->locations ) ) {
$this->locations[] = $lat;
} else {
return false;
}
return $args;
}, 10, 2 );
}
}
new FWPMapDulicateChecker();
2 years ago
<?php
/** be sure your link has a data-id attribute with the postid and a data-lat attribute with the latitude
**/
add_action( 'wp_head', function() { ?>
<script>
(function($) {
$( 'body' ).on( 'click', '.post-item', function(e) {
e.preventDefault(); // Necessary if '.post-item' is an <a> tag and you don't want it to open the post page itself.
var postid = $(this).attr('data-id');
var postlat = $(this).attr('data-lat'); // add a data-lat to the <a> tag with the location's latitude value
var marker = FWP_MAP.get_post_markers(postid);
$.each( marker, function( key, value ) {
if ( value.position.lat() == postlat ) {
FWP_MAP.map.setCenter({
lat: value.position.lat(),
lng: value.position.lng()
});
FWP_MAP.is_zooming = true; // Needed if you are using the "Enable map filtering" button
FWP_MAP.map.setZoom(17); // Set a zoom level between 1 and 20. Make sure it is higher than the marker clusterer's bottom limit.
// google.maps.event.trigger(value, 'click'); // If you don't have spiderfied markers
google.maps.event.trigger(value, 'spider_click'); // If you have spiderfied markers. Will also work if you don't have spiderfied markers.
}
});
});
})(jQuery);
</script>
<?php } );
3 months ago
<?php
/**
* Set marker content specific to the ACF repeater locaton.
* Change 'locations_multi' to the name of the ACF repeater field.
* Change 'store' to the name of the Google Map field within the repeater field.
* Add other or different code as needed to $args['content']. This overwrites anything in the 'Marker content' setting.
* For more info and an example on how to use other custom fields within the repeater field, see:
* https://facetwp.com/help-center/facets/facet-types/map/#add-marker-content-from-acf-repeater-fields
**/
add_filter( 'facetwp_map_marker_args', function( $args, $post_id ){
$latitude = $args['position']['lat'];
$locations = get_field( 'locations_multi', $post_id );
foreach ( $locations AS $location ) {
if ( (string)$location['store']['lat'] == (string)$latitude ) {
$args['content'] = $location['store']['address']; // Retrieves Google address from the Google Map field.
// Or:
// $args['content'] = $location['other_field']; // Retrieves value from another custom field in the repeater
break;
}
}
return $args;
}, 10, 2 );
3 years ago
<?php
// Add to your (child) theme's functions.php
// Note that this is untested, and may have side effects!
add_filter( 'facetwp_map_marker_args', function( $args, $post_id ) {
if ( ! isset( FWP()->map_locations ) ) {
FWP()->map_locations = [];
}
// generate the hash for checking duplicates
$hash = $args['position']['lat'] . ':' . $args['position']['lng'];
// skip if this exact location has already been used
if ( isset( FWP()->map_locations[ $hash ] ) ) {
return false;
}
FWP()->map_locations[ $hash ] = true;
return $args;
}, 10, 2 );