amap.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <div>
  3. <div id="container" tabindex="0"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import AMapLoader from '@amap/amap-jsapi-loader';
  8. export default {
  9. name: 'amap',
  10. data() {
  11. return {
  12. AMap: null,
  13. map: null,
  14. lngLat: [106.628201, 26.646694],
  15. adName: '',
  16. zoom: 8,
  17. adcode: '贵州省',
  18. mapLevel: 'province',
  19. province: '',
  20. districtExplorer: null,
  21. tipMarker: null,
  22. $tipMarkerContent: null,
  23. currentAreaNode: null
  24. };
  25. },
  26. mounted() {
  27. // this.initMAp();
  28. this.initAMap();
  29. },
  30. created() {},
  31. methods: {
  32. initAMap() {
  33. let _this = this;
  34. window._AMapSecurityConfig = {
  35. securityJsCode: '4a6a8d8ea053d9aa8f4677ee1179fe57' // 密钥
  36. };
  37. AMapLoader.load({
  38. key: '58f9d9f14f2700689ddbc618495693b7', // 申请好的Web端开发者Key,首次调用 load 时必填
  39. version: '1.4.15', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  40. AMapUI: {
  41. // 是否加载 AMapUI,缺省不加载
  42. version: '1.1', // AMapUI 缺省 1.1
  43. plugins: ['overlay/SimpleMarker'] // 需要加载的 AMapUI ui插件
  44. }
  45. // plugins:['AMap.ToolBar','AMap.DistrictSearch','AMap.Object3DLayer','AMap.Object3D'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  46. }).then((AMap) => {
  47. _this.AMap = AMap;
  48. _this.map = new AMap.Map('container', {
  49. //设置地图容器id
  50. viewMode: '3D', //是否为3D地图模式
  51. terrain: true,
  52. zoom: _this.zoom, //初始化地图级别
  53. center: _this.lngLat, //初始化地图中心点位置
  54. pitch: 30,
  55. layers: [
  56. new AMap.TileLayer.Satellite(),
  57. new AMap.TileLayer.RoadNet()
  58. ],
  59. features: ['road', 'bg', 'building'] //地图要素
  60. });
  61. AMapUI.load(
  62. ['ui/geo/DistrictExplorer', 'lib/$'],
  63. function (DistrictExplorer) {
  64. _this.loadMapData(DistrictExplorer, $);
  65. }
  66. );
  67. // this.$nextTick(() => {
  68. // // this.getDistrict()
  69. // });
  70. });
  71. },
  72. getDistrict(adName, mapLevel) {
  73. let that = this;
  74. AMap.plugin(['AMap.DistrictSearch'], function () {
  75. var district = new AMap.DistrictSearch({
  76. // 返回行政区边界坐标等具体信息
  77. extensions: 'all',
  78. // 设置查询行政区级别为 区 (district)
  79. level: mapLevel,
  80. // 显示下级行政区级数,1表示返回下一级行政区
  81. subdistrict: 1
  82. });
  83. district.search(adName, function (status, result) {
  84. var outer = [
  85. new AMap.LngLat(-360, 90, true),
  86. new AMap.LngLat(-360, -90, true),
  87. new AMap.LngLat(360, -90, true),
  88. new AMap.LngLat(360, 90, true)
  89. ];
  90. var holes = result.districtList[0].boundaries;
  91. var pathArray = [outer];
  92. pathArray.push.apply(pathArray, holes);
  93. var polygon = new AMap.Polygon({
  94. pathL: pathArray,
  95. strokeColor: '#00eeff',
  96. strokeWeight: 1,
  97. fillColor: '#1c212a', // 遮罩背景色黑色
  98. fillOpacity: 0.8
  99. });
  100. polygon.setPath(pathArray);
  101. that.map.add(polygon);
  102. // AMap.plugin(['AMap.Object3DLayer', 'AMap.Object3D'], function () {
  103. // //object3Dlayer可以看做一个容器,用来放多个3d对象
  104. // let object3Dlayer = new AMap.Object3DLayer();
  105. // //把object3Dlayer添加到map对象中
  106. // that.map.add(object3Dlayer);
  107. // var wall = new AMap.Object3D.Wall({
  108. // //版块边界线
  109. // path: holes,
  110. // //墙的高度
  111. // height: 80000,
  112. // //墙的颜色
  113. // color: '#0dcdd1'
  114. // });
  115. // //wall 有两个面,该属性表示哪个面可见,可选值:back ,front ,both表示两面 默认为front
  116. // wall.backOrFront = 'both';
  117. // // 是否允许使用透明颜色
  118. // wall.transparent = true;
  119. // //将wall放到object3Dlayer中
  120. // object3Dlayer.add(wall);
  121. // console.log('object3Dlayer');
  122. // });
  123. });
  124. });
  125. },
  126. loadMapData(DistrictExplorer, $) {
  127. let that = this;
  128. //创建一个实例
  129. that.districtExplorer = window.districtExplorer = new DistrictExplorer({
  130. eventSupport: true, //打开事件支持
  131. map: that.map
  132. });
  133. //当前聚焦的区域
  134. this.$tipMarkerContent = $('<div class="tipMarker top"></div>');
  135. this.tipMarker = new AMap.Marker({
  136. content: this.$tipMarkerContent.get(0),
  137. offset: new AMap.Pixel(0, 0),
  138. bubble: true
  139. });
  140. //监听feature的hover事件
  141. this.districtExplorer.on(
  142. 'featureMouseout featureMouseover',
  143. (e, feature) => {
  144. this.toggleHoverFeature(
  145. feature,
  146. e.type === 'featureMouseover',
  147. e.originalEvent ? e.originalEvent.lnglat : null
  148. );
  149. }
  150. );
  151. //监听鼠标在feature上滑动
  152. this.districtExplorer.on('featureMousemove', (e) => {
  153. //更新提示位置
  154. this.tipMarker.setPosition(e.originalEvent.lnglat);
  155. });
  156. var adName = that.adcode; //贵州省
  157. var mapLevel = that.mapLevel;
  158. var adcode = '520000';
  159. //feature被点击
  160. this.districtExplorer.on('featureClick', (e, feature) => {
  161. that.map.clearMap();
  162. const props = feature.properties;
  163. this.switch2AreaNode(props.adcode);
  164. mapLevel = props.level;
  165. adName = props.name;
  166. that.getDistrict(adName, mapLevel);
  167. });
  168. this.switch2AreaNode(adcode);
  169. that.getDistrict(adName, mapLevel);
  170. },
  171. //根据Hover状态设置相关样式
  172. toggleHoverFeature(feature, isHover, position) {
  173. if (feature.properties.subFeatureIndex != null) {
  174. this.tipMarker.setMap(isHover ? this.map : null);
  175. if (!feature) {
  176. return;
  177. }
  178. const props = feature.properties;
  179. if (isHover) {
  180. //更新提示内容
  181. this.$tipMarkerContent.html(props.name);
  182. //更新位置
  183. this.tipMarker.setPosition(position || props.center);
  184. }
  185. //更新相关多边形的样式
  186. const polys = this.districtExplorer.findFeaturePolygonsByAdcode(
  187. props.adcode
  188. );
  189. polys.forEach((elemnt) => {
  190. elemnt.setOptions({
  191. fillOpacity: isHover ? 0.5 : 0
  192. });
  193. });
  194. }
  195. },
  196. //绘制某个区域的边界
  197. renderAreaPolygons(areaNode) {
  198. //更新地图视野
  199. if (!this.aReContry) {
  200. this.map.setBounds(areaNode.getBounds(), null, null, true);
  201. } else {
  202. this.map.setZoom(4);
  203. this.map.setCenter(new AMap.LngLat(103.714129, 38.150339));
  204. }
  205. //清除已有的绘制内容
  206. this.districtExplorer.clearFeaturePolygons();
  207. //绘制子区域
  208. this.districtExplorer.renderSubFeatures(areaNode, () => {
  209. return {
  210. cursor: 'default',
  211. bubble: true,
  212. strokeColor: 'yellow', //线颜色
  213. strokeOpacity: 1, //线透明度
  214. strokeWeight: 2, //线宽
  215. fillOpacity: 0 //填充透明度
  216. };
  217. });
  218. //绘制父区域
  219. this.districtExplorer.renderParentFeature(areaNode, {
  220. cursor: 'default',
  221. bubble: true,
  222. strokeColor: '#00eeff', //线颜色
  223. strokeOpacity: 1, //线透明度
  224. strokeWeight: 2, //线宽
  225. fillOpacity: 0 //填充透明度
  226. });
  227. },
  228. //切换区域后刷新显示内容
  229. refreshAreaNode(areaNode) {
  230. this.districtExplorer.setHoverFeature(null);
  231. this.renderAreaPolygons(areaNode);
  232. },
  233. //切换区域
  234. switch2AreaNode(adcode, callback) {
  235. if (
  236. this.currentAreaNode &&
  237. '' + this.currentAreaNode.getAdcode() === '' + adcode
  238. ) {
  239. return;
  240. }
  241. this.loadAreaNode(adcode, (error, areaNode) => {
  242. if (error) {
  243. if (callback) {
  244. callback(error);
  245. }
  246. return;
  247. }
  248. this.currentAreaNode = areaNode;
  249. //设置当前使用的定位用节点
  250. this.districtExplorer.setAreaNodesForLocating([this.currentAreaNode]);
  251. this.refreshAreaNode(areaNode);
  252. if (callback) {
  253. callback(null, areaNode);
  254. }
  255. });
  256. },
  257. // 加载区域
  258. loadAreaNode(adcode, callback) {
  259. this.districtExplorer.loadAreaNode(adcode, (error, areaNode) => {
  260. if (error) {
  261. if (callback) {
  262. callback(error);
  263. }
  264. window.console.error(error);
  265. return;
  266. }
  267. if (callback) {
  268. callback(null, areaNode);
  269. }
  270. });
  271. }
  272. }
  273. };
  274. </script>
  275. <style lang='scss' scoped>
  276. #container {
  277. width: 100%;
  278. height: 100vh;
  279. /deep/ .tdt-pane {
  280. z-index: auto;
  281. }
  282. }
  283. /deep/ .amap-marker-content {
  284. .tipMarker {
  285. color: #555;
  286. background-color: rgba(255, 254, 239, 0.8);
  287. border: 1px solid #7e7e7e;
  288. padding: 2px 6px;
  289. font-size: 12px;
  290. white-space: nowrap;
  291. display: inline-block;
  292. &:before,
  293. &:after {
  294. content: '';
  295. display: block;
  296. position: absolute;
  297. margin: auto;
  298. width: 0;
  299. height: 0;
  300. border: solid transparent;
  301. border-width: 5px 5px;
  302. }
  303. &.top {
  304. transform: translate(-50%, -110%);
  305. &:before,
  306. &:after {
  307. bottom: -9px;
  308. left: 0;
  309. right: 0;
  310. border-top-color: rgba(255, 254, 239, 0.8);
  311. }
  312. &:before {
  313. bottom: -10px;
  314. border-top-color: #7e7e7e;
  315. }
  316. }
  317. }
  318. }
  319. /deep/ .amap-logo,
  320. .amap-copyright {
  321. display: block !important;
  322. visibility: inherit !important;
  323. }
  324. </style>