【openlayers-5】地图点、线、面等要素添加

目录

1、添加点

2、添加线

 3、添加面

4、添加多边形

5、定位到某个点


1、通过样式信息和几何信息构建要素;

2、将要素添加到矢量数据源;

3、将矢量数据源添加到矢量图层;

4、添加矢量图层到地图容器。

1、添加点

//创建一个点
var point = new ol.Feature({
	geometry: new ol.geom.Point([117.2, 35.8] ),
  })
//设置点的样式信息
point.setStyle(
  new ol.style.Style({
	//填充色
	fill: new ol.style.Fill({
	  color: 'rgba(255, 255, 255, 0.2)',
	}),
	//边线颜色
	stroke: new ol.style.Stroke({
	  color: '#ffcc33',
	  width: 2,
	}),
	//形状
	image: new ol.style.Circle({
	  radius: 17,
	  fill: new ol.style.Fill({
		color: '#ffcc33',
	  }),
	}),
  })
)

// 需要一个vector的layer来放置点
var layer = new ol.layer.Vector({
  source: new ol.source.Vector()
})


var url = 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}';

var view = new ol.View({
	center: [117.23, 36.43],
	zoom: 10,
	projection: 'EPSG:4326'//设置坐标系,地图本身3857坐标系,即横轴墨卡托投影,因为中心点用的度,所以设置成wgs84代号是4326
  });
var map = new ol.Map({
  // 设置地图图层
  layers: [
	new ol.layer.Tile({ source: new ol.source.XYZ({ url: url }) }),
	layer
  ],
  // 设置显示地图的视图
  view: view,
  // 让id为map的div作为地图的容器
  target: 'mapCon',
});

layer.getSource().addFeature(point);

2、添加线

//创建一条线
var Line = new ol.Feature({
	geometry: new ol.geom.LineString([[117.2, 35.8], [117.48, 36.8]]),
})
//设置点的样式信息
Line.setStyle(
  new ol.style.Style({
	//填充色
	fill: new ol.style.Fill({
	  color: 'rgba(255, 255, 255, 0.2)',
	}),
	//边线颜色
	stroke: new ol.style.Stroke({
	  color: '#ffcc33',
	  width: 2,
	}),
	//形状
	image: new ol.style.Circle({
	  radius: 17,
	  fill: new ol.style.Fill({
		color: '#ffcc33',
	  }),
	}),
  })
)

。。。

layer.getSource().addFeature(Line);

 3、添加面

var Rectangle = new ol.Feature({
    geometry: new ol.geom.Polygon.fromExtent([117.2, 35.8, 117.48, 36.8])
});

Rectangle.setStyle(new ol.style.Style({
    fill: new ol.style.Fill({
        color: 'rgba(123, 237, 159,0.5)'
    }),
    stroke: new ol.style.Stroke({
        color: '#4a8fff',
        width: 4
    }),
    image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
            color: '#4a8fff'
        })
    })
}));

。。。

layer.getSource().addFeature(Rectangle);

4、添加多边形

var Rectangle = new ol.Feature({
    geometry: new ol.geom.Polygon([[[117.56,35.32], [118.25, 36.23], [117.82, 37.25]]])
});

参考链接  openlayers-06-坐标添加点、线、面_openlayers 添加点-CSDN博客

5、定位到某个点

map.getView().animate({
	center: [117.403, 42.924],
	duration: 1000,
	zoom: 12,
 })
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 OpenLayers 绘制要素,你需要先创建一个地图实例,然后创建一个矢量图层,并在该图层上添加要素。 首先,你需要在 HTML 页面中引入 OpenLayers 库文件。可以在 head 标签中添加以下代码: ```html <head> <!-- 引入 OpenLayers 库 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.css"> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js"></script> </head> ``` 接下来,在 body 标签中创建一个具有固定大小的容器来显示地图: ```html <body> <div id="map" style="width: 100%; height: 400px;"></div> </body> ``` 然后,在 JavaScript 中创建地图实例和矢量图层,并添加要素: ```javascript // 创建地图实例 var map = new ol.Map({ target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); // 创建矢量图层 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); // 创建要素 var point = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([longitude, latitude])) }); // 添加要素到矢量图层 vectorLayer.getSource().addFeature(point); // 将矢量图层添加地图 map.addLayer(vectorLayer); ``` 在上面的代码中,`longitude` 和 `latitude` 是要素的经度和纬度值,你需要根据实际情况进行设置。 这样就可以使用 OpenLayers 绘制要素了。记得根据实际需求调整地图的中心和缩放级别,以及要素的样式等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值