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

时间:2020-06-08 11:45:25

标签: javascript angular typescript google-maps google-geocoder

我正在尝试获取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,以便我可以获取它们的长号/经度?

谢谢!

1 个答案:

答案 0 :(得分:1)

您实际上应该初始化geocoderResults类字段:

public geocoderResults: any[] = [];
相关问题