QqMap.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. const template=`
  2. <component is="style">
  3. .searchPanel{
  4. width: 100%;
  5. position: relative;
  6. z-index: 999
  7. }
  8. .search{
  9. width: 100%;
  10. }
  11. .searchList{
  12. width: 100%;
  13. position: relative;
  14. top:5px;
  15. }
  16. .searchList .el-card__body{
  17. padding: 0;
  18. }
  19. .searchList ul{
  20. margin: 0;
  21. padding: 0;
  22. }
  23. .searchList ul li{
  24. list-style: none;
  25. height: 30px;
  26. line-height: 30px;
  27. cursor: pointer;
  28. padding:0 15px;
  29. }
  30. .searchList ul li:hover{
  31. background: var(--el-color-primary-light-9);
  32. }
  33. #map-box{
  34. position: fixed;
  35. top: 0;
  36. left: 0;
  37. right: 0;
  38. bottom: 0;
  39. z-index: 998;
  40. cursor: crosshair;
  41. }
  42. .confirmPosition{
  43. position: absolute;
  44. right: 10px;
  45. bottom: 10px;
  46. width:60px;
  47. z-index: 999;
  48. }
  49. .closePosition{
  50. position: absolute;
  51. right: 85px;
  52. bottom: 10px;
  53. width:60px;
  54. z-index: 999;
  55. }
  56. </component>
  57. <el-input v-model="input" placeholder="选择位置" :readonly="true">
  58. <template #append>
  59. <el-button type="primary" size="small" @click="show=true;">选择</el-button>
  60. </template>
  61. </el-input>
  62. <el-drawer
  63. v-model="show"
  64. custom-class="chooseIcon"
  65. size="100%"
  66. @opened="showMap"
  67. :show-close="false"
  68. direction="rtl">
  69. <template #title>
  70. <div class="searchPanel">
  71. <el-input v-model="search" class="search">
  72. <template #append>
  73. <el-button type="primary" size="small" @click="searchMap">搜索</el-button>
  74. </template>
  75. </el-input>
  76. <el-card shadow="never" class="searchList" v-if="searchList.length>0">
  77. <ul>
  78. <li v-for="item in searchList" @click="choose(item)">{{item.title}}</li>
  79. </ul>
  80. </el-card>
  81. </div>
  82. </template>
  83. <div id="map-box"></div>
  84. <el-button type="danger" class="closePosition" @click="show=false">取消</el-button>
  85. <el-button type="primary" class="confirmPosition" @click="confirm">确认</el-button>
  86. </el-drawer>
  87. `;
  88. let marker,map;
  89. export default {
  90. name: "qqmap",
  91. data: function () {
  92. return {
  93. input:'',
  94. map:'',
  95. marker:'',
  96. show:false,
  97. search:'',
  98. searchList:[],
  99. init:false
  100. }
  101. },
  102. props:{
  103. value:''
  104. },
  105. mounted:function (){
  106. Yunqi.ajax.get('qqmap/key').then(res=>{
  107. Yunqi.use('https://map.qq.com/api/gljs?v=1.exp&key='+res);
  108. });
  109. if(this.value){
  110. this.input=this.value;
  111. }
  112. },
  113. emits:['change'],
  114. template:template,
  115. methods:{
  116. //搜索地图
  117. searchMap:function (){
  118. Yunqi.ajax.get('qqmap/search',{keywords:this.search},true,false).then(res=>{
  119. if(res){
  120. this.searchList=res;
  121. }else{
  122. Yunqi.message.info('没有找到相关位置')
  123. }
  124. });
  125. },
  126. choose:function (e){
  127. this.search=e.title;
  128. map.setZoom(18);
  129. map.setCenter(new TMap.LatLng(e.location.lat,e.location.lng));
  130. this.searchList=[];
  131. },
  132. confirm:function (){
  133. if(marker){
  134. let lat = Number(marker.geometries[0].position.lat).toFixed(8);
  135. let lng = Number(marker.geometries[0].position.lng).toFixed(8);
  136. this.input=lat+','+lng;
  137. this.$emit('change',this.input);
  138. }
  139. this.show=false;
  140. },
  141. parseMaker:function(lat,lng){
  142. marker = new TMap.MultiMarker({
  143. map: map,
  144. styles: {
  145. "myStyle": new TMap.MarkerStyle({
  146. "width": 24, // 点标记样式宽度(像素)
  147. "height": 24, // 点标记样式高度(像素)
  148. "src": '/assets/img/zuobiao.png',
  149. })
  150. },
  151. //点标记数据数组
  152. geometries: [{
  153. "id": "1", //点标记唯一标识,后续如果有删除、修改位置等操作,都需要此id
  154. "styleId": 'myStyle', //指定样式id
  155. "position": new TMap.LatLng(lat, lng), //点标记坐标位置
  156. "properties": {
  157. "title": ""
  158. }
  159. }]
  160. });
  161. },
  162. showMap:function (){
  163. if(this.init){
  164. return;
  165. }
  166. let container = document.getElementById("map-box");
  167. let lat='26.6015';
  168. let lng='106.62254';
  169. if(this.value){
  170. let arr=this.value.split(',');
  171. lat=arr[0];
  172. lng=arr[1];
  173. }
  174. let center = new TMap.LatLng(lat,lng);
  175. map = new TMap.Map(container, {
  176. center: center,
  177. zoom: 13,
  178. viewMode:'3D',
  179. minZoom:7
  180. });
  181. this.parseMaker(lat,lng);
  182. //点击map获取经纬度
  183. map.on("click", (e) => {
  184. if(marker){
  185. marker.setMap(null);
  186. }
  187. let lat = e.latLng.lat;
  188. let lng = e.latLng.lng;
  189. this.parseMaker(lat,lng);
  190. });
  191. this.init=true;
  192. }
  193. }
  194. };