I want to be able to draw routes on a web-based Google Map. I have spent quite a bit of time looking at the Google Maps API and I have written the following 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);
}
My .kml file looks like this:
<?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>
Yet despite this, I can’t get the second line to draw within Google Maps. When I load the KML within Google Earth, it loads no problem. What am I doing wrong?
This was far easier than I ever expected. I didn’t need the KML or the KML layer (which was notorious for caching btw). I simply used:
Making sure that when setting up the map, geodesic is set to true:
With the lat longs set as google lat longs:
This worked a treat 🙂