使用Google KML在基于网络的Google地图上成功绘制路线/线路

时间:2011-09-12 20:54:48

标签: javascript google-maps google-maps-api-3

我希望能够在基于网络的Google地图上绘制路线。我花了很多时间查看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>

尽管如此,我还是无法在Google地图中绘制第二行。当我在Google地球中加载KML时,它没有加载任何问题。我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

var p = new google.maps.Polyline({ 
   map: m.map, 
   path: [hkg, end], 
   strokeColor: color, 
   geodesic: m.geodesic 
});

确保在设置地图时,geodesic设置为true:

var maps = {
            mymap: { 
                map: null, 
                geodesic: true, 
                overlay: { path: null} }
 };

将lat long设置为google lat longs:

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

这是一种享受:)