2012-04-16 9 views
3

私はいくつかのマーカーでKMLベクターレイヤーを表示するマップを持っています。マーカーをクリックすると、情報ボックスがポップアップします。私は自動的にページに渡されたパラメータに基づいてインフォボックスをポップアップできるようにしたい。 私は、これを行うにはgetFeaturesByAttribute()を使って名前を見つける必要があると思いますが、フィーチャ配列は常に空であるようです。 (FireBugを使用すると内容がわかりますが)OpenLayers features配列が空です

アレイ内のアイテムを取得するには何が必要ですか?

コード:HTTP呼び出しは、層の機能を取り込むために非同期的に起こっているので

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 

    } 

答えて

2

この問題は、私が思うに、です。したがって、HTTP呼び出しが返される前にアラート(groups.features.length)が表示されるため、レイヤーにはフィーチャがなく、groups.features.lengthは正しく0になります。この方法でフィーチャの数を確認するには、 HTTPイベントが返った後に呼び出すlayer loadend eventに接続し、すべての機能を調べることができます。

+0

ありがとうございました。 – Paul

関連する問題