google.maps.Polygon.getCentroid
/** Mixin to extend the behavior of the Google Maps JS API Polygon type with logic to find the centroid of the polygon
HTML
<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=geometry"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<div id="map-canvas"></div>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
CSS
#map-canvas{
height: 200px;
width: 100%;
background-color: red;
}
JavaScript
/** Mixin to extend the behavior of the Google Maps JS API Polygon type
* with logic to find the centroid of the polygon
*
* Implements the algorithm @ http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
*
* Tested against v3.14 of the GMaps API.
*
* @author [email protected]
*
* @license http://opensource.org/licenses/MIT
*
* @version 1.0
*
* @mixin
*/
(function(){
"use strict";
var category = 'google.maps.Polygon.getCentroid';
// check that the GMaps API was already loaded
if (null == google || null == google.maps || null == google.maps.Polygon) {
console.error(category, 'Google Maps API not found');
return;
}
/** Mixin to extend the behavior of the Google Maps JS API Polygon type
* to compute the centroid of a polygon
*
* Tested against v3.14 of the GMaps API.
*
* @author [email protected]
*
* @license http://opensource.org/licenses/MIT
*
* @version 1.0
*
* @mixin
*
* @extends {google.maps.Polygon}
*
* @param {(number|Array|google.maps.MVCArray)} [path] - an optional polygon path; defaults to the first path of the polygon
* @returns {boolean} true if the path is clockwise; false if the path is counter-clockwise
*/
function getCentroid(path) {
var self = this,
vertexes;
if (!self instanceof google.maps.Polygon)
return;
if (null === path)
throw new Error('Path is optional, but cannot be null');
// default to the first path
if (arguments.length === 0)
path = self.getPath();
// support for passing an index number to a path
if (typeof(path) === 'number')
path = self.getPaths().getAt(path);
if ( (!(path instanceof Array)) && (!(path instanceof google.maps.MVCArray)) )
throw new Error('Path must be an Array or MVCArray');
if (path instanceof google.maps.MVCArray)
vertexes = path.getArray();
vertexes =...