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

    function addressSearch($parse, $rootScope, $http, $timeout, $sce, $rootScope) {
        return {
            templateUrl: '/src/js/directives/address-search/partial.html',
            scope: {
                address: '=model'
            },
            link: function (scope, elm, attrs) {
                scope.placeholder = attrs["placeholder"];
                scope.selected = {
                    address: null
                };

                scope.order = $parse(attrs['order'])(scope);
                scope.lbl = $parse(attrs['lbl'])(scope);

                var autocompleteService = new google.maps.places.AutocompleteService();
                var placesService = new google.maps.places.PlacesService($rootScope.map);

                scope.request = function (value) {
                    if (value && value.length > 3) {
                        autocompleteService.getPlacePredictions({
                            input: value
                        }, function (results) {
                            scope.addresses = results;
                            scope.$apply();
                        });
                    }
                }

                scope.$watch('selected.address', function (newvalue) {
                    if (newvalue && newvalue.place_id) {
                        placesService.getDetails({
                            placeId: newvalue.place_id
                        }, function (result) {
                            scope.result = result;
                            scope.address.AddressLine1 = scope.result.address_components[1].long_name;
                            scope.address.AddressLine2 = scope.result.address_components[2].long_name;
                            scope.address.City = scope.result.address_components[3].long_name;
                            scope.address.PostCode = scope.result.address_components[0].long_name;
                            scope.address.Latitude = scope.result.geometry.location.lat();
                            scope.address.Longitude = scope.result.geometry.location.lng();
                            scope.address.Description = newvalue.description;
                            scope.address.valid = true;


                            if (!scope.address.$marker) {
                                scope.address.$marker = new google.maps.Marker({
                                    map: $rootScope.map,
                                    position: scope.result.geometry.location,
                                    icon: '.././includes/images/c1.png'
                                });
                            } else {
                                scope.address.$marker.setPosition({ lat: scope.result.geometry.location.lat(), lng: scope.result.geometry.location.lng() });
                            }
                            scope.$apply();
                            scope.$emit('StopChanged');
                        });
                    }
                });
            }
        }
    }
})(angular);