Last Updated: February 25, 2016
·
9.666K
· havvg

GeoJSON MultiPolygon to Google Maps Polygon

// A MultiPolygon for German borders
// see http://www.naturalearthdata.com/
var coordinates = [[[[6.743,53.578],[6.75,53.572],[6.757,53.563],[6.748,53.566],[6.735,53.575],[6.727,53.577],[6.716,53.576],[6.702,53.571],[6.696,53.57],[6.678,53.575],[6.664,53.587],[6.66,53.599],[6.672,53.604],[6.748,53.618],[6.785,53.62],[6.799,53.604],[6.776,53.602],[6.741,53.593],[6.723,53.591],[6.723,53.584],[6.734,53.582],[6.743,53.578]]],[[[7.086,53.687],[6.874,53.672],[6.911,53.683],[7.048,53.694],[7.086,53.687]]]]; // incomplete

var paths = _.map(coordinates, function(entry) {
    return _.reduce(entry, function(list, polygon) {
        // This map() only transforms the data.
        _.each(_.map(polygon, function(point) {
            // Important: the lat/lng are vice-versa in GeoJSON
            return new google.maps.LatLng(point[1], point[0]);
        }), function(point) {
            list.push(point);
        });

        return list;
    }, []);
});

var polygon = new google.maps.Polygon({
    paths: paths,
});

// given "map" is defined, this will display the polygon on the map
polygon.setMap(map);

1 Response
Add your response

Works great. thanks.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside