Javascript 如何从goole.maps.places API getPlacePredictions发出回调?

Javascript 如何从goole.maps.places API getPlacePredictions发出回调?,javascript,Javascript,我正试图提出一个谷歌地图API函数,但似乎没有成功。调用我的函数时,我得到了一个无法读取未定义的属性“then” 我试着按照这篇文章中的例子来做,但没有成功: 回调函数如下所示: predictionService = new google.maps.places.AutocompleteService(); predictionService.getPlacePredictions( { input: '1 main street, south bend' }, displa

我正试图提出一个谷歌地图API函数,但似乎没有成功。调用我的函数时,我得到了一个
无法读取未定义的属性“then”

我试着按照这篇文章中的例子来做,但没有成功:

回调函数如下所示:

predictionService = new google.maps.places.AutocompleteService();
predictionService.getPlacePredictions(
    { input: '1 main street, south bend' }, 
    displayPredictionSuggestionsCallback 
);

function displayPredictionSuggestionsCallback( input ){
    // output results
}
predictionService = new google.maps.places.AutocompleteService();

function getPredictionSuggestion ( input ){

    var dfd = $.Deferred();

    predictionService.getPlacePredictions( { 
        input: input
    }, function (place, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            return dfd.reject("Request failed: " + status);
        }
        dfd.resolve( place ).promise();
    });
}
我的承诺是这样的:

predictionService = new google.maps.places.AutocompleteService();
predictionService.getPlacePredictions(
    { input: '1 main street, south bend' }, 
    displayPredictionSuggestionsCallback 
);

function displayPredictionSuggestionsCallback( input ){
    // output results
}
predictionService = new google.maps.places.AutocompleteService();

function getPredictionSuggestion ( input ){

    var dfd = $.Deferred();

    predictionService.getPlacePredictions( { 
        input: input
    }, function (place, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            return dfd.reject("Request failed: " + status);
        }
        dfd.resolve( place ).promise();
    });
}
这是调用服务的函数:

    getPredictionSuggestion( '1 main street, south bend' ).then( function(results) {
        console.log( 'promise results = ' + results );
    }, function( err ) {
        alert(err);
    });

大多数事情你都做对了。需要改变的事情:

  • 您需要从您的函数中返回承诺。添加
    返回dfd.promise()在末尾

  • 您不需要在回调中返回
    return
    ,只需返回
    else

  • 您不需要在
    解决
    呼叫中使用
    .promise()

  • 因此:


    这是jQuery的
    延迟的
    。您可能希望使用本机
    Promise
    ,如果需要,可以使用polyfill:

    predictionService = new google.maps.places.AutocompleteService();
    
    function getPredictionSuggestion(input) {
        return new Promise(function(resolve, reject) {
            predictionService.getPlacePredictions( { 
                input: input
            }, function (place, status) {
                if (status != google.maps.places.PlacesServiceStatus.OK) {
                    reject(new Error(status));
                } else {
                    resolve(place);
                }
            });
        });
    }
    

    注意
    新错误
    拒绝
    的用法。一般来说,将
    Error
    与reject一起使用非常有用,因为它提供了上下文(发生“错误”的地方)。

    大多数事情都做对了。需要改变的事情:

  • 您需要从您的函数中返回承诺。添加
    返回dfd.promise()在末尾

  • 您不需要在回调中返回
    return
    ,只需返回
    else

  • 您不需要在
    解决
    呼叫中使用
    .promise()

  • 因此:


    这是jQuery的
    延迟的
    。您可能希望使用本机
    Promise
    ,如果需要,可以使用polyfill:

    predictionService = new google.maps.places.AutocompleteService();
    
    function getPredictionSuggestion(input) {
        return new Promise(function(resolve, reject) {
            predictionService.getPlacePredictions( { 
                input: input
            }, function (place, status) {
                if (status != google.maps.places.PlacesServiceStatus.OK) {
                    reject(new Error(status));
                } else {
                    resolve(place);
                }
            });
        });
    }
    

    注意
    新错误
    拒绝
    的用法。一般来说,将
    Error
    与reject一起使用非常有用,因为它提供了上下文(发生“错误”的地方)。

    你是一个生命救世主。你是一个生命救世主。