openlayers通过坐标点画出一块区域


直接上代码:
其中的文件是json格式的本地文件,本地文件里坐标太多了,所以将里面的所需要的坐标匹配出来,然后写成json格式, 这样就通过坐标画出面了。

$.getJSON('../../scripts/shanxi-city.geo.json', function (city) {
                angular.forEach(city.features, function (item) {
                    if ($scope.city.name == item.properties.name) {
                        $scope.geoPoint = { features: [item], type: "FeatureCollection" }
                    }
                })
                var styles = [
                    new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: '#5DA7D2',
                            width: 3
                        })
                    })
                ];
                var features = (new ol.format.GeoJSON()).readFeatures($scope.geoPoint, {     // 用readFeatures方法可以自定义坐标系
                    dataProjection: 'EPSG:4326',    // 设定JSON数据使用的坐标系
                    featureProjection: 'EPSG:3857' // 设定当前地图使用的feature的坐标系
                });
                var Source = new ol.source.Vector({
                    features: features
                });

                var vectorLayer = new ol.layer.Vector({
                    name: 'theData',
                    source: Source,
                    style: styles
                });
                console.log(vectorLayer)
                $scope.clear = vectorLayer
                // map.addLayer(vectorLayer);
            })

转载自:https://blog.csdn.net/weixin_42476786/article/details/84394877

You may also like...