Skip to content

Instantly share code, notes, and snippets.

@DylanCodeCabin
Created February 22, 2023 05:34
Show Gist options
  • Save DylanCodeCabin/c511a9d87ac20d37fe2a56335156b556 to your computer and use it in GitHub Desktop.
Save DylanCodeCabin/c511a9d87ac20d37fe2a56335156b556 to your computer and use it in GitHub Desktop.
/**
* This script will automatically reload the marker data via the REST API
*
* It only appends new markers, meaning this is somewhat resful
*
* Currently, this does not deal with shapes or any other data (markers only)
*
* Adjust 'refresh.seconds' to change how frequently the polling request is made
*/
jQuery(($) => {
if(!WPGMZA || !WPGMZA.maps){
return;
}
const refresh = {
seconds : 10,
fetchRequest : false
};
refresh.plot = (data) => {
if(refresh.map && data && data.markers){
for(let i in data.markers){
const markerData = data.markers[i];
const exists = refresh.map.getMarkerByID(markerData.id);
if(!exists){
const marker = WPGMZA.Marker.createInstance(markerData);
refresh.map.addMarker(marker);
}
}
}
};
refresh.poll = () => {
refresh.map = WPGMZA.maps[0];
let filter = refresh.map.markerFilter.getFilteringParameters();
refresh.map.showPreloader(true);
if(refresh.fetchRequest){
refresh.fetchRequest.abort();
}
let data = refresh.map.getRESTParameters({
filter: JSON.stringify(filter)
});
this.fetchRequest = WPGMZA.restAPI.call("/features/", {
useCompressedPathVariable: true,
data: data,
success: function(result, status, xhr) {
refresh.map.showPreloader(false);
refresh.fetchRequest = false;
refresh.plot(result);
}
});
};
refresh.timer = setInterval(() => {
refresh.poll();
}, refresh.seconds * 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment