OpenLayers - 未捕获TypeError:无法读取未定义的属性'div'

时间:2013-12-20 07:06:29

标签: openlayers

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

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 个答案:

答案 0 :(得分: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/