Created
June 23, 2013 01:17
-
-
Save hpneo/5843329 to your computer and use it in GitHub Desktop.
Using gmaps.markerFilter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how i can set visible on the map only the markers returned in filtered_markers?