geocoder.geocode 无法从外部变量访问结果

geocoder.geocode results cannot be accessed from outside variables

我正在尝试获取 google.maps.GeocodeResults 以便我可以访问我的地址的经度和纬度,以便我可以将其推送到外部 Array<any> 变量。

下面,我可以记录 results[0],但是当我尝试将其推送到数组进行存储时,我得到状态 OVER_QUERY_LIMIT,这对我来说没有意义因为我已经有了价值。

 public geocoderResults!: Array<any>;

 public addCodedAddress(address: string): void {
        let geocoder = new google.maps.Geocoder();
        geocoder.geocode({ address: address }, (results, status) => {
            if (status == google.maps.GeocoderStatus.OK && results[0]) {
                console.log('this does have something in it', results[0]);
                this.geocoderResults.push(results[0]);
                console.log(
                    'this should have something in it, but doesnt ',
                    this.geocoderResults,
                );
            } else {
                console.warn(
                    'Geocode was not successful for the following reason: ' +
                        status,
                );
            }
        });
    }

我如何访问这些 GeocodeResults 以便获取它们的经度/纬度?

谢谢!

你实际上应该初始化你的 geocoderResults class 字段:

public geocoderResults: any[] = [];