Reactjs React和Google';s自动完成服务和获取zipcodes

Reactjs React和Google';s自动完成服务和获取zipcodes,reactjs,google-places,Reactjs,Google Places,我对谷歌的自动完成定位服务有一些问题。有两种不同的方法,这两种方法都让我想到了一些问题: 选项1使用“反应位置自动完成”并在必要时应用扩展: handleInputChange(event) { this.props.inputProps.onChange(event.target.value) if (!event.target.value) { this.clearAutocomplete() return } this.autoc

我对谷歌的自动完成定位服务有一些问题。有两种不同的方法,这两种方法都让我想到了一些问题:

选项1使用“反应位置自动完成”并在必要时应用扩展:

  handleInputChange(event) {
    this.props.inputProps.onChange(event.target.value)
    if (!event.target.value) {
      this.clearAutocomplete()
      return
    }
    this.autocompleteService.getPlacePredictions({ ...this.props.options, input: event.target.value }, this.autocompleteCallback)
  }
上面用预测调用了我的autocompleteCallback方法,这很好。我有街道地址、城市、州、国家,但没有邮政编码。也许我应该在选择一个预测后再打第二个电话,然后获取邮政编码信息?不过,我还没有看到谷歌会从adddress中获取邮政编码的场景

备选案文2A:

    method1(a1, a2, a3) {
        //do state things because method1 is bound in the constructor
    };
... later ....
    var input = document.getElementById('my-input-id');
    var options = {
        types: ['address'],
        componentRestrictions: {
            country: 'us'
        }
    };
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    google.maps.event.addListener(autocomplete, 'place_changed', this.method1);
备选方案2B

    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        place = autocomplete.getPlace();
        //call states
    })
我对选项2的问题第一部分是,尽管我们使用了方法1,我可以访问道具/状态,但变量autocommpleten超出了范围,因此我无法获取地址

我对选项2(第二部分)的问题是,我看不到访问addlistener方法内部状态的方法


我觉得我的选项1是最合理的解决方案,但出于某种原因,谷歌决定不包括邮政编码。此外,使用侦听器使我在react中感到不安。

我找到了一种更优雅的方法,它保留了使用父类“react places autocomplete”的能力 在我的handleselect中,我添加了以下代码:

    googleMapsClient.geocode({
        address: address
    }, this.method1);
其中googlemapsclient=require(“@google/maps”).createClient({ 键:“…” });

它调用了另一个不理想的异步方法,但它保留了我的其他信息