当我在var view
之后添加map.on("singleclick", function(event)
时,我注意到方法getView()
(http://openlayers.org/en/master/apidoc/ol.Map.html#getView)停止工作。任何人都可以给我一个提示,为什么会这样?在我的浏览器控制台中,我收到以下错误:
TypeError:map未定义
这是我的代码:
map.on("singleclick", function(event) {
var map = evt.map;
var feature = map.forEachFeatureAtPixel(
event.pixel, function(feature, layer) {
return feature
}
)
var icon_feature = new ol.Feature({
geometry: new ol.geom.Point(event.coordinate),
type: 'icon',
coords: event.coordinate
})
icon_feature.setStyle(icon_style)
icon_layer.getSource().addFeature(icon_feature)
})
var view = map.getView(); //error in this line
//more code here ...
答案 0 :(得分:1)
请勿重新定义map
(var map = evt.map;
)变量!只需使用:
var view = map.getView();