Created
October 4, 2018 13:04
-
-
Save Tilotiti/c386507fb756c42b173b956271bb81b8 to your computer and use it in GitHub Desktop.
Google Map : Join Polygons
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
// Import JSTS : http://bjornharrtell.github.io/jsts/ | |
// Import Google Map API | |
var map = new google.maps.Map(document.getElementById('map'), { | |
mapTypeId: google.maps.MapTypeId.TERRAIN | |
}); | |
var bound = new google.maps.LatLngBounds(); | |
var paths = [ | |
// Polygon 1 | |
[ | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
], | |
// Polygon 2 | |
[ | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
], | |
// Polygon 3 | |
[ | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
{lat: 0.00000, lng: 0.00000}, | |
] | |
]; | |
for(var i in paths) { | |
for(var z in paths[i]) { | |
bound.extend(paths[i][z]); | |
} | |
} | |
var polygonPath = mergePath(paths); | |
new google.maps.Polygon({ | |
map: map, | |
paths: polygonPath, | |
strokeColor: '#000', | |
strokeOpacity: 0.3, | |
strokeWeight: 2, | |
fillColor: '#0000FF', | |
fillOpacity: 0.8 | |
}); | |
map.fitBounds(bound); | |
function mergePath(paths) { | |
var polyUnion = null; | |
var geometryFactory = new jsts.geom.GeometryFactory(); | |
for(var i in paths) { | |
var linearRing = []; | |
for(var z in paths[i]) { | |
linearRing.push( | |
new jsts.geom.Coordinate(paths[i][z].lat, paths[i][z].lng) | |
); | |
} | |
var jtspoly = geometryFactory.createPolygon(geometryFactory.createLinearRing(linearRing)); | |
jtspoly.normalize(); | |
if(polyUnion == null) { | |
polyUnion = jtspoly; | |
} else { | |
polyUnion = polyUnion.union(jtspoly); | |
} | |
} | |
var path = []; | |
var coordinates = polyUnion.getCoordinates(); | |
for(var i in coordinates) { | |
path.push(new google.maps.LatLng(coordinates[i].x, coordinates[i].y)); | |
} | |
return path; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment