Advertisement
krot

vue-yandex-maps

Dec 21st, 2020
2,691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.  
  3. <div >
  4.                      
  5.                        
  6.                         <input name="f_addr" :value="addrMy" >
  7.                        
  8.                         <div id="mapid" class="map-col">
  9.                             <yandex-map
  10.                               @click="onClick"
  11.                             :coords="coordsMy"
  12.                             zoom=15
  13.                             :controls="controls"
  14.                             :options="options"
  15.                            
  16.                         >
  17.                                     <ymap-marker  
  18.                                     marker-type="placemark"
  19.                                     marker-id="my"
  20.                                     :coords="coordsMy"
  21.                                     />
  22.                         </yandex-map>
  23.  
  24.                        
  25.                         </div>
  26.  
  27.                     </div>
  28. </template>
  29. <script>
  30. import { loadYmap } from 'vue-yandex-maps';
  31. export default {
  32.     props: {
  33.       coords:Array,
  34.       id:Number,
  35.       addr:String
  36.       },
  37.     components:{
  38.      loadYmap
  39.     },
  40.  data() {
  41.       return {
  42.    
  43.  
  44.      controls:["zoomControl"],
  45.      options:{
  46.        suppressMapOpenBlock: true,
  47.          yandexMapDisablePoiInteractivity:true
  48.        },
  49.      addrMy:null,
  50.      noResults:false,
  51.      focused:false,
  52.      coordsMy:[1,1],
  53.      mapShow:false,
  54.   }
  55.  },
  56.    async fetch() {
  57.      if(this.coords.length===0){
  58.        let geo=await this.$store.getters['getAuthGeo'];
  59.        if(geo.geo_lat){
  60.          this.coordsMy=[geo.geo_lat, geo.geo_lng];
  61.        }else{
  62.          this.coordsMy=[55.753595,37.621031];
  63.        }
  64.      }
  65.     },
  66.     created () {
  67.       this.addrMy=this.addr
  68.       this.setMarker(this.coords);
  69.     },
  70.      async mounted(){
  71.         if(process.client){
  72.           let self=this;
  73.      
  74.             let ymapsettings= {
  75.                        lang:this.$i18n.locale,
  76.                         apiKey: process.env.YAMAP_KEY,
  77.                          version: '2.1'
  78.                          };
  79.                    
  80.                   loadYmap({...ymapsettings}).then(function () {
  81.                             window. console.log('ymaps load ' );
  82.                             let suggestView = new ymaps.SuggestView('f_addr');
  83.                             suggestView.events.add('select', function (event) {
  84.                                 self.addrMy=(event.get('item').value);
  85.                                  window.ymaps.geocode(
  86.                                             self.addrMy
  87.                                         ).then((obj) =>
  88.                                         self.coordsMy=(obj.geoObjects.get(0).geometry.getCoordinates())
  89.                                         );          
  90.                             });
  91.                     },
  92.                     function (e) {
  93.                         window. console.error('ymaps return error: ' + e);
  94.                         self.coordsMy=[];
  95.                   });
  96.             //}  
  97.  
  98.         }
  99.      },
  100.    methods: {
  101.           setAddr:function(addr){
  102.                 this.addrMy=addr;
  103.           },
  104.           setMarker:function(coords){
  105.                 this.coordsMy=coords;
  106.                 this.mapShow=true;
  107.                 /* eventBus.$emit('changeGeo',  {
  108.                                                 'id':this.id,
  109.                                                 'coords':coords,
  110.                                                 'addr':this.addrMy
  111.                                                 }) ;*/
  112.                                                
  113.           },
  114.            onClick(e) {
  115.              let self=this;
  116.               this.setMarker(e.get('coords'));
  117.  
  118.                window.ymaps.geocode(
  119.                 this.coordsMy
  120.                ).then((obj) =>
  121.                self.geocode(obj)
  122.                );
  123.  
  124.  
  125.             },
  126.           geocode:function(obj){
  127.             this.addrMy=obj.geoObjects.get(0).getAddressLine();
  128.           },
  129.           onSubmit:function(result){
  130.                this.addrMy=result;
  131.                  window.ymaps.geocode(
  132.                      result
  133.                   ).then((obj) => this.setMarker(obj.geoObjects.get(0).geometry.getCoordinates()));  
  134.           },
  135.          
  136.  
  137.  
  138.  
  139.   }
  140.  
  141. }
  142. </script>
  143.  
  144.  <style scoped>
  145.  .map-col{
  146.  
  147.  height: 300px;
  148.   overflow: hidden;
  149.  }
  150. .map {
  151.   width: 100%;
  152.   height: 100%;
  153. }
  154.  
  155. .ymap-container {
  156.   height: 300px;
  157.   width: 100%;
  158. }
  159.  </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement