OpenLayers功能数组为空 [英] OpenLayers features array is empty

查看:64
本文介绍了OpenLayers功能数组为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张地图,其中显示了带有某些标记的KML矢量图层.当您单击标记时,它会弹出一个信息框.我希望能够根据传递给页面的参数自动弹出信息框. 我想我需要使用getFeaturesByAttribute()来找到名称,但是features数组似乎总是空的. (尽管使用FireBug可以看到内容)

I've got a map that displays a KML Vector layer with some markers. When you click on the marker it pops up an info box. I want to be able to automatically pop up an info box based on a parameter passed into the page. I think I need to do this by using getFeaturesByAttribute() to find a name, however the features array always seems to be empty. (Though I can see the contents when I use FireBug)

我需要怎么做才能在阵列中获取物品?

What do I need to do to get items in the array?

代码:

function init()
{
        var options = {
            projection: new OpenLayers.Projection("EPSG:900913"),
            displayProjection: new OpenLayers.Projection("EPSG:4326"),
            units: "m",
        };
        map = new OpenLayers.Map('map', options);
        var mapnik = new OpenLayers.Layer.OSM("OpenStreetMap");
        var gmap = new OpenLayers.Layer.Google("Google", {sphericalMercator:true});
        var gsat = new OpenLayers.Layer.Google(
            "Google Satellite",
            {type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22}
        );

        groups = new OpenLayers.Layer.Vector("Groups", {
            projection: map.displayProjection,
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "http://maps.google.co.uk/maps/ms?msa=0&msid=210450558816094618535.0004bd79ceb30e9acb9da&output=kml",
                format: new OpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true
                })
            })
        });

        map.addLayers([mapnik, gmap, gsat, groups]);

        select = new OpenLayers.Control.SelectFeature(groups);

        groups.events.on({
            "featureselected": onFeatureSelect,
            "featureunselected": onFeatureUnselect
        });

        map.addControl(select);
        select.activate();   

        map.addControl(new OpenLayers.Control.LayerSwitcher());

        var center = new OpenLayers.LonLat(-2.58789,51.52283).transform(map.displayProjection, map.projection);
        var zoom = 12
        map.setCenter(center, zoom);

        alert(groups.features.length);   // is always 0

    }

推荐答案

我认为,此问题是因为HTTP调用是异步发生的,以填充该图层的功能.因此,您要在HTTP调用返回之前命中alert(groups.features.length),因此该图层没有功能,因此groups.features.length正确为0.如果要以这种方式查看功能数量,则需要将函数附加到图层加载结束事件它将在HTTP事件返回后调用,您将可以查询所有功能.

This issue, I think, is because the HTTP call is happening asynchronously to populate the features of the layer. You are therefore hitting alert(groups.features.length) before the HTTP call has returned and hence the layer has no features so groups.features.length is rightly 0. If you want to see the number of features in this manner you need to attach function to the layer loadend event which will call after the HTTP event has returned and you will be able to interrogate all the features.

这篇关于OpenLayers功能数组为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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