将KML图层添加到Google地图API 3 [英] Adding KML layer to google map API 3

查看:133
本文介绍了将KML图层添加到Google地图API 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户从表单中选择一些内容并点击提交时,它会将所有内容添加到Google地图中。添加标记可以很好地工作,并添加一个监听事件来添加多义线也可以。由于某些原因,KML文件无法正确覆盖。我不确定我是否正确地做到了。来自窗体的值确实回显出来,所以我知道数据至少到达了地图函数。这是不工作的代码:

  window.onload = function(){
//创建一个对象包含属性的文字
//我们要传递给地图
var options = {
zoom:5,
center:new google.maps.LatLng(39.09,-95.71 ),
mapTypeId:google.maps.MapTypeId.ROADMAP
};
//创建地图
var map = new google.maps.Map(document.getElementById('map'),options);
//创建一个LatLngBounds对象
var bounds = new google.maps.LatLngBounds();

//创建一个空的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);

这里是设置kmlFiles变量的代码:

  kmlFiles = new google.maps.KmlLayer(<?php echo $ _POST ['kmlFile'];?>); 


解决方案

我怀疑你缺少URL引号例如

var layer = new google.maps.KmlLayer(http://www.site.com/foo.kml);



无效,而

var layer = new google.maps.KmlLayer(http://www.site.com/foo .kml);

有效。


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.

这篇关于将KML图层添加到Google地图API 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