Skip to content

Instantly share code, notes, and snippets.

@DylanCodeCabin
Last active February 17, 2022 13:53
Show Gist options
  • Save DylanCodeCabin/fc2390ad22b4bc7c52a0ff680b18471e to your computer and use it in GitHub Desktop.
Save DylanCodeCabin/fc2390ad22b4bc7c52a0ff680b18471e to your computer and use it in GitHub Desktop.
jQuery(function(){
jQuery(window).load(function(){
WPGMZA.MarkerClusterer.prototype.redraw = function() {
WPGMZA.MarkerClusterer.log("redraw called", this);
this.map.clusterSepBridge = true;
if(typeof this.shouldRegenNVC === 'undefined'){
this.shouldRegenNVC = true;
}
if(this.maxZoom && this.map.getZoom() >= this.maxZoom){
this.map.markers.forEach(function(marker) {
if(!marker.isFiltered && (!marker.separatorGroup || marker.separatorGroup.state != WPGMZA.MarkerSeparatorGroup.STATE_CLOSED)){
marker.setVisible(true);
}
});
/* Proxy NVC Relay */
if(WPGMZA.settings.wpgmza_near_vicinity_control_enabled && this.shouldRegenNVC){
this.shouldRegenNVC = false;
if(this.map){
if(this.map.markerSeparator){
if(this.map.markerSeparator.groups && this.map.markerSeparator.groups.length > 0){
for(var gI in this.map.markerSeparator.groups){
var groupMarker = this.map.markerSeparator.groups[gI].placeholder;
this.map.removeMarker(groupMarker);
}
}
this.map.markerSeparator.destroy();
}
this.map.markerSeparator = new WPGMZA.MarkerSeparator(this.map.id);
}
}
return;
}
this.shouldRegenNVC = true;
this.setReady(true);
this.createClusters();
}
WPGMZA.MarkerSeparator.prototype.groupMarkers = function(){
var start = new Date().getTime();
var self = this;
var points = [];
var markers = this.getMarkers();
if(!markers.length)
return;
var distanceFunction;
var position = markers[0].getPosition();
var minLat = position.lat,
maxLat = position.lat,
avgLat = 0, latRange;
markers.forEach(function(marker) {
if(marker.isFiltered){
return;
}
var latLng = WPGMZA.MarkerSeparator.getNativeLatLng(marker.getPosition());
minLat = Math.min(latLng.lat, minLat);
maxLat = Math.max(latLng.lat, maxLat);
avgLat += latLng.lat;
latLng.marker = marker;
points.push(latLng);
});
avgLat /= markers.length;
latRange = Math.abs(maxLat - minLat);
if(latRange < 5 || WPGMZA.settings.forceCheapRuler)
{
var cheapRuler = new CheapRuler(avgLat, "kilometers");
distanceFunction = function(a, b) {
return cheapRuler.distance([a.lat, a.lng], [b.lat, b.lng]);
};
}
else
distanceFunction = WPGMZA.Distance.between;
var tree = new kdTree(points, distanceFunction, ["lat", "lng"]);
for(var i = 0; i < markers.length; i++)
{
if(markers[i].separatorGroup)
continue;
if(!markers[i].getVisible() || markers[i].isFiltered){
/* Marker not visible at the moment, don't create any group */
continue;
}
// TODO: Add max group size setting, add warning when groups are full
var marker = markers[i];
if(marker.separatorGroup)
continue;
var maxGroupSize = (WPGMZA.settings.marker_separator_maximum_group_size ? WPGMZA.settings.marker_separator_maximum_group_size : 8)
var nearest = tree.nearest(marker.getPosition(), maxGroupSize, [this.threshold]);
var group = null;
for(var j = 0; j < nearest.length; j++){
var other = nearest[j][0].marker;
if(other === marker)
continue;
if(other.separatorGroup)
continue;
if(!group)
{
group = new WPGMZA.MarkerSeparatorGroup();
group.addMarker(marker);
}
group.addMarker(other);
}
if(!group)
continue;
if(WPGMZA.isProVersionBelow7_10_00){
group.placeholder.googleMarker.setMap(this.map);
}else{
this.map.addMarker(group.placeholder);
}
this.groups.push(group);
}
}
});
jQuery(document.body).on("filteringcomplete.wpgmza", function(event) {
if(event.map){
const map = event.map;
setTimeout(function(){
if(map.markerClusterer){
map.markerClusterer.shouldRegenNVC = true;
map.markerClusterer.redraw();
}
}, 300);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment