Skip to content

Instantly share code, notes, and snippets.

@ericjames
Last active December 19, 2016 16:03
Show Gist options
  • Save ericjames/40542e69ac8237f0f7111812088734c1 to your computer and use it in GitHub Desktop.
Save ericjames/40542e69ac8237f0f7111812088734c1 to your computer and use it in GitHub Desktop.
Converts KML shapefile syntax into a Google Maps Polyline. It is rather ironic this is not built into Gmaps since it is Google's spec.
function getPolylines(geometry, style) {
// Parse Geometry
var kmlstring = geometry.toString();
kmlstring = kmlstring.substr(40);
kmlstring = kmlstring.substr(0, kmlstring.length - 43);
var linestrings = kmlstring.split("</coordinates></LineString><LineString><coordinates>");
// Set polylines
var polylines = [];
for (var i in linestrings) {
var linepoints = linestrings[i].split(",0 ");
var routeLine = createLine(linepoints, style.color, style.stroke, style.opacity, style.zIndex);
polylines.push(routeLine);
}
return polylines;
};
function createLine(linepoints, style, stroke, opacity, zIndex) {
var coordArray = [];
for (var i in linepoints) {
var coord = linepoints[i].split(",");
var lat = coord[1];
var lng = coord[0];
var lineCoord = new google.maps.LatLng(lat, lng);
coordArray.push(lineCoord);
}
var routeLine = new google.maps.Polyline({
path: coordArray,
strokeColor: style,
strokeOpacity: opacity,
strokeWeight: stroke,
zIndex: zIndex
});
return routeLine;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment