{"id":225520,"date":"2021-09-06T10:48:13","date_gmt":"2021-09-06T02:48:13","guid":{"rendered":"https:\/\/gulass.cn\/?p=225520"},"modified":"2021-08-27T16:49:16","modified_gmt":"2021-08-27T08:49:16","slug":"vue-openlayer-select","status":"publish","type":"post","link":"https:\/\/gulass.cn\/vue-openlayer-select.html","title":{"rendered":"Vue+Openlayer\u4e2d\u4f7f\u7528select\u9009\u62e9\u8981\u7d20"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u672c\u6587\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u7ed9\u5927\u5bb6\u4ecb\u7ecdVue+Openlayer\u4e2d\u4f7f\u7528select\u9009\u62e9\u8981\u7d20\uff0c\u4ee3\u7801\u7b80\u5355\u6613\u61c2\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u501f\u9274\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53c2\u8003\u4e0b\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u6548\u679c\u56fe\uff1a \u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n \r\n<template>\r\n <div id=\"map\" ref=\"map\" style=\"width: 100vw; height: 100vh\"><\/div>\r\n<\/template>\r\n \r\n<script>\r\nimport \"ol\/ol.css\";\r\nimport { Map, View } from \"ol\";\r\nimport { OSM, Vector as VectorSource } from \"ol\/source\";\r\nimport { Vector as VectorLayer, Tile as TileLayer } from \"ol\/layer\";\r\nimport GeoJSON from \"ol\/format\/GeoJSON\";\r\n \r\nimport Select from \"ol\/interaction\/Select\";\r\nimport { altKeyOnly, click, pointerMove } from \"ol\/events\/condition\";\r\n \r\nexport default {\r\n name: \"gif\",\r\n data() {\r\n return {\r\n map: {},\r\n layer: {},\r\n geojsonData: {\r\n type: \"FeatureCollection\",\r\n features: [\r\n {\r\n type: \"Feature\",\r\n properties: {\r\n title: \"\u8b66\u62a51\",\r\n },\r\n geometry: {\r\n type: \"Point\",\r\n coordinates: [91.48879670091165, 37.83814884701121],\r\n },\r\n },\r\n {\r\n type: \"Feature\",\r\n properties: {\r\n title: \"\u8b66\u62a52\",\r\n },\r\n geometry: {\r\n type: \"Point\",\r\n coordinates: [99.19515576149941, 26.713646654711134],\r\n },\r\n },\r\n {\r\n type: \"Feature\",\r\n properties: {\r\n title: \"\u8b66\u62a53\",\r\n },\r\n geometry: {\r\n type: \"Point\",\r\n coordinates: [123.74363825288785, 44.363694825734726],\r\n },\r\n },\r\n ],\r\n },\r\n select: {},\r\n };\r\n },\r\n mounted() {\r\n this.initMap();\r\n },\r\n methods: {\r\n \/\/ \u521d\u59cb\u5316\u5730\u56fe\r\n initMap() {\r\n this.layer = new VectorLayer({\r\n source: new VectorSource({\r\n features: new GeoJSON().readFeatures(this.geojsonData),\r\n }),\r\n });\r\n this.map = new Map({\r\n target: \"map\",\r\n layers: [\r\n new TileLayer({\r\n source: new OSM(),\r\n }),\r\n this.layer,\r\n ],\r\n view: new View({\r\n projection: \"EPSG:4326\",\r\n center: [104.912777, 34.730746],\r\n zoom: 4.5,\r\n }),\r\n });\r\n \r\n this.select = new Select({\r\n condition: click, \/\/\u5355\u51fb\u9009\u62e9\r\n });\r\n this.map.addInteraction(this.select);\r\n this.select.on(\"select\", (e) => {\r\n let coordinate = e.mapBrowserEvent.coordinate; \/\/\u83b7\u53d6\u9009\u62e9\u7684\u5750\u6807\r\n let properties = e.selected[0].getProperties(); \/\/\u83b7\u53d6\u5f53\u524d\u8981\u7d20\u7684\u6240\u6709\u5c5e\u6027\r\n });\r\n \r\n \/\/ \u8bbe\u7f6e\u9f20\u6807\u5212\u8fc7\u77e2\u91cf\u8981\u7d20\u7684\u6837\u5f0f\r\n this.map.on(\"pointermove\", (e) => {\r\n const isHover = this.map.hasFeatureAtPixel(e.pixel);\r\n this.map.getTargetElement().style.cursor = isHover ? \"pointer\" : \"\";\r\n });\r\n },\r\n },\r\n};\r\n<\/script>\r\n<\/pre>\n |