Openlayers 通过geojson设置点要素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>geojson设置点要素</title>
    <!-- 1 导入openlayers依赖-->
    <link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
    <script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
</head>
<body>
    <!-- 2 设置地图容器或地图挂载点-->
    <div id="map"></div>

    <script>
        // 3 初始化地图图层

        const geoda = new ol.layer.Tile({
            title:"高德地图",
            source: new ol.source.XYZ({
                url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                wrapX: false
            })
        });
        // 4 初始化openlayers地图
        const map = new ol.Map({
            // 将初始化的地图设置到id为map 的DOM元素上
            target:"map",
            // 设置图层
            layers:[geoda],
            // 设置视图
            view:new ol.View({
                center:[114.30,30.50],
                // 设置地图放大级别
                zoom :10,
                projection:"EPSG:4326"
            })
        })
        //创建GEOjson数据
        var data = {
            type:"FeatureCollection",
            features:[
                {
                    type:"Feature",
                    geometry:{
                        type:"Point",
                        coordinates:[114.30,30.50]
                    }
                }
            ]
        }
        //数据添加到矢量数据源中
        var source = new ol.source.Vector({
            features:new ol.format.GeoJSON().readFeatures(data)
        })
        //设置矢量图层
        var layer = new ol.layer.Vector({
            source
        })
        //添加地图到矢量容器
        map.addLayer(layer)


    </script>
</body>
</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenLayers是一个开源的JavaScript库,用于在Web上显示地图和地理数据。它提供了丰富的功能和灵活的API,可以用于显示各种地理数据,包括点、线和面。 要在OpenLayers中显示GeoJSON的点、线和面,可以按照以下步骤进行操作: 1. 创建一个OpenLayers地图对象: ```javascript var map = new ol.Map({ target: 'map', // 指定地图容器的ID或元素 layers: [ // 添加地图图层 new ol.layer.Tile({ source: new ol.source.OSM() // 使用OpenStreetMap作为底图 }) ], view: new ol.View({ center: [0, 0], // 地图中心点的坐标 zoom: 10 // 地图缩放级别 }) }); ``` 2. 创建一个GeoJSON格式的数据源: ```javascript var geojsonObject = { 'type': 'FeatureCollection', 'features': [ // 添加点、线、面的GeoJSON要素 { 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [0, 0] // 点的坐标 }, 'properties': { 'name': 'Point' // 点的属性信息 } }, { 'type': 'Feature', 'geometry': { 'type': 'LineString', 'coordinates': [[0, 0], [1, 1]] // 线的坐标数组 }, 'properties': { 'name': 'LineString' // 线的属性信息 } }, { 'type': 'Feature', 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [1, 1], [1, 0], [0, 0]]] // 面的坐标数组 }, 'properties': { 'name': 'Polygon' // 面的属性信息 } } ] }; var vectorSource = new ol.source.Vector({ features: new ol.format.GeoJSON().readFeatures(geojsonObject) }); ``` 3. 创建一个矢量图层,并将数据源添加到图层中: ```javascript var vectorLayer = new ol.layer.Vector({ source: vectorSource }); map.addLayer(vectorLayer); // 将矢量图层添加到地图中 ``` 通过以上步骤,你就可以在OpenLayers地图上显示GeoJSON的点、线和面了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值