ol3中的geom、feature、layer、地图的关系及操作

废话不多说,直接上代码吧。

//点

var pointFeature = new ol.Feature(new ol.geom.Point([0, 0]));

//线

var lineFeature = new ol.Feature(

new ol.geom.LineString([[-1e7, 1e6], [-1e6, 3e6]]));

//面

var polygonFeature = new ol.Feature(

new ol.geom.Polygon([[[-3e6, -1e6], [-3e6, 1e6],

[-1e6, 1e6], [-1e6, -1e6], [-3e6, -1e6]]]));

//也可以这样创建feature

//var geom=new ol.geom.Polygon([坐标数组]);

//var polygonFeature=new Feature({

// geomtery:geom

//})

//图层资源

var source=new ol.source.Vector({

features: [pointFeature, lineFeature, polygonFeature]

})

//也可以这样加载feature

//source.addFeaute(polygonFeature);

//图层

var layer=new ol.layer.Vector({

source:source

,style: new ol.style.Style({

image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({

anchor: [0.5, 46],

anchorXUnits: 'fraction',

anchorYUnits: 'pixels',

opacity: 0.95,

src: 'https://openlayers.org/en/v3.19.1/examples/data/icon.png'

})),

stroke: new ol.style.Stroke({

width: 3,

color: [255, 0, 0, 1]

}),

fill: new ol.style.Fill({

color: [0, 0, 255, 0.6]

})

})

})

//也可以这样添加sourc

//map.addSource(source);

//地图

var map = new ol.Map({

layers: [

new ol.layer.Tile({

source: new ol.source.TileJSON({

url: 'https://api.tiles.mapbox.com/v3/mapbox.geography-class.json?secure'

})

})

,layer

],

target: 'map',

view: new ol.View({

center: [0, 0],

zoom: 2

})

});

//也可以这样加图层

//map.addLayer(layer);

//跳转到定区域

map.getView().fit(polygonFeature.getGeometry(),map.getSize());

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 题记:站在时光的此岸,遥念彼岸的光阴,以诗为鉴,以词为盟,以星为证,以风为谋,轻盈出一片嫣红。为你,低吟浅唱那一缕...
    Dolfly阅读 5,186评论 0 0
  • 1、张昭字子布,彭城人也。少好学,善隶书,从白侯子安受《左氏春秋》,博览众书,与琅邪赵昱、东海王朗俱发名友善。弱冠...
    长衫赵紫龙阅读 2,817评论 0 0
  • 1. 小艺今天又去相亲了。据我知,这已经是第44次了。 小艺不敢回家,手机也不敢开机,连上班都不敢去了。 她来找我...
    石头喃喃阅读 11,295评论 1 8
  • 如果发现自己最感兴趣的东西 却是自己最缺乏的地方时,会是怎样一种体会? 当自认为有很多感兴趣的事情,每天安排不过来...
    小维克克阅读 1,079评论 0 0