Skip to content

Instantly share code, notes, and snippets.

@hpneo
Created June 23, 2013 01:17
Show Gist options
  • Save hpneo/5843329 to your computer and use it in GitHub Desktop.
Save hpneo/5843329 to your computer and use it in GitHub Desktop.
Using gmaps.markerFilter
GMaps.prototype.markerFilter = function(conditions) {
var filtered_markers = [], i = 0, detail, marker;
for (i = 0; i < this.markers.length; i++) {
marker = this.markers[i];
if (conditions instanceof Array) {
if (conditions.indexOf(marker.detail.id) > -1) {
filtered_markers.push(marker);
}
}
else if (conditions instanceof Function) {
if (conditions.call(marker.detail) === true) {
filtered_markers.push(marker);
}
}
else if (conditions instanceof Object) {
for (var c in conditions) {
if (marker.detail[c] == conditions[c]) {
filtered_markers.push(marker);
break;
}
}
}
}
return filtered_markers;
};
// Example
/*
var map = new GMaps({lat: ...});
map.addMarker({
lat: -7,
lng: -12,
detail: {
id: 10
}
});
map.addMarker({
lat: -7,
lng: -13,
detail: {
id: 21,
user_id: 616
}
});
...
*/
// Filtering with array
map.markerFilter([10, 35, 742]);
// => Array of markers which id (defined in detail) are included in [10, 35, 742]
// Filtering with function
map.markerFilter(function(detail) {
return detail.user_id == 616 && detail.id >= 21;
});
// => Array of markers for which the function returns a true value
// Filtering with object
map.markerFilter({
user_id: 616
});
// => Array of markers for which their attributes are the same as { user_id: 616 }
@xzegga
Copy link

xzegga commented Jan 11, 2016

how i can set visible on the map only the markers returned in filtered_markers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment