2014-01-15 1 views
1

미리 작성된 Google 지역 정보 자동 완성에 대한 맞춤 UI를 구현하려고하므로 수동으로 결과를 선택할 수 없습니다. 모든 기능은 getPlacePredictions 함수 옵션에서 여러 유형을 사용하지 않을 때 작동하지만 [ '(regions)', '(cities)']를 사용하면 상태가 'invalid request'를 반환합니다.Google 장소에서 여러 유형을 반환하십시오. autocompleteService.getPlacePredictions

잘못된 것이 있습니까? 아니면 불가능합니다. 여러 유형을 반환하려면?

var _this = this; 

this.input = $('#zipcode_autocomplete'); 

this.service = new google.maps.places.AutocompleteService(); 

this.input.on('input', function() { 
    return _this.service.getPlacePredictions({ 
    input: _this.input.val(), 
    types: ['(regions)', '(cities)'], 
    componentRestrictions: { 
     country: 'us' 
    } 
    }, _this.callback); 
}); 

this.callback = function(predictions, status) { 
    var i, prediction, _results; 
    console.log(predictions); 
    if (status !== google.maps.places.PlacesServiceStatus.OK) { 
    alert(status); 
    return; 
    } 
    i = 0; 
    prediction = void 0; 
    this.results = $('ul.location_results').removeClass('hidden').html(''); 
    _results = []; 
    while (prediction = predictions[i]) { 
    this.results.append("<li><span class='location-address'>" + prediction.terms[0].value + "</span><span class='location-state'>" + prediction.terms[prediction.terms.length - 2].value + "</span></li>"); 
    _results.push(i++); 
    } 
    return _results; 
}; 
+0

[ '일반적으로 단지 하나의 유형이 허용됨 '] (https://developers.google.com/places/documentation/autocomplete#place_types). – Andy

+0

좋아, 그게 좌절이야, 페이지를 가리켜 주셔서 감사합니다, 나는 그것을 어디서나 찾을 수 없습니다. @ 앤디 만약 당신이 대답을 게시하고 싶다면 그것을 받아 들일 것입니다. – joewoodward

+0

done. 거기에 조금 더 많은 정보가 추가되었습니다. – Andy

답변

5

API 'In general only a single type is allowed'에 따르면.

당신은 당신이 할 수있는 것은, 프로세스를 관리하는 deferred object를 사용하여이 같은 개별적 두 가지 유형을 얻으려고한다면 다음 API에 따르면

// set a new promises array, set the types array 
var promises = [], types = ['(regions)', '(cities)']; 

// loop over the types and push the output of getPredications() for each one 
// into the promises array 
for (var i = 0, l = types.length; i < l; i++) { 
    promises.push(getPredictions(types[i])); 
} 

// when all promises have completed then do something 
// This uses jQuery's when method which can take an array of 
// promises when used with apply 
$.when.apply(null, promises).then(runThisFunction); 

function getPredictions(type) { 

    // Set up a new deferred object 
    var deferred = new $.Deferred(); 

    // Call the asynchronous function to get the place predictions 
    this.service.getPlacePredictions({ 
    input: _this.input.val(), 
    types: type, 
    componentRestrictions: { 
     country: 'us' 
    } 
    }, function (data) { 

    // Once the data has been returned perhaps do something with data 
    // but definitely resolve the deferred object.  
    deferred.resolve(); 
    }); 

    // return the promise 
    return deferred.promise(); 
} 
+0

나는 (내 자신의 출처와 Google에서 데이터를 가져와) 비슷한 일을하려고했는데, 그 해결책은 Google에서 그런 경우에 대해서만 하나입니다. 불행하게도 나는 이것이 정확히 어떻게 작동하는지 이해하는 데 어려움을 겪고있다. 조금 설명해 주시겠습니까? –

+1

@MichaelSzyndel, 몇 가지 메모를 추가했습니다. 자세한 내용은 jQuery의 ['deferred objects API'] (https://api.jquery.com/category/deferred-object/) 문서를보십시오. – Andy

관련 문제