"Object has no method 'getBounds'" error in OpenLayers
I'm using this code to draw a point on a map:
function addPointToMap(pMap){
var coordinates = new Array();
// Style Point
var style_blue = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
style_b开发者_如何转开发lue.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);
}
I'm getting this error from the console:
Uncaught TypeError: Object POINT(33, 33) has no method 'getBounds'
What am I doing wrong?
For the sake of completeness, I received a similar error while adding a polygon (not a point) from raw WKT data. The error that there are no bounds occur because the object was of the wrong type.
When you call addFeatures
, it expects an array of OpenLayers.Feature.Vector
objects, which are created by 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]);
The answer is to add a multipoint geometry:
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);
}
Does your map have a 'baselayer'? Otherwise you should add option 'alloverlays'=true to your map options. I'm not sure if this solves your problem though...
精彩评论