Adding KML layer to google map API 3
I am trying to make it so that when the user selects some things from a form and hits submit it will add all the stuff to the google map. Adding markers works perfectly fine and add a listening event to add polylines works fine also. For some reason the KML file will not overlay correctly. I am not sure I am doing it correctly though. The value from the form does echo out correctly so I know the data is reaching at least to the map function. Here is the code that is not working:
window.onload = function() {
// Creating an object literal containing the properties
// we want to pass to the map
var options = {
zoom: 5,
center: new google.maps.LatLng(39.09, -95.71),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Creating the map
var map = new google.maps.Map(document.getElementById('map'), options);
// Creating a LatLngBounds object
var bounds = new google.maps.LatLngBounds();
// Creating an empty MVCArray
var route = new google.maps.MVCArray();
var polyline = new google.maps.Polyline({
path: route,
strokeColor: "#ff0000",
strokeOpacity: 0.6,
开发者_如何学编程 strokeWeight: 5
});
polyline.setMap(map);
google.maps.event.addListener(map, 'click', function(e) {
var path = polyline.getPath();
path.push(e.latLng);
});
kmlFiles.setMap(map);
here is the code that sets the kmlFiles variable:
kmlFiles = new google.maps.KmlLayer(<?php echo $_POST['kmlFile']; ?>);
I suspect you are missing quotes around the URL that you are passing to KMLLayer e.g.
var layer = new google.maps.KmlLayer(http://www.site.com/foo.kml);
is invalid while
var layer = new google.maps.KmlLayer("http://www.site.com/foo.kml");
is valid.
精彩评论