javascript - 使用 OpenMaps/OpenLayers 时不显示圆形几何特征

标签 javascript openlayers openlayers-3 openmap

我正在尝试使用自定义标记在 map 上绘制一个点,然后围绕该点绘制一个以英里为单位的圆圈。我可以显示标记,但我似乎无法弄清楚为什么我的圈子不起作用。

谁能看出我做错了什么吗?最终我希望能够用圆圈添加多个点。

HTML

<div id="map"></div>    

<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.js" type="text/javascript"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.css">

    <script>
        let center_point = [-85.3096801, 35.0456297];
        let color_blue = "#2E86C1";

        let marker_icon_image = '/imgs/map/map_point_white.png'';
        let points = [];

        points.push({lat: 35.0456297, lon: -85.3096801, radius: 5, color: color_blue});

    </script>

    <script src="js/PointDrawer.js'"></script>
    <script>
        let init = function () {
            showPointsOnMap(points);
        };

        $(document).ready(init);

    </script>

PointDrawer.js

let attribution = new ol.control.Attribution({
    collapsible: true
});

let marker_icon = new ol.style.Icon(({
    crossOrigin: 'anonymous',
    src: marker_icon_image
}));

let center = ol.proj.transform([center_point[0], center_point[1]], 'EPSG:4326', 'EPSG:3857');

let baseMapLayer = new ol.layer.Tile({
    source: new ol.source.OSM()
});

let map_view = new ol.View({
    center: center,
    maxZoom: 18,
    zoom: 12
});

let map = new ol.Map({
    controls: ol.control.defaults({attribution: false}).extend([attribution]),
    layers: [baseMapLayer],
    loadTilesWhileAnimating: true,
    target: 'map',
    view: map_view,
});


let cache = {};
let features = [];
let vectorSource = new ol.source.Vector({});

let vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    updateWhileAnimating: true,
    updateWhileInteracting: true,
});


function buildFeature(point) {
    console.log([point.lon, point.lat]);

    let feature = new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.transform([point.lon, point.lat], 'EPSG:4326', 'EPSG:3857')),
    });



    let iconStyle = new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [0.5, 0.5],
            anchorXUnits: 'fraction',
            anchorYUnits: 'fraction',
            src: marker_icon_image,
            color: point.color,
            crossOrigin: 'anonymous',
        })
    });

    let circleStyle = new ol.style.Style({
        geometry: 'circle',
        fill: new ol.style.Fill({
            color: point.color
        }),
        stroke: new ol.style.Stroke({
            color: point.color,
            width: 5
        })
    });

    feature.setStyle(iconStyle);
    feature.setStyle(circleStyle);

    return feature;
}

function cacheAvatar(person) {
    // cache styles by photo url
    // let avatar_url = person.avatar;
    let avatar_url = marker_icon_image;
    if (!cache[avatar_url]) {
        console.log('Caching User: ' + person.name + ' Avatar: ' + avatar_url);
        // use the icon style and scale the image to 10% so its not too large
        cache[avatar_url] = new ol.style.Style({
            image: new ol.style.Icon({
                anchor: [0.5, 0.5],
                anchorXUnits: 'fraction',
                anchorYUnits: 'fraction',
                crossOrigin: 'anonymous',
                scale: 0.10,
                src: avatar_url,
                //         src: marker_icon_image,
                color: '#00CEFF',
            })
        });
    }
    return [cache[avatar_url]];
}

function showPointsOnMap(points) {
    points.forEach(function (point) {
    vectorSource.addFeature(buildFeature(point));
});

    map.addLayer(vectorLayer);
}

最佳答案

您可能想使用ol.style.Circle https://openlayers.org/en/v4.6.5/apidoc/ol.style.Circle.html这将在屏幕上设置一个固定大小的圆圈。另外,要在功能上设置两种样式,您必须指定样式数组,因为调用 setStyle 两次会用第二个样式覆盖第一个样式。

let iconStyle = new ol.style.Style({
    image: new ol.style.Icon({
        anchor: [0.5, 0.5],
        anchorXUnits: 'fraction',
        anchorYUnits: 'fraction',
        src: marker_icon_image,
        color: point.color,
        crossOrigin: 'anonymous',
    })
});

let circleStyle = new ol.style.Style({
    image: new ol.style.Circle({
        radius: radius_in_pixels,
        fill: new ol.style.Fill({
            color: point.color
        }),
        stroke: new ol.style.Stroke({
            color: point.color,
            width: 5
        })
    })
});

feature.setStyle([iconStyle, circleStyle]);

return feature;

如果您想要围绕地面上的点设计一个具有固定半径的圆,以便屏幕尺寸随着您缩放 map 而改变,则可以使用几何函数

let circleStyle = new ol.style.Style({
    geometry: function(feature) {
      return new ol.geom.Circle(feature.getGeometry().getCoordinates(), radius_in_meters);
    },
    fill: new ol.style.Fill({
        color: point.color
    }),
    stroke: new ol.style.Stroke({
        color: point.color,
        width: 5
    })
});

关于javascript - 使用 OpenMaps/OpenLayers 时不显示圆形几何特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59383611/

相关文章:

javascript - 我们可以使用 html 标签和 css 来设置 openlayers 中的功能样式吗?

openlayers-3 - 确保所有切片都加载到 Open Layers 3 XYZ 源中

javascript - 如何使用sheets.AppendRow?我在所有单元格中不断收到错误 [Ljava.lang.Object;@29080d11

javascript - Openlayers2 : I want to Draw a buffer on openlayer2 according to user input length

javascript - 更改 OpenLayers 标记图标

openlayers-3 - map.on ('click' ) 在 Firefox 中坐标错误

javascript - AngularJS 格式 JSON 字符串输出

javascript - Highcharts,两个图的系列点之间的算术运算

javascript - 带有按钮的 Jquery 自动 slider

openlayers - 如何在 OpenLayers map 版本 4.6.4 上触发、模拟或调度点击事件?