简体   繁体   English

使用可公开访问的网址时,KML文件在javascript Googlemaps中不起作用

[英]KML file not working in javascript Googlemaps when using publicly accessable URL

I am trying to implement a kml file using javascript API but it is not working. 我正在尝试使用JavaScript API实现kml文件,但无法正常工作。 The kml file is working when I use Google Earth. 当我使用Google Earth时,kml文件正在工作。 I don't seem to know where the problem is. 我似乎不知道问题出在哪里。 All the other markers,Polygons etc are working. 所有其他标记,多边形等都在工作。 I tried running the code on my domain and it is working without any issues there. 我尝试在我的域上运行代码,并且该代码没有任何问题。 But the problems arises when I am trying to run the html file locally and kml file is in my url. 但是,当我尝试在本地运行html文件且kml文件位于我的url中时,就会出现问题。 I want it to run locally. 我希望它在本地运行。 please help with that. 请帮忙。

The working url is click here to see the working code 工作网址是单击此处以查看工作代码

my code is as follows 我的代码如下

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
html {height:100%}
body {height:100%;margin:0;padding:0}
#googleMap {height:100%}
</style>
<script src="http://maps.googleapis.com/maps/api/js?key={API_Key}&sensor=false">
</script>

<script>

function initialize()
{
var mapProp = {
  center:new google.maps.LatLng(49.4076800,8.6907900),
  zoom:11,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };
var map=new google.maps.Map(document.getElementById("googleMap")
  ,mapProp);

var ctaLayer = new google.maps.KmlLayer({
    url: 'http://phanishashank.com/test.kml'
  });
  ctaLayer.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" ></div>

</body>
</html>

The data in my kml file is as follows. 我的kml文件中的数据如下。

<kml xmlns="http://earth.google.com/kml/2.1">
  <Document>
    <name>Test Path</name>
    <description>map in Heidelberg</description>

    <Style id="blueLine">
      <LineStyle>
        <color>ffff0000</color>
        <width>4</width>
      </LineStyle>
    </Style>


    <Placemark>
      <name>Blue Line</name>
      <styleUrl>#blueLine</styleUrl>
      <LineString>
        <altitudeMode>relative</altitudeMode>
        <coordinates>
    8.645272,49.41662700000001,0
    8.693593,49.408993,0
    8.645667,49.416447,0
    8.645178,49.416057,0
    8.64531,49.415982,0
    8.642668000000001,49.413872,0
    8.641953000000001,49.41256,0
    8.645405,49.41147599999999,0
    8.647867,49.412848,0
    8.667192999999999,49.408138,0
    8.669862,49.409094,0
    8.690248,49.410926,0
    8.691844,49.411231,0
        </coordinates>
      </LineString>
    </Placemark>



  </Document>
</kml>

Any suggestions on what may be the problem? 关于什么可能有什么建议?

The kml loads fine. kml加载正常。 I believe your problem is a missing comma in the mapProp definition 我相信您的问题是mapProp定义中缺少逗号

instead of 代替

var mapProp = {
  center:new google.maps.LatLng(49.4076800,8.6907900),
  zoom:11
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

it should be 它应该是

var mapProp = {
  center:new google.maps.LatLng(49.4076800,8.6907900),
  zoom:11,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

always pay attention to the console ;) 始终注意控制台;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM