Map.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div class="main">
  3. <div id="Mapcontainer"></div><!-- 地图容器 -->
  4. <!-- <input type="text" v-model="keyword" class="keywordInput" @change="keywordsSearch" placeholder="输入关键字">
  5. <div id="panel" v-show="queryReusltIsshow"></div> 关键字查询结果展示框 -->
  6. </div>
  7. </template>
  8. <script>
  9. let map = undefined;
  10. let placeSearch = undefined;
  11. // import BMap from 'BMap'
  12. import AMapLoader from '@amap/amap-jsapi-loader';
  13. // window._AMapSecurityConfig = {
  14. // // 安全密钥
  15. // securityJsCode: "d6a497a6074277b5f144512f74e36d16",
  16. // };
  17. export default {
  18. data() {
  19. return {
  20. position: {
  21. lng: 0,
  22. lat: 0,
  23. address: ""
  24. }, //传出坐标对象
  25. keyword: '', //关键字
  26. queryReusltIsshow: false, //查询列表是否显示
  27. geoCoder: null
  28. }
  29. },
  30. props: {
  31. lng: { //精度
  32. type: String,
  33. default: ''
  34. },
  35. lat: { //纬度
  36. type: String,
  37. default: ''
  38. }
  39. },
  40. methods: {
  41. //初始化地图
  42. // initMap() {
  43. // var that = this
  44. // AMapLoader.load({
  45. // key: "50a17f18b751052cc34771f93b897846", // 申请好的Web端开发者Key,首次调用 load 时必填
  46. // version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  47. // plugins: ['AMap.Geolocation,AMap.PlaceSearch'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  48. // }).then((AMap) => {
  49. // // AMap = AMap
  50. // map = new AMap.Map("Mapcontainer", { //设置地图容器id
  51. // viewMode: "2D", //是否为3D地图模式
  52. // zoom: 16, //初始化地图级别
  53. // });
  54. // //地址逆解析插件
  55. // AMap.plugin("AMap.Geocoder", function () {
  56. // that.geoCoder = new AMap.Geocoder({
  57. // city: "010", //城市设为北京,默认:“全国”
  58. // radius: 1000, //范围,默认:500
  59. // });
  60. // })
  61. //
  62. //
  63. // /* // 搜索提示插件
  64. // this.AutoComplete = new AMap.AutoComplete({ city: "全国" });*/
  65. // map.on('click', this.MapClick); //绑定单机事件
  66. //
  67. // if (this.lng && this.lat) {
  68. // let marker = new AMap.Marker({
  69. // position: new AMap.LngLat(this.lng, this.lat),
  70. // icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
  71. // offset: new AMap.Pixel(-13, -30)
  72. // });
  73. // map.setCenter([this.lng, this.lat]);
  74. // map.add(marker)
  75. // } else {
  76. // this.getLocation(map)
  77. // }
  78. //
  79. // }).catch(e => {
  80. // console.log(e);
  81. // })
  82. // },
  83. // 关键字搜索功能r
  84. // keywordsSearch() {
  85. // if (!this.keyword) {
  86. // this.queryReusltIsshow = false
  87. // }
  88. // let self = this
  89. // map.plugin('AMap.PlaceSearch', function () {
  90. // placeSearch = new AMap.PlaceSearch({
  91. // pageSize: 8, // 单页显示结果条数
  92. // pageIndex: 1, // 页码
  93. // city: "全国", // 兴趣点城市
  94. // citylimit: false, //是否强制限制在设置的城市内搜索
  95. // type: "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施",
  96. // map: map, // 展现结果的地图实例
  97. // panel: "panel", // 结果列表将在此容器中进行展示。
  98. // autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
  99. // });
  100. // //关键字查询
  101. // placeSearch.search(self.keyword, function (status, result) {
  102. // console.log('查询结果', result)
  103. // if (result == 'USER_DAILY_QUERY_OVER_LIMIT') {
  104. // window.alert('免费配额查询超出限制!')
  105. // } else {
  106. // self.queryReusltIsshow = true
  107. // }
  108. // });
  109. // })
  110. // },
  111. //地图单机事件
  112. MapClick(e) {
  113. this.position = {
  114. lng: e.lnglat.lng,
  115. lat: e.lnglat.lat,
  116. }
  117. this.toaddress().then((data)=>{
  118. this.$emit('recievePosition', data)
  119. })
  120. this.clearMap()
  121. let marker = new AMap.Marker({
  122. position: new AMap.LngLat(e.lnglat.lng, e.lnglat.lat),
  123. icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
  124. offset: new AMap.Pixel(-22, -55)
  125. });
  126. map.add(marker)
  127. },
  128. toaddress(){
  129. return new Promise((resolve)=>{
  130. var that = this;
  131. let lnglat = [this.position.lng, this.position.lat];
  132. this.geoCoder.getAddress(lnglat, function (status, result) {
  133. if (status === 'complete' && result.info === 'OK') {
  134. that.position.address = result.regeocode.formattedAddress;
  135. resolve(that.position)
  136. /* that.position.lng=e.lnglat.lng;
  137. that.position.lng=e.lnglat.lng;*/
  138. // result为对应的地理位置详细信息
  139. }
  140. })
  141. })
  142. },
  143. //高德API定位
  144. getLocation() {
  145. const that = this;
  146. map.plugin('AMap.Geolocation', function () {
  147. var geolocation = new AMap.Geolocation({
  148. enableHighAccuracy: true, // 是否使用高精度定位,默认:true
  149. timeout: 1000, // 设置定位超时时间,默认:无穷大
  150. offset: [10, 20], // 定位按钮的停靠位置的偏移量
  151. zoomToAccuracy: true, // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
  152. position: 'RB' // 定位按钮的排放位置, RB表示右下
  153. })
  154. geolocation.getCurrentPosition(function (status, result) {
  155. if (status == 'complete') {
  156. onComplete(result)
  157. } else {
  158. onError(result)
  159. }
  160. });
  161. function onComplete(data) {
  162. map.setCenter([data.position.KL, data.position.kT]);
  163. }
  164. function onError(data) {
  165. // 定位出错
  166. console.log('定位出错', data)
  167. // that.lib_getPosition();
  168. }
  169. })
  170. },
  171. // lib_getPosition() {
  172. // const that = this;
  173. // const BMapGeolocation = new BMap.Geolocation();
  174. //
  175. // function myFun(result) {
  176. // console.log(result)
  177. // if (result.center.lng && result.center.lat) {
  178. // console.log("baidu");
  179. // // 获取到经纬度(百度转换高德经纬度)
  180. // const {lng, lat} = that.bd_decrypt(result.center.lng, result.center.lat);
  181. // that.center = [lng, lat];
  182. // map.setCenter([lng, lat]);
  183. // // 根据经纬度获取当前经纬度的位置信息
  184. // } else {
  185. // console.log(22);
  186. // }
  187. // }
  188. //
  189. // var myCity = new BMap.LocalCity();
  190. // myCity.get(myFun);
  191. // },
  192. bd_decrypt(bdLng, bdLat) {
  193. const X_PI = Math.PI * 3000.0 / 180.0;
  194. const x = bdLng - 0.0065;
  195. const Y = bdLat - 0.006;
  196. const Z = Math.sqrt(x * x + Y * Y) - 0.00002 * Math.sin(Y * X_PI);
  197. const Theta = Math.atan2(Y, x) - 0.000003 * Math.cos(x * X_PI);
  198. const lng = Z * Math.cos(Theta);
  199. const lat = Z * Math.sin(Theta);
  200. return {lng, lat};
  201. },
  202. //确定按钮
  203. btnEnter() {
  204. if (this.position) {
  205. this.$emit('recieveposition', this.position)
  206. window.alert('坐标采集成功!')
  207. } else {
  208. window.alert('坐标为空,请点击选择坐标!')
  209. }
  210. },
  211. //清除地图所有覆盖物
  212. clearMap() {
  213. map.clearMap();
  214. }
  215. },
  216. mounted() {
  217. this.initMap()
  218. }
  219. }
  220. </script>
  221. <style scoped>
  222. .main {
  223. width: 100%;
  224. height: 100%;
  225. position: relative;
  226. }
  227. .keywordInput {
  228. padding: 0;
  229. width: 320px;
  230. height: 30px;
  231. position: absolute;
  232. right: 20px;
  233. top: 10px;
  234. border: 0px;
  235. }
  236. .keywordInput:focus {
  237. outline: 0;
  238. /* border: 1px solid #123456; */
  239. border: 0px;
  240. }
  241. #Mapcontainer {
  242. width: 100%;
  243. height: 100%;
  244. position: absolute;
  245. overflow: hidden !important;
  246. }
  247. .amap-layers {
  248. overflow: hidden !important;
  249. }
  250. .keywordSearch {
  251. position: absolute;
  252. right: 10px;
  253. top: 10px;
  254. width: 70px;
  255. height: 30px;
  256. line-height: 30px;
  257. background-color: #1685a9;
  258. color: #fff;
  259. z-index: 1000;
  260. border: 0;
  261. }
  262. .btnEnter {
  263. position: absolute;
  264. right: 10px;
  265. bottom: 10px;
  266. width: 70px;
  267. height: 30px;
  268. line-height: 30px;
  269. background-color: #1685a9;
  270. color: #fff;
  271. z-index: 1000;
  272. border: 0;
  273. }
  274. #panel {
  275. position: absolute;
  276. right: 20px;
  277. top: 50px;
  278. width: 320px;
  279. height: 650px;
  280. /* z-index: 1000; */
  281. background-color: white;
  282. overflow-y: auto;
  283. }
  284. </style>