openlayers-06-坐标添加点、线、面

添加绘制层

//如果想画画,首先需要有一张纸或者画布,地图则需要一个绘制层
//创建一个矢量图层Vector 作为绘制层,ol.source.Vector是用来设置矢量图层数据来源的
var source = new ol.source.Vector();
//创建一个图层 ol.layer.Vector矢量图层类
var vector = new ol.layer.Vector({
    source: source
});
//将绘制层添加到地图容器中
map.addLayer(vector);		

添加点

//创建地图实例时,使用的是 EPSG:4326 投影坐标,以下的方法均采用经纬度坐标坐标点进行实现

//添加点
var point = new ol.Feature({
    geometry: new ol.geom.Point([118.26, 37.35])
});
//设置点1的样式信息(Feature对象 只能 通过setStyle方法设置style属性)
point.setStyle(new ol.style.Style({
    //填充色
    fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.2)'
    }),
    //边线颜色
    stroke: new ol.style.Stroke({
        color: '#4a8fff',
        width: 2
    }),
    //形状
    image: new ol.style.Circle({
        radius: 17,
        fill: new ol.style.Fill({
            color: '#4a8fff'
        })
    })
}));
//将创建的 点 元素 添加到图层
source.addFeature(point);

添加线

//添加线
//创建一个线
var Line = new ol.Feature({
    geometry: new ol.geom.LineString([[118.25,36.17], [113.56, 35.11]])
});

//设置线的样式
Line.setStyle(new ol.style.Style({
    //填充色
    fill: new ol.style.Fill({
        color: 'rgba(74, 143, 255, 0.2)'
    }),
    //边线颜色
    stroke: new ol.style.Stroke({
        color: '#4a8fff',
        width: 5
    }),
    //形状
    image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
            color: '#4a8fff'
        })
    })
}));
//将创建的 线 元素 添加到图层
source.addFeature(Line);

添加圆

//添加圆
//创建一个圆
var Circle = new ol.Feature({
    geometry: new ol.geom.Circle([116.53,34.23], 0.1)//0.01表示1公里
});

Circle.setStyle(new ol.style.Style({
    //填充色
    fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.5)'
    }),
    //边线颜色
    stroke: new ol.style.Stroke({
        color: '#4a8fff',
        width: 3
    }),
    //形状
    image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
            color: '#4a8fff'
        })
    })
}));
//将创建的 圆 元素 添加到图层
source.addFeature(Circle);

添加正方形

var CircleForSquare = new ol.geom.Circle([115.63,37.82], 0.1);//创建一个圆
//根据圆获取多边形

var Square = new ol.Feature({
    //fromCircle() 通过圆矢量特性创建一个多边形矢量特性,(圆矢量特性,多边形的边数(默认32),第一个顶点的多边形角弧度)
    geometry: new ol.geom.Polygon.fromCircle(CircleForSquare, 4, 150)
});

Square.setStyle(new ol.style.Style({
    //填充色
    fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.8)'
    }),
    //边线颜色
    stroke: new ol.style.Stroke({
        color: 'green',
        width: 2
    }),
    //形状
    image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
            color: '#4a8fff'
        })
    })
}));
//将创建的 正方形 元素 添加到图层
source.addFeature(Square);

添加矩形

//添加矩形
//根据范围获取多边形
var Rectangle = new ol.Feature({
    geometry: new ol.geom.Polygon.fromExtent([116.35,36.77,118.89,35.89])
});

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'
        })
    })
}));
//将创建的 矩形 元素 添加到图层
source.addFeature(Rectangle);

添加多边形

//创建一个多变形
var Polygon = new ol.Feature({
    geometry: new ol.geom.Polygon([[[117.56,35.32], [118.25, 36.23], [117.82, 37.25]]])
});
//设置区样式信息
Polygon.setStyle(new ol.style.Style({
    //填充色
    fill: new ol.style.Fill({
        color: 'rgba(237, 193, 166, 0.5)'
    }),
    //边线颜色
    stroke: new ol.style.Stroke({
        color: '#e54ced',
        width: 2
    }),
    //形状
    image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
            color: '#e54ced'
        })
    })
}));
//将创建的 多变形 元素 添加到图层
source.addFeature(Polygon);

