OpenLayers-未捕获的TypeError:无法读取未定义的属性"div" [英] OpenLayers - Uncaught TypeError: Cannot read property 'div' of undefined

查看:199
本文介绍了OpenLayers-未捕获的TypeError:无法读取未定义的属性"div"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to display markers on vector layer using following code,

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上看到地图,但是缺少标记.在控制台中,我发现以下错误消息,

I'm able to see map on respective div, but missing markers. In console I found following error message,

Uncaught TypeError: Cannot read property 'div' of undefined

我在哪里弄错了?我是OpenLayers的新手,如果我犯了任何愚蠢的错误并且我不想使用群集策略,我深表歉意,是否必须使用此策略,否则,我该如何跳过群集策略. ?

Where I did mistake ? I'm new to OpenLayers, I apologies if I made any silly mistake and I don't want to use cluster strategy, is it mandatory to use this, if not how can I skip cluster strategy. ?

推荐答案

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

addFeatures modifies the layer itself, does not return a new layer apparently

这有效:

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/

这篇关于OpenLayers-未捕获的TypeError:无法读取未定义的属性"div"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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