SelectMap.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <template>
  2. <view class="container" >
  3. <view id="container" :style="myStyle">
  4. </view>
  5. <view class="botton">
  6. <view style="margin-bottom: 20px;">{{address}}</view>
  7. <button class="get" @click="okBtn">确认</button>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import MapLoader from '@/utils/AMap'
  13. let _self;
  14. function mapMovestart(e) {
  15. //'movestart')
  16. }
  17. function mapMove(e){
  18. // //'mapMove')
  19. }
  20. function mapMoveend(e){
  21. //let _self = e;
  22. let pos = _self.logMapInfo();
  23. console.log(pos)
  24. _self.getPositionByLonLats({
  25. latitude:pos.center.lat,
  26. longitude : pos.center.lng
  27. })
  28. _self.$emit('onMoveEnd',pos);
  29. }
  30. export default {
  31. name:"Chargermap",
  32. data() {
  33. return {
  34. enabledMove:true,
  35. myStyle:"width: 100%; height: 1200rpx;",
  36. chargerIcon:'',
  37. chargerSelectedIcon:'',
  38. positionIcon:'',
  39. personIcon:'',
  40. longitude: 112.276527,
  41. latitude: 30.306427,
  42. AMap:null,
  43. //车的位置
  44. car: {
  45. obj:null,
  46. longitude: '',
  47. latitude: '',
  48. },
  49. down: {
  50. obj:null,
  51. longitude: '',
  52. latitude: '',
  53. },
  54. up: {
  55. obj:null,
  56. longitude: '',
  57. latitude: '',
  58. },
  59. address:"",
  60. //个人
  61. info: {
  62. obj:null,
  63. init: false,
  64. longitude: '',
  65. latitude: '',
  66. },
  67. mapcharger:null,
  68. startend: [{
  69. longitude:112.279274,
  70. latitude:30.303273,
  71. name:''
  72. }, {
  73. longitude:112.273867,
  74. latitude:30.309817,
  75. name:''
  76. }],
  77. start:{},
  78. end:{},
  79. chargerList:[],
  80. covers: [
  81. {
  82. longitude:112.273867,
  83. latitude:30.309817,
  84. },{
  85. longitude:112.280261,
  86. latitude:30.3041,
  87. },{
  88. longitude:112.279274,
  89. latitude:30.303273,
  90. }
  91. ],
  92. };
  93. },
  94. methods:{
  95. okBtn(){
  96. let pos = this.logMapInfo();
  97. this.$emit('okBtn',{
  98. latitude:pos.center.lat,
  99. longitude : pos.center.lng,
  100. address:this.address
  101. });
  102. },
  103. getPositionByLonLats(pos) {
  104. var _this = this;
  105. console.log(pos)
  106. MapLoader().then(AMap => {
  107. var lnglatXY =//[_this.longitude, _this.latitude]
  108. [pos.longitude, pos.latitude];
  109. AMap.service('AMap.Geocoder', function() {
  110. let geocoder = new AMap.Geocoder({});
  111. geocoder.getAddress(lnglatXY, function(status, result) {
  112. //lnglatXY);
  113. //status, result);
  114. if (status === 'complete' && result.info === 'OK') {
  115. console.log(address)
  116. var address = result.regeocode.formattedAddress;
  117. _this.address=address
  118. _this.$forceUpdate()
  119. console.log(address)
  120. //'当前位置'+JSON.stringify(lnglatXY)+address);
  121. //_this.message= address;
  122. } else {
  123. //_this.message= '无法获取定位';
  124. }
  125. });
  126. });
  127. }).catch(error => {
  128. //error)
  129. })
  130. },
  131. setMyStyle(s){
  132. this.myStyle=s;
  133. },
  134. checkCenter(item){
  135. let pos = this.mapcharger.getCenter();
  136. if(pos.lng == item.longitude && pos.lat == item.latitude){
  137. //'setCenter pos '+JSON.stringify(pos))
  138. //'setCenter item '+JSON.stringify(item))
  139. this.listenMove();
  140. }else{
  141. //'setCenter pos1 '+JSON.stringify(pos))
  142. //'setCenter item1 '+JSON.stringify(item))
  143. setTimeout(() => {
  144. this.checkCenter(item);
  145. }, 1000)
  146. }
  147. },
  148. setCenter(item){
  149. // this.enableMove(false);
  150. this.removeMove();
  151. // //'setCenter'+JSON.stringify(item))
  152. if(this.mapcharger){
  153. this.mapcharger.setCenter([item.longitude,item.latitude]);
  154. this.checkCenter(item);
  155. // this.listenMove();
  156. // this.enableMove(true);
  157. return true
  158. }else{
  159. // this.listenMove();
  160. // this.enableMove(true);
  161. return false
  162. }
  163. },
  164. logMapInfo() {
  165. var posCenter = this.mapcharger.getCenter();
  166. // //'center'+JSON.stringify(posCenter));
  167. var limitBounds = this.mapcharger.getBounds();
  168. let pos = {center:posCenter,bounds:limitBounds};
  169. return pos;
  170. },
  171. init(){
  172. _self = this;
  173. // //'init')
  174. var _this = this;
  175. MapLoader().then(AMap => {
  176. _this.AMap=AMap;
  177. _this.mapcharger && _this.mapcharger.destroy();
  178. //使用地图容器ID创建
  179. _this.mapcharger=new AMap.Map("container", {
  180. resizeEnable: true,
  181. dragEnable : true,
  182. center: [_this.longitude, _this.latitude],
  183. zoom: 13
  184. });
  185. //_this.mapcharger.setMapStyle('amap://styles/macaron');
  186. _this.mapcharger.setMapStyle('amap://styles/f9b17f73bb4576ab1894c29fe9d03c6c');
  187. //
  188. var img=require("@/assets/img/md-my_location@3x.png")
  189. // 创建一个自定义图标
  190. var icon = new AMap.Icon({
  191. size: new AMap.Size(40, 40), // 图标的大小
  192. image: img, // 图标的图片URL
  193. imageSize: new AMap.Size(40, 40) ,// 图片的实际大小
  194. anchor: 'center', //设置锚点
  195. offset: new AMap.Pixel(0,0) //设置偏移量
  196. });
  197. // 创建标记并将其添加到地图中心
  198. var marker = new AMap.Marker({
  199. position: _this.mapcharger.getCenter(), // 设置标记的坐标为地图的中心
  200. icon: icon, // 设置标记的图标
  201. map: _this.mapcharger // 将标记添加到地图
  202. });
  203. var clickHandler = function(e) {
  204. _this.$emit('onClicked',e);
  205. //'您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图!');
  206. };
  207. _this.mapcharger.on('moveend', function() {
  208. marker.setPosition(_this.mapcharger.getCenter()); // 更新标记位置为地图中心
  209. var pos=_this.mapcharger.getCenter()
  210. _this.getPositionByLonLats({
  211. latitude:pos.lat,
  212. longitude : pos.lng
  213. })
  214. });
  215. _this.mapcharger.on('click', clickHandler);
  216. _this.$emit('onload')
  217. //_this.addPosition();
  218. _this.listenMove();
  219. }, e => {
  220. //_this.mui.toast('地图加载失败');
  221. //'地图加载失败', e)
  222. console.log(e)
  223. })
  224. },
  225. removeMove(){
  226. var _this = this;
  227. //this.enabledMove = false;
  228. // _this.mapcharger.off('movestart', mapMovestart);
  229. // _this.mapcharger.off('mapmove', mapMove);
  230. // _this.mapcharger.off('moveend', mapMoveend);
  231. },
  232. enableMove(enable){
  233. this.enabledMove = enable;
  234. },
  235. listenMove(){
  236. var _this = this;
  237. /*var mapMovestart = function(){
  238. if(!_this.enabledMove)
  239. return;
  240. //'movestart')
  241. }
  242. var mapMove = function(){
  243. if(!_this.enabledMove)
  244. return;
  245. ////'mapMove')
  246. }
  247. var mapMoveend = function(){
  248. let pos = _this.logMapInfo();
  249. if(!_this.enabledMove)
  250. return;
  251. _this.$emit('onMoveEnd',pos);
  252. }*/
  253. // //'listenMove')
  254. // _this.mapcharger.on('movestart', mapMovestart);
  255. // _this.mapcharger.on('mapmove', mapMove);
  256. // _this.mapcharger.on('moveend',mapMoveend);
  257. },
  258. navigate(startLngLat,endLngLat){
  259. //'start'+JSON.stringify(startLngLat))
  260. //'end'+JSON.stringify(endLngLat))
  261. AMap.plugin('AMap.Driving', function() {
  262. var driving = new AMap.Driving({
  263. // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
  264. policy: AMap.DrivingPolicy.LEAST_TIME
  265. })
  266. // var startLngLat = [116.379028, 39.865042]
  267. // var endLngLat = [116.427281, 39.903719]
  268. driving.search(startLngLat, endLngLat, function (status, result) {
  269. //'status'+JSON.stringify(result))
  270. // 未出错时,result即是对应的路线规划方案
  271. })
  272. })
  273. },
  274. initMap(point,stationList,startend){
  275. //'初始化地图'+this.longitude+this.latitude)
  276. var _this = this;
  277. this.covers=point;
  278. this.startend=startend;
  279. MapLoader().then(AMap => {
  280. this.AMap=AMap;
  281. _this.mapcharger && _this.mapcharger.destroy();
  282. _this.mapcharger=new AMap.Map("container", {
  283. resizeEnable: true,
  284. center: [this.longitude, this.latitude],
  285. //center: [116.397428, 39.90923],
  286. zoom: 14
  287. });
  288. var lineArr=[]
  289. this.$emit('onload')
  290. }, e => {
  291. //_this.mui.toast('地图加载失败');
  292. //'地图加载失败', e)
  293. })
  294. },
  295. }
  296. }
  297. </script>
  298. <style scoped lang="scss">
  299. /*去除下标*/
  300. .amap-logo{
  301. display: none;
  302. }
  303. .amap-copyright{
  304. opacity:0;
  305. font-size: 1px;
  306. }
  307. .input-card {
  308. width: 80rpx;
  309. height: 80rpx;
  310. display: flex;
  311. flex-direction: column;
  312. min-width: 0;
  313. word-wrap: break-word;
  314. background-clip: border-box;
  315. border-radius: .25rem;
  316. border-width: 0;
  317. border-radius: 0.4rem;
  318. position: fixed;
  319. top: calc(50% - 30rpx); ;
  320. left: calc(50% - 30rpx); ;
  321. bottom: 1rem;
  322. right: 1rem;
  323. -ms-flex: 1 1 auto;
  324. flex: 1 1 auto;
  325. padding: 0.75rem 0.75rem;
  326. }
  327. .station-icon {
  328. width: 36px;
  329. height: 36px;
  330. line-height: 36px;
  331. text-align: center;
  332. border: 1px solid rgba(133, 140, 255, 100);
  333. color: #b58cff;
  334. border-radius: 999px;
  335. position: absolute;
  336. top: 89px;
  337. left: 38px;
  338. }
  339. .corner{
  340. width: 0;
  341. height: 0;
  342. position: absolute;
  343. top: 34px;
  344. left: 0;
  345. right: 0;
  346. margin: auto;
  347. border-bottom: 4px solid transparent;
  348. border-left: 4px solid transparent;
  349. border-right: 4px solid transparent;
  350. border-top: 6px solid #b58cff ;
  351. }
  352. .station-icon2 {
  353. width: 100px;
  354. height: 36px;
  355. line-height: 20px;
  356. border-radius: 50px;
  357. background-color: rgba(0, 185, 98, 100);
  358. text-align: center;
  359. display: flex;
  360. position: absolute;
  361. top: 191px;
  362. left: 168px;
  363. }
  364. .icon2-left{
  365. width: 36px;
  366. height: 36px;
  367. line-height: 36px;
  368. text-align: center;
  369. border: 1px solid rgba(0, 185, 98, 100);
  370. border-radius: 999px;
  371. background-color: #fff;
  372. color: #00b962;
  373. }
  374. .icon2-right{
  375. color: #ffffff;
  376. line-height: 14px;
  377. padding: 4px;
  378. }
  379. .corner2{
  380. width: 0;
  381. height: 0;
  382. position: absolute;
  383. top: 36px;
  384. left: 0;
  385. right: 0;
  386. margin: auto;
  387. border-bottom: 6px solid transparent;
  388. border-left: 6px solid transparent;
  389. border-right: 6px solid transparent;
  390. border-top: 8px solid #00b962 ;
  391. }
  392. .btn{
  393. margin-top: 32rpx;
  394. .get{
  395. border-radius: 50px;
  396. background: linear-gradient(90deg, rgba(0,171,91,1) 0%,rgba(0,209,66,1) 100%);
  397. color: rgba(255, 255, 255, 1);
  398. font-size: 16px;
  399. text-align: center;
  400. }
  401. }
  402. // 尾部
  403. .botton {
  404. border: 1px solid rgba(242, 242, 242, 1);
  405. background: linear-gradient(180deg, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
  406. position: fixed;
  407. //background: #fff;
  408. width: 100%;
  409. padding: 24rpx 40rpx;
  410. line-height: 88rpx;
  411. bottom: 0;
  412. left: 0;
  413. z-index: 999;
  414. //padding: 0px 32rpx 32rpx;
  415. padding-bottom: 12px;
  416. button {
  417. margin-left: 40rpx;
  418. display: flex;
  419. align-items: center;
  420. justify-content: center;
  421. //width: 45.8%;
  422. // background-color: black;
  423. // color: #fff;
  424. // background-color: rgba(255, 255, 255, 1);
  425. // color: rgba(0, 185, 98, 1);
  426. background-color: rgba(0, 185, 98, 100);
  427. color: rgba(255, 255, 255, 100);
  428. height: 88rpx;
  429. border-radius: 50px;
  430. font-size: 32rpx;
  431. }
  432. }
  433. </style>