directive.js 2.98 KB
(function(angular) {
    var app = angular.module('batmanDelivers');
    app.directive("addressSearch", addressSearch);
    addressSearch.$inject = ['$parse', '$rootScope', '$http', '$timeout', '$sce']

    function addressSearch($parse, $rootScope, $http, $timeout, $sce) {
        return {
            templateUrl: '/src/js/directives/address-search/partial.html',
            replace: true, //study
            transclude: true, //study
            link: function(scope, elm, attrs) {

                scope.placeholder = attrs["placeholder"];
                scope.address = $parse(attrs['model'])(scope);
                scope.order = $parse(attrs['order'])(scope);
                scope.lbl = $parse(attrs['lbl'])(scope);



                scope.request = function(value){
                  if (value && value.length > 3) {
                      $http({
                          dataType: 'jsonp',
                          method: "GET",
                          url: "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + value + "&key=AIzaSyAPYatPrCcmNcVdAO_MF0YhO3c1mgdLvuQ"
                      }).then(function mySuccess(response) {
                          scope.addresses = response.data.predictions;
                      }, function myError(response) {
                          scope.error = response.statusText;
                      });
                  }
                }

                scope.selectAddress = function(place) {
                    $http({
                        method: "GET",
                        url: "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + place.place_id + "&fields=geometry,adr_address,address_component,formatted_address&key=AIzaSyAPYatPrCcmNcVdAO_MF0YhO3c1mgdLvuQ"
                    }).then(function mySuccess(response) {
                        scope.result = response.data.result;

                        scope.stops[scope.order] = {
                            AddressLine1: scope.result.address_components[1].long_name,
                            AddressLine2: scope.result.address_components[2].long_name,
                            City: scope.result.address_components[3].long_name,
                            PostCode: scope.result.address_components[0].long_name,
                            Latitude: scope.result.geometry.location.lat,
                            Longitude: scope.result.geometry.location.lng,
                            Description: place.description,
                            changed: false
                        }

                    }, function myError(response) {
                        scope.error = response.statusText;
                    });
                }

                scope.addStop = function(){
                  scope.stops.push({
                    Description: "Start typing to search..."
                  })
                }
                scope.removeStop = function(index){
                  scope.stops.splice(index, 1);
                }
            }
        }
    }
})(angular);