示例代码

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: OpenLayers 5 是一个开源的 JavaScript 库,用于创建交互式地图应用。要实现在地图上绘制坐标之间的连线,可以按照以下步骤进行: 1. 首先,在 HTML 文件中导入 OpenLayers 库和样式文件: ```html <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" /> <script src="https://openlayers.org/en/v5.3.0/build/ol.js"></script> ``` 2. 创建一个地图容器的 div 元素: ```html <div id="map" style="width: 400px; height: 300px;"></div> ``` 3. 在 JavaScript 文件中,创建一个用于显示地图的 map 对象: ```javascript var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 2 }) }); ``` 4. 创建一个矢量图层和一个矢量源对象: ```javascript var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(vectorLayer); var vectorSource = vectorLayer.getSource(); ``` 5. 为地图添加单击事件,获取击位置的坐标: ```javascript map.on('click', function(event) { var coordinate = event.coordinate; // 在此处可以将坐标添加到 vectorSource 中 }); ``` 6. 在单击事件的回调函数中,创建一个要素并添加到矢量源对象中: ```javascript var pointFeature = new ol.Feature({ geometry: new ol.geom.Point(coordinate) }); vectorSource.addFeature(pointFeature); ``` 7. 如果要连接这些坐标,可以使用 ol.geom.LineString 创建一个线要素,然后添加到矢量源对象中: ```javascript var lineStringFeature = new ol.Feature({ geometry: new ol.geom.LineString(coordinates) }); vectorSource.addFeature(lineStringFeature); ``` 最后,你将在地图上看到所有的坐标以及它们之间的连线。这是使用 OpenLayers 5 实现坐标线的基本步骤。可以通过进一步自定义样式和处理事件来增加功能。 ### 回答2: OpenLayers 5是一个开源的JavaScript库,可用于在Web上实现地图功能。如果要在地图上绘制坐标之间的连线,可以按照以下步骤进行: 1. 初始化地图:首先,需要在页上创建一个地图容器,然后初始化地图对象。可以指定地图的中心坐标、缩放级别等属性。 2. 添加图层:接下来,创建一个矢量图层,并将其添加到地图中。这样可以在地图上绘制矢量要素,如线等。 3. 创建要素:使用OpenLayers的Point类,可以创建一个要素,其中包含要绘制的坐标的经纬度信息。 4. 创建线要素:使用OpenLayers的LineString类,可以创建一个线要素,其中包含要绘制的坐标序列。 5. 绘制要素:将要素添加到图层中,再将线要素添加到图层中。这样,地图上就可见了这些要素。 以下是一个示例代码片段,展示了如何在OpenLayers 5中实现坐标线的功能: ```javascript // 初始化地图 var map = new ol.Map({ target: 'map-container', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 10 }) }); // 创建矢量图层 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(vectorLayer); // 创建要素 var point1 = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([lon1, lat1])) }); var point2 = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([lon2, lat2])) }); // 创建线要素 var lineFeature = new ol.Feature({ geometry: new ol.geom.LineString([ ol.proj.fromLonLat([lon1, lat1]), ol.proj.fromLonLat([lon2, lat2]) ]) }); // 添加要素到图层 vectorLayer.getSource().addFeature(point1); vectorLayer.getSource().addFeature(point2); vectorLayer.getSource().addFeature(lineFeature); ``` 以上代码中的`map-container`是在HTML页上用来容纳地图的一个元素,`lon1`、`lat1`、`lon2`和`lat2`是两个坐标的经纬度值。通过将要素和线要素添加到矢量图层中,即可在地图上看到这些要素,实现坐标的划线功能。 ### 回答3: 使用OpenLayers 5实现坐标线的方法如下: 首先,创建一个地图容器,可以使用HTML中的一个div元素作为容器: ```html <div id="map" style="width: 100%; height: 500px;"></div> ``` 然后,在JavaScript中,创建一个OpenLayers地图实例并将其显示在地图容器中: ```javascript var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 4 }) }); ``` 接下来,创建一个用于存储坐标的数组: ```javascript var coordinates = [ [0, 0], // 第一个的经纬度 [10, 10], // 第二个的经纬度 [20, 20], // 第三个的经纬度 // ... ]; ``` 然后,使用OpenLayers的Vector模块创建一个特征集合,并将坐标添加到特征集合中: ```javascript var features = new ol.Collection(); var feature = new ol.Feature({ geometry: new ol.geom.LineString(coordinates) }); features.push(feature); ``` 再创建一个用于显示特征集合的矢量层: ```javascript var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: features }) }); ``` 最后,将矢量层添加到地图上,就可以在地图上显示坐标的划线了: ```javascript map.addLayer(vectorLayer); ``` 需要注意的是,以上代码只是一个简单示例,实际应用中您可能需要根据具体需求调整参数和添加其他功能。此外,还需要确保引入了OpenLayers的JavaScript文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值