Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let mapSelectComponent = {
  2.     bindings: {
  3.         profile: '<',
  4.         tag: '<',
  5.         selectAddress: '&'
  6.     },
  7.     controller: function (GoogleMapService) {
  8.         'ngInject';
  9.         this.gmap = GoogleMapService;
  10.         this.map = {};
  11.         this.geocoder = new google.maps.Geocoder;
  12.         this.address = {};
  13.  
  14.         this.init = () => {
  15.           // Initializing our map
  16.           this.gmap.init().then(res => {
  17.               this.setMarkerListener(this.gmap.marker);
  18.               return this.map = res.map;
  19.           });
  20.  
  21.           // Setting up autocomplete
  22.           this.input = document.querySelector('#input');
  23.           this.autocomplete = new google.maps.places.Autocomplete(this.input);
  24.           this.autocomplete.bindTo('boundsTo', this.map);
  25.  
  26.           this.autocomplete.addListener('place_changed', () => {
  27.              this.gmap.marker.setVisible(false);
  28.              // Getting the place from autocomplete
  29.              let place = this.autocomplete.getPlace();
  30.              // Moving the map center to place we selected and placing a marker there
  31.              this.gmap.changeLocation(place);
  32.              this.formAddressObject(place);
  33.           });
  34.         };
  35.  
  36.         /**
  37.          * Sets a listener on google maps marker drag event
  38.          * Finds and reverse geocodes the results
  39.          * And forms an object to pass to the parent component
  40.          * From where it is sent to server
  41.          * @param {Object} marker
  42.          */
  43.         this.setMarkerListener = (marker) => {
  44.             // Setting a listener on our marker drag event
  45.             google.maps.event.addListener(marker, 'dragend', (evt) => {
  46.                 // Getting the coordinates of drag end point
  47.                 let latlng = {
  48.                     lat: evt.latLng.lat(),
  49.                     lng: evt.latLng.lng()
  50.                 };
  51.                 // Reverse geocoding the coordinates to get full featured place with address and other properties
  52.                 this.geocoder.geocode({'location': latlng }, (results, status) => {
  53.                     // We get an array consisting of our selected place and places nearby, so we need the first element
  54.                     let place = results[0];
  55.                     this.formAddressObject(place)
  56.                 });
  57.             });
  58.         };
  59.  
  60.         /**
  61.          * Forms an object that we pass to parent component from the data we get from the map
  62.          * @param {Object} place
  63.          * @returns {{Tag, Coordinates: {Latitude, Longtitute}, ProfileId, LocationName: string}}
  64.          */
  65.         this.formAddressObject = (place) => {
  66.             return this.address = {
  67.                 Tag: this.tag,
  68.                 Coordinates: {
  69.                     Latitude: place.geometry.location.lat(),
  70.                     Longtitute: place.geometry.location.lng()
  71.                 },
  72.                 ProfileId: this.profile.ProfileId,
  73.                 LocationName: `${place.address_components[1].short_name}, ${place.address_components[0].long_name}`
  74.             };
  75.         };
  76.  
  77.         this.init();
  78.     },
  79.     template: `
  80.         <div class="map">
  81.             <input type="text" id="input" class="form-control">
  82.             <map></map>
  83.             <button ng-click="$ctrl.selectAddress({location: $ctrl.address})"></button>
  84.         </div>
  85.     `
  86. };
  87.  
  88. export default mapSelectComponent;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement