Javascript 使用Google KML在基于web的Google地图上成功绘制路线/线条

Javascript 使用Google KML在基于web的Google地图上成功绘制路线/线条,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我希望能够在基于网络的谷歌地图上绘制路线。我花了很多时间研究Google Maps API,并编写了以下Javascript: $(document).ready(function () { $(function() { loadScript(); }); }); function initialize() { var myOptions = { zoom: 10, disa

我希望能够在基于网络的谷歌地图上绘制路线。我花了很多时间研究Google Maps API,并编写了以下Javascript:

$(document).ready(function () {
            $(function() {
                loadScript();
            });
});

function initialize() {
    var myOptions = {
        zoom: 10,
        disableDefaultUI: true,
        center: new google.maps.LatLng(51.5001524, -0.1262362),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById('fullscreen'), myOptions);

    var ck = Math.floor(Math.random() * 1000);
    var ctaLayer = new google.maps.KmlLayer('http://xxx/storage/kml/test.kml');
    ctaLayer.setMap(map);

}

function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize';
    document.body.appendChild(script);
}
我的
.kml
文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Style id="blueLine">
      <LineStyle>
        <color>ffff0000</color>
        <width>4</width>
      </LineStyle>
    </Style>
    <Style id="pinkLine">
      <LineStyle>
        <color>ffff33ff</color>
        <width>4</width>
      </LineStyle>
    </Style>
    <Placemark>
      <name>LHR-BSS</name>
      <visibility>1</visibility>
      <styleUrl>#blueLine</styleUrl>
      <description><![CDATA[LHR-BSS]]></description>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <coordinates>
          -0.4534243,51.4703429,0
          4.4895353,50.9035504,0
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>

ffff0000
4.
ffff33ff
4.
LHR-BSS
1.
#蓝线
1.
1.
-0.4534243,51.4703429,0
4.4895353,50.9035504,0

尽管如此,我还是无法在谷歌地图中画出第二条线。当我在Google Earth中加载KML时,它不会加载任何问题。我做错了什么?

这比我想象的要容易得多。我不需要KML或KML层(因为缓存btw而臭名昭著)。我只是用了:

var p = new google.maps.Polyline({ 
   map: m.map, 
   path: [hkg, end], 
   strokeColor: color, 
   geodesic: m.geodesic 
});
确保在设置地图时,“测地线”设置为true:

var maps = {
            mymap: { 
                map: null, 
                geodesic: true, 
                overlay: { path: null} }
 };
将lat longs设置为google lat longs时:

var hkg = new google.maps.LatLng(22.3088856, 113.9141464); /*hong kong*/

这很有效:)

kml文件可以公开访问吗?可以。我把它放在我的博客上,并按它应该做的那样下载到谷歌地球上……我发现了一些非常类似于我想要实现的东西:。不是搜索,而是地图上绘制的线和标记。如果我能从1400万个表中挖掘出代码,我计划在那里删除代码(tsk-tsk-Google)。