Created
June 28, 2012 21:04
-
-
Save havvg/3013886 to your computer and use it in GitHub Desktop.
Google Maps union multiple polygon within BackboneJS view
This file contains 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
var MapView = Backbone.View.extend({ | |
/** | |
* Combine the boundaries of a list of Polygons into one boundary. | |
* | |
* The resulting boundary may for example be used in Map.fitBounds(). | |
* | |
* @param {google.maps.Polygon[]} | |
* | |
* @return {google.maps.LatLngBounds} | |
*/ | |
unionBounds: function(polygons) { | |
return _.reduce(polygons, function(bounds, polygon) { | |
return bounds.union(polygon.getBounds()); | |
}, new google.maps.LatLngBounds()); | |
} | |
}); | |
// example getBounds method | |
google.maps.Polygon.prototype.getBounds = function() { | |
var bounds = new google.maps.LatLngBounds(); | |
this.getPaths().forEach(function(path) { | |
path.forEach(function(point) { | |
bounds.extend(point); | |
}); | |
}); | |
return bounds; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment