1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| import './style.css';
import 'ol/ol.css'; import Map from 'ol/Map'; import View from 'ol/View'; import { OSM } from 'ol/source'; import TileLayer from 'ol/layer/Tile'; import GeoJSON from 'ol/format/GeoJSON'; import VectorSource from 'ol/source/Vector'; import { Fill, Icon, Stroke, Style, Text } from 'ol/style'; import { getBottomLeft, getHeight, getWidth } from 'ol/extent'; import { toContext } from 'ol/render'; import VectorLayer from 'ol/layer/Vector'; import XYZ from 'ol/source/XYZ' import { Control, defaults as defaultControls } from 'ol/control'; import { Size } from 'ol/size'; import { Feature } from 'ol'; import { Geometry } from 'ol/geom'; import { FeatureLike } from 'ol/Feature'; import { attractions } from './attractions';
//定義基本底圖 let baseLayerUrl = 'https://wmts.nlsc.gov.tw/wmts/EMAP/default/GoogleMapsCompatible/{z}/{y}/{x}.png'
//osm //baseLayerUrl = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' let baseLayer = new TileLayer({ source: new XYZ({ url: baseLayerUrl, }) });
//osm let osmLayer = new TileLayer({ source: new OSM(), })
//樣式 let twLayerStyles: Array<Style> = []
//景點圖層 let attractionsLayer = new VectorLayer({ source: new VectorSource({ format: new GeoJSON(), features: new GeoJSON().readFeatures(attractions) //url: './data/attractions.geojson' }),
style: feature => { return genStyle(feature) }, });
//地圖 const map = new Map({ controls: defaultControls({ attribution: false, zoom: false, rotate: false }).extend([
]), // layers: this.layers, layers: [ baseLayer, attractionsLayer ], target: 'map', view: new View({ projection: 'EPSG:4326', center: [120.4553, 22.873], zoom: 11, maxZoom: 18, }), });
// 縮放 icon function scaleAttractionsIcon(map: Map) { var zoom = map.getView().getZoom();
if (zoom === undefined) return 1
if (zoom == 9) { return 0.4; }
if (zoom < 9) { return 0.2; }
return 1; }
// icon 圖片 function setIconSrc(feature: FeatureLike): string { var name = feature.getProperties()['Name']; console.log(name); return `img/attractions-min/${name}.jpg`; // return ''
}
//台灣 let twLayer = new VectorLayer({ // renderMode: 'image', source: new VectorSource({ format: new GeoJSON(), url: 'data/tw.geojson' }), style: feature => { let style: Style let countryName = feature.getProperties()['COUNTYNAME']; console.log(countryName)
let isFind = twLayerStyles.some(x => x.getText().getText() === countryName) if (isFind) { style = twLayerStyles.filter(x => x.getText().getText() === countryName)[0]; return style } else {
let style = new Style({ stroke: new Stroke({ color: 'rgba(0, 0, 0, 1)', width: 1 }), text: new Text({ text: countryName, fill: new Fill({ color: '#000' }), stroke: new Stroke({ color: '#FF8800', width: 10 }), }) }) twLayerStyles.push(style) return style }
}, });
function scaleAttractionsText(feature: FeatureLike): string | string[] { let name = feature.getProperties()['Name'] return name }
let styles: Array<Style> = []
function genStyle(feature: FeatureLike) { let style: Style; // 從 styles 的 cache 裡面找出資料 let isFind = styles.some(x => { return x.getText().getText() === feature.get('Name') }) console.log('isFind', isFind) if (isFind) { // 如果 styles 的 cache 裡面有資料的話 , 回傳該名稱的樣式 style = styles.filter(x => { return x.getText().getText() === feature.get('Name') })[0] return style } else { // 如果沒找到的話新增 style 並且 push 到裡面去 , 最後回傳 style = new Style({ image: new Icon(({ src: setIconSrc(feature), scale: scaleAttractionsIcon(map) })), text: new Text({ text: scaleAttractionsText(feature), fill: new Fill({ color: '#000' }), stroke: new Stroke({ color: '#fff', width: 2 }), offsetY: 24 }) }); styles.push(style) return style; }
}
|