1

how would I angularize this js function? I need it to use asynchronously. my understanding is .then and angular can do that.

    function showResult(result) {
        var lat = result.geometry.location.lat();
        var long = result.geometry.location.lng();
        $scope.vm.showroom.Longitude = long;
        $scope.vm.showroom.Latitude = lat;

    }

    function GetLatitudeLongitude(callback, address) {
        var address1 = address || '234 eisenhower avenue salt lake city';
        // Initialize the Geocoder
        var geocoder = new google.maps.Geocoder();
        if (geocoder) {
            geocoder.geocode({
                'address': address1
            }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    callback(results[0]);
                }
            });
        }        
    }
1

1 Answer 1

5

You can do something like this:

    function GetLatitudeLongitude(address) {
        address = address || '234 eisenhower avenue salt lake city';

        var deferred = $q.defer();
        // Initialize the Geocoder
        var geocoder = new google.maps.Geocoder();

        if (!geocoder) {
            deferred.reject('No geocoder available.');
            return deferred.promise;
        }

        geocoder.geocode({
            'address': address
        }, function (results, status) {
            if (status !== google.maps.GeocoderStatus.OK) {
                return deferred.reject('Geocoder status error.');
            }

            deferred.resolve(results[0]);
        });

        return deferred.promise;
    }

and then you can call that function like this:

GetLatitudeLongitude(address).then(function (result) {
    var lat = result.geometry.location.lat();
    var long = result.geometry.location.lng();
    $scope.vm.showroom.Longitude = long;
    $scope.vm.showroom.Latitude = lat;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.