开发者问题收集

OpenLayers-未捕获的类型错误:无法读取未定义的属性“div”

2013-12-20
6420

我尝试使用以下代码在矢量图层上显示标记,

var map;
function init(){
   map = new OpenLayers.Map('map_element',{});
   var wms = new OpenLayers.Layer.WMS(
       'OpenLayers WMS',
       'http://vmap0.tiles.osgeo.org/wms/vmap0',
               {layers: 'basic'},
       {}
       );
       map.addLayer(wms);
       if (!map.getCenter())
       {
           map.zoomToMaxExtent();
       }
       var feature_data = {
           "type": "FeatureCollection",
           "features": [
               {"type": "Feature", "properties": {},
                   "geometry": {"type": "Point", "coordinates": [-81, 42]}},
               {"type": "Feature", "properties": {},
                   "geometry": {"type": "Point", "coordinates": [-82, 43]}},
               {"type": "Feature", "properties": {},
                   "geometry": {"type": "Point", "coordinates": [-80, 41]}},
               {"type": "Feature", "properties": {},
                   "geometry": {"type": "Point", "coordinates": [19, -24]}},
               {"type": "Feature", "properties": {},
                   "geometry": {"type": "Point", "coordinates": [4, 42]}},
               {"type": "Feature", "properties": {},
                   "geometry": {"type": "Point", "coordinates": [32, 35]}},
           ]
       }
       var format_geojson = new OpenLayers.Format.GeoJSON({});
       var vector_strategies = [new OpenLayers.Strategy.Cluster({distance: 42})];
       vector_layer = new OpenLayers.Layer.Vector('More Advanced VectorLayer', {strategies: vector_strategies});
       var fLayer = vector_layer.addFeatures(format_geojson.read(feature_data));
       map.addLayer(fLayer);
   }

我能够在相应的 div 上看到地图,但缺少标记。在控制台中,我发现以下错误消息,

Uncaught TypeError: Cannot read property 'div' of undefined

我哪里犯了错误?我是 OpenLayers 的新手,如果我犯了任何愚蠢的错误,我深表歉意,我不想使用集群策略,是否必须使用这个,如果不是,我如何跳过集群策略。?

1个回答

addFeatures 修改图层本身,显然不返回新图层

这有效:

vector_layer.addFeatures(format_geojson.read(feature_data));
map.addLayer(vector_layer);

代替

var fLayer = vector_layer.addFeatures(format_geojson.read(feature_data));
map.addLayer(fLayer);

示例: http://jsfiddle.net/G8k2H/

user2314737
2013-12-20