read out KML FIle with Javascript
read out KML FIle with Javascript
我有一个包含城市各区的KML文件,并希望使用Javascript读取该文件,以便在地图上显示这些套印格式(多边形)(Google Maps API v.3)。此外,我希望保存KML文件中的地理点和对象中各区的名称。但我不知道该怎么做。谁能帮我解决这个问题吗。感谢
这里是紧凑的KML解析器代码。这仅适用于google.maps标记和多边形。
html
<input type='file' accept=".kml,.kmz" onchange="fileChanged()">
脚本,我使用了typescript,但它与javascript 非常相同
file: any
fileChanged(e) {
this.file = e.target.files[0]
this.parseDocument(this.file)
}
parseDocument(file) {
let fileReader = new FileReader()
fileReader.onload = async (e: any) => {
let result = await this.extractGoogleCoords(e.target.result)
//Do something with result object here
console.log(result)
}
fileReader.readAsText(file)
}
async extractGoogleCoords(plainText) {
let parser = new DOMParser()
let xmlDoc = parser.parseFromString(plainText, "text/xml")
let googlePolygons = []
let googleMarkers = []
if (xmlDoc.documentElement.nodeName == "kml") {
for (const item of xmlDoc.getElementsByTagName('Placemark') as any) {
let placeMarkName = item.getElementsByTagName('name')[0].childNodes[0].nodeValue.trim()
let polygons = item.getElementsByTagName('Polygon')
let markers = item.getElementsByTagName('Point')
/** POLYGONS PARSE **/
for (const polygon of polygons) {
let coords = polygon.getElementsByTagName('coordinates')[0].childNodes[0].nodeValue.trim()
let points = coords.split(" ")
let googlePolygonsPaths = []
for (const point of points) {
let coord = point.split(",")
googlePolygonsPaths.push({ lat: +coord[1], lng: +coord[0] })
}
googlePolygons.push(googlePolygonsPaths)
}
/** MARKER PARSE **/
for (const marker of markers) {
var coords = marker.getElementsByTagName('coordinates')[0].childNodes[0].nodeValue.trim()
let coord = coords.split(",")
googleMarkers.push({ lat: +coord[1], lng: +coord[0] })
}
}
} else {
throw "error while parsing"
}
return { markers: googleMarkers, polygons: googlePolygons }
}
输出
markers: Array(3)
0: {lat: 37.42390182131783, lng: -122.0914977709329}
...
polygons: Array(1)
0: Array(88)
0: {lat: -37.79825999283025, lng: 144.9165994157198}
...
有两种方式可以将KML文件提供给Javascript。
1) 用户上传KML文件。在这种情况下,您可以使用JS的File和FileReader API。它仅在HTML5中可用。下面是一个在HTML5中读取文件的例子。
http://www.html5rocks.com/en/tutorials/file/dndfiles/
2) 如果KML文件在您端或任何其他第三方服务器上。使用Ajax从该服务器获取文件并读入JS代码。只需将此文件作为XML读取即可。
var xmlDoc = new DOMParser().parseFromString(ajaxResponse,'text/xml');
在这两种情况下,在阅读KML文档时。您可以将Geopoints对象创建为JSON。
根据我的理解,您正在寻找parser
来解析Google API 3返回的KML
响应。
如果是这样,请查看专门针对Google Maps Javascript API版本3的kmlmapparser。
从文档来看,它似乎是受以下启发的原始代码:
- Lance Dyas geoxml项目位于:http://code.google.com/p/geoxml/(v3版本)
- Sterling Udell geoxml3项目位于:http://code.google.com/p/geoxml3/
所以你也可以试试这个。
希望你能理解。
相关文章:
- Angular 2.0 with JavaScript or TypeScript?
- errors with Javascript try catch
- Laravel 4 render() PDF with Javascript
- Trouble overlaying one div with another with JavaScript &
- Replace a "href" element with javascript
- geolocation with javascript and ruby on rails
- Rails 4 jQuery conflicts with javascript
- PHP Error with Javascript
- listen to 'CTRL+N' with javascript
- Dissect an id with javascript?
- change jquery masonry css with javascript
- JQuery Mobile Styling With JavaScript
- Missing ) with JavaScript Code
- RadioButton with Javascript (MVC3)
- Perl with Javascript (Perlscript)
- Image Rollovers with Javascript
- javascript innerhtml with javascript variables
- AngularJS conflicts with JavaScript minifier
- sqlite with javascript with phonegap using build.phonegap.co
- onClientClick with JavaScript function and eval