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

Circle Geometry Feature not displaying when using OpenMaps / OpenLayers

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

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

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;

如果您想围绕地面上的点设计一个具有固定半径的圆,以便在缩放地图时屏幕大小发生变化,则可以使用几何函数

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
    })
});