“对象在OpenLayers中没有方法'getBounds'”错误

时间:2011-07-21 10:38:08

标签: javascript openlayers

我正在使用此代码在地图上绘制一个点:

function addPointToMap(pMap){
    var coordinates = new Array();

    // Style Point
    var style_blue = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
    style_blue.strokeColor = "blue"; 
    style_blue.fillColor = "blue"; 

    // Make Point
    coordinates.push(new OpenLayers.Geometry.Point(33, 33));
    var pointFeature =  new OpenLayers.Feature.Vector(coordinates, null, style_blue);

    // Layer
    var pointsLayer = new OpenLayers.Layer.Vector("Points Layer");
    pointsLayer.addFeatures([pointFeature]);
    pMap.addLayer(pointsLayer);         
}

我从控制台收到此错误:

Uncaught TypeError: Object POINT(33, 33) has no method 'getBounds'

我做错了什么?

3 个答案:

答案 0 :(得分:1)

答案是添加多点几何:

function addPointToMap(pMap){
            var coordinates = new Array();

            // Style Point
            var style_blue = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
            style_blue.strokeColor = "blue"; 
            style_blue.fillColor = "blue"; 

            // Make Point
            coordinates.push(new OpenLayers.Geometry.Point(lon, lat));
            var pointsGeometry = new OpenLayers.Geometry.MultiPoint(coordinates);
            var pointFeature =  new OpenLayers.Feature.Vector(pointsGeometry, null, style_blue);

            // Layer
            var pointsLayer = new OpenLayers.Layer.Vector("Points Layer");
            pointsLayer.addFeatures([pointFeature]);
            pMap.addLayer(pointsLayer);         
        }

答案 1 :(得分:1)

为了完整起见,我在从原始WKT数据添加多边形(不是点)时收到了类似的错误。出现没有边界的错误,因为对象的类型错误。

当您致电addFeatures时,它需要一组OpenLayers.Feature.Vector个对象,这些对象由Format.read创建。

var wkt_parser = new OpenLayers.Format.WKT();
var wkt_data_parsed = wkt_parser.read(some_raw_wkt_data_string);
layer.addFeatures([wkt_data_parsed]);

答案 2 :(得分:0)

你的地图有“基层”吗?否则,您应该在地图选项中添加选项'alloverlays'= true。我不确定这是否能解决你的问题......