Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Converts polygon xml from KML files to BingMaps 7.0 Polygon objects

View bingKmlParser.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
var bingKmlParser = function() {
 
// private variables
var _styles = {};
var _styleMap = {};
 
 
// private methods
 
function _parsePolygons(placemarkXml) {
var vertices;
$(placemarkXml).find('Polygon').find('coordinates').each(function(index, coordinates) {
vertices = _parseVertices(coordinates.firstChild.data);
});
 
var colorName = $(placemarkXml).find('styleUrl').contents()[0].data.substr(1);;
var polygonOptions = _styleMap[colorName];
 
var polygon = new Microsoft.Maps.Polygon(vertices, polygonOptions);
 
return polygon;
}
 
function _parseVertices(coordinateString) {
coordinateString = $.trim(coordinateString);
var coordinates = coordinateString.split(' ');
var msLocations = [];
 
for (i=0; i<coordinates.length; i++) {
var coordinate = coordinates[i];
var xyz = coordinate.split(',');
 
var location = new Microsoft.Maps.Location(xyz[1],xyz[0],xyz[2]);
msLocations.push(location);
}
 
return msLocations;
};
 
/**
* Parse a KML color string that comes as a abgr hex string.
* @param kmlColorString An abgr hex string
* @return {Microsoft.Maps.Color}
* @private
*/
function _parseColor(kmlColorString) {
// Our KML files have color strings as hex in abgr order. Here we swap them to argb order.
var hexArray = [kmlColorString.charAt(0) + kmlColorString.charAt(1),
kmlColorString.charAt(6) + kmlColorString.charAt(7),
kmlColorString.charAt(4) + kmlColorString.charAt(5),
kmlColorString.charAt(2) + kmlColorString.charAt(3)];
 
var argbArray = [];
for (i=0; i<hexArray.length; i++) {
argbArray.push(parseInt(hexArray[i], 16));
}
 
return new Microsoft.Maps.Color(argbArray[0], argbArray[1], argbArray[2], argbArray[3]);
};
 
/**
* Create a map of style names to styles (PolygonOptions - http://msdn.microsoft.com/en-us/library/gg427596.aspx).
* @param kmlXml
* @return {Object} a map of <String, PolygonOptions>
* @private
*/
function _parseStyles(kmlXml) {
$(kmlXml).find('Style').each(function(index, styleXml) {
var styleId = $(styleXml).attr('id');
var msPolygonOptions = {};
 
if ($(styleXml).find('PolyStyle').length > 0) {
msPolygonOptions.fillColor = _parseColor($(styleXml).find('PolyStyle').find('color').contents()[0].data);
msPolygonOptions.strokeColor = _parseColor($(styleXml).find('LineStyle').find('color').contents()[0].data);
msPolygonOptions.strokeThickness = parseInt($(styleXml).find('LineStyle').find('width').contents()[0].data);
}
 
_styles[styleId] = msPolygonOptions;
});
 
$(kmlXml).find('StyleMap').each(function(index, styleMapXml) {
var styleMapId = $(styleMapXml).attr('id');
var mappedStyle = $(styleMapXml).find('styleUrl').contents()[0].data.substr(1);
 
_styleMap[styleMapId] = _styles[mappedStyle];
});
}
 
/**
* public functions that are available using this object.
*/
var _public = {
parsePolygons: function(kmlXml) {
var polygons = [];
 
_parseStyles(kmlXml);
 
$(kmlXml).find('Placemark').each(function(index, placemarkXml) {
polygons.push(_parsePolygons(placemarkXml));
});
 
return polygons;
}
};
 
return _public;
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.