使用XMLHttpRequest加载KML谷歌地图,不能加载KML
Using XMLHttpRequest to load KML google maps, cannot load KML
我必须使用XMLHttpRequest来抓取kml文件,因为我不能直接对kml进行更改,并且需要绘制具有自己单独的信息窗口的多边形,这些信息窗口带有存储在kml中的详细信息,但不是作为描述标记或类似的东西,因此我不能轻松抓取它。我设法做到了这一点,多边形显示和信息窗口工作。这是一个相当大的程序,所以我没有在这里显示它,但基本上当我加载我的getKML函数时,它不会在开发环境中工作或出现问题。而它在我的本地主机上运行得很好。
这是我一直得到的错误信息:未捕获的网络错误:未能在'XMLHttpRequest'上执行'send':未能加载'https://someURL/polygons_live.kml'.
下面是代码,你真的只需要看前几行,因为这是使用xmlhttprequest的地方,也请原谅我的混乱代码,仍然是实习生和重构: /** Calls using xmlhttprequest to grab the kml file
* and later prints it out as one or more polygons
*/
function getKML(kmlUrl) {
var xmlRequest = new XMLHttpRequest();
xmlRequest.open("GET", kmlUrl, false);
xmlRequest.send();
xmlDoc = xmlRequest.responseXML;
var x = xmlDoc.getElementsByTagName("Placemark");
// travels through each Placemark tag in the kml files
var outage_time, restoration_time, event_number_value, fillColour, borderColour;
var objArray = [];
for (var i = 0; i < x.length; i++) {
// uses momentjs api to create human readable dates
var date_format = "MMM DD, hh:mm a";
// gets the event number
event_number_value = x[i].getElementsByTagName("SimpleData")[2].childNodes[0].nodeValue;
// gets outage start time
var outage_time_value = x[i].getElementsByTagName("SimpleData")[3].childNodes[0].nodeValue;
var outage_time_moment = moment(outage_time_value);
outage_time = outage_time_moment.format(date_format);
// gets estimated restoration time
var restoration_time_value = x[i].getElementsByTagName("SimpleData")[5].childNodes[0].nodeValue;
// checks to see if we have a restoration time or not
if (restoration_time_value === "2001-01-01T00:00:00") {
restoration_time = "Not yet determined";
} else {
var restoration_time_moment = moment(restoration_time_value);
restoration_time = restoration_time_moment.format(date_format);
}
// gets the coordinates of the polygon
var coords = x[i].getElementsByTagName("coordinates")[0].childNodes[0].nodeValue;
var coordinate = coords.split(",0 ");
var coordJoined = coordinate.join();
var coordAgain = coordJoined.split(",");
// gets the colour of the polygon
var colour = x[i].getElementsByTagName("styleUrl")[0].childNodes[0].nodeValue;
// determines the colour out of yellow, orange and red
if (colour === "#Style1") {
fillColour = '#f1c40f';
borderColour = '#f1c40f';
} else if (colour === "#Style2") {
fillColour = '#e67e22';
borderColour = '#e67e22';
} else {
fillColour = '#c0392b';
borderColour = '#c0392b';
}
// creates objects and adds it to array to be later used as data
var obj = {
eventID : event_number_value,
offTime : outage_time,
restoreTime : restoration_time,
fill : fillColour,
borderCol : borderColour
};
objArray.push(obj);
// create a LatLng array out of the coordinate string
var polygonCoords = new Array();
var j = 0;
var z = j + 1;
//var firstCoord = new google.maps.LatLng();
while (z < coordAgain.length) {
// adds the first and last latLng to the array of polygonCoords
if ((j % 2) == 0) {
var co1 = parseFloat(coordAgain[z]);
var co2 = parseFloat(coordAgain[j]);
var newLatLng = new google.maps.LatLng(co1, co2);
polygonCoords.push(newLatLng);
} else {
var co1 = parseFloat(coordAgain[j]);
var co2 = parseFloat(coordAgain[z]);
var newLatLng = new google.maps.LatLng(co1, co2);
polygonCoords.push(newLatLng);
}
j++;
z++;
}
//removes last coordinate as its useless as its not a number
polygonCoords.pop();
/** Adds the polygon to a polygon array
* and maps it onto the map
*/
var newPoly = new google.maps.Polygon({
paths : polygonCoords,
strokeColor : objArray[i].borderCol,
strokeOpacity : 0.35,
strokeWeight : 2,
fillColor : objArray[i].fill,
fillOpacity : 0.35
})
newPoly.setMap(map);
newPoly.set("eventNum", objArray[i].eventID);
newPoly.set("offTime", objArray[i].offTime);
newPoly.set("resTime", objArray[i].restoreTime);
google.maps.event.addListener(newPoly, 'click',
attachInfoWindow(newPoly));
polyArray.push(newPoly);
}
}
更新1:我实际上发现这个错误后来出现在我的控制台:XMLHttpRequest无法加载https://someurl/polygons_live.kml。请求的资源上没有'Access-Control-Allow-Origin'标头。因此不允许访问Origin 'https://someurl'。
这是一个跨域请求问题,我将开始使用相对地址来指向抓取我的kml.
我的问题解决了
相关文章:
- Kml层没有'重新加载页面时不会更新
- Kml层在刷新后工作正常,但给定typeError:无法读取属性getMap.第一次加载时
- 如何在谷歌地图中加载KML,而不更改地图中心
- “加载 KML”复选框
- KML 在加载后覆盖默认视图
- 谷歌地图 KML 加载 - 414 请求 URI 太大
- 无法在谷歌地图上加载 KML 图层
- 为什么 KML 无法在谷歌地图中加载
- 谷歌地图不会加载/显示KML文件
- 如何将KML文件动态加载到谷歌地图,并进一步加载到折线
- kml文件在谷歌地图上加载了一半
- KML层未在谷歌地图上加载
- 谷歌地图-加载KML覆盖
- 无法在开始或位置搜索时加载KML文件以在自定义地图中工作
- 无法使用geoxml3加载KML文件
- 需要帮助加载KML -这是我的第一个谷歌地图项目
- 向加载的KML中的标记添加单击事件
- 使用geokml3在谷歌地图上加载KML层
- 使用XMLHttpRequest加载KML谷歌地图,不能加载KML
- 任何方式,我可以加载KML数据从谷歌地图(再次)通过JS API