├── LICENSE
├── README.md
├── demo1.png
├── demo2.png
├── index.css
├── index.html
├── index.js
└── screenshot.png
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 Modus Create
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 | ### [DEPRECATED] This repository is no longer maintained
3 | > While this project is fully functional, the dependencies are no longer up to date. You are still welcome to explore, learn, and use the code provided here.
4 | >
5 | > Modus is dedicated to supporting the community with innovative ideas, best-practice patterns, and inspiring open source solutions. Check out the latest [Modus Labs](https://labs.moduscreate.com?utm_source=github&utm_medium=readme&utm_campaign=deprecated) projects.
6 |
7 | [](https://labs.moduscreate.com?utm_source=github&utm_medium=readme&utm_campaign=deprecated)
8 |
9 | ---
10 |
11 | # Polygon Experiment
12 |
13 | Experiments with placing a marker around the center of a polygon. This repo has an accompanying [blog post](http://moduscreate.com/placing-markers-inside-polygons-with-google-maps/).
14 |
15 | 
16 |
17 | Find approximate center point of an arbitrary polygon on Google Maps. Process:
18 |
19 | * Add a `getBoundingBox` method to `google.maps.Polygon.prototype` which returns a LatLngBounds object (rectangle) that entirely contains an arbitrarily complex polygon
20 | * Get the center of that bounding box
21 | * If the center of the bounding box is within the area of the polygon, put the marker there
22 | * If the center of the bounding box is not within the area of the polygon then:
23 | * Work out the height of the bounding box
24 | * Look at points North, East, South and West of the center at 5% increments of the total height and width of the bounding box
25 | * If any of those points is within the area of the polygon, place the marker there and stop looking
26 |
27 | This may not be foolproof but should get a point within the polygon that's good enough. As this moves up and down the bounding box
28 | looking for points within the polygon at 5% height increments, it could miss a very thin slice of the polygon that crosses the
29 | center line and never find a point... could fix this by using 1% increments and a 50 loop count for higher search "resolution"
30 | but lower performance.
31 |
32 | ## Examples
33 |
34 | The following show two sample polygons where the centroid (blue) of the bounding box (shown as black outline) falls outside the polygon.
35 | The algotithm found the nearest point North, South, East or West and decided on a final placement where the red marker lies.
36 |
37 | 
38 |
39 | This example selected the point due East of the centroid because the polygon is much wider than it is high, so the East / West search
40 | increments were bigger than the North / South ones, so the Eastern border was discovered first.
41 |
42 | 
43 |
44 | ## Notes
45 |
46 | * You need to supply a Google Maps API key and set this in the link to the Google Maps API JavaScript in `index.html`
47 | * Because we are using `google.maps.geometry` functions, the link to get the Google Maps API JavaSCript needs to include `&libraries=geometry` (see `index.html`)
48 |
49 | ## Other Ways of Doing This
50 |
51 | There are other possible algorithms for getting similar results:
52 |
53 | * MapBox has one [here](https://github.com/mapbox/polylabel/blob/master/index.js) with a supporting [blog post] (https://www.mapbox.com/blog/polygon-center/)
54 | * [Discussion on calculating centroid](http://mathcentral.uregina.ca/qq/database/qq.09.07/h/david7.html)
55 |
--------------------------------------------------------------------------------
/demo1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ModusCreateOrg/google-maps-polygon-center-approximation-blog/8faff17af84f367710f8c25bd37ffd7b7281c986/demo1.png
--------------------------------------------------------------------------------
/demo2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ModusCreateOrg/google-maps-polygon-center-approximation-blog/8faff17af84f367710f8c25bd37ffd7b7281c986/demo2.png
--------------------------------------------------------------------------------
/index.css:
--------------------------------------------------------------------------------
1 | #map {
2 | height: 600px;
3 | position: relative;
4 | width: 100%;
5 | }
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |