Skip to content

Instantly share code, notes, and snippets.

@havvg
Created June 28, 2012 21:04
Show Gist options
  • Save havvg/3013886 to your computer and use it in GitHub Desktop.
Save havvg/3013886 to your computer and use it in GitHub Desktop.
Google Maps union multiple polygon within BackboneJS view
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