【问题标题】:geocoder.geocode results cannot be accessed from outside variables无法从外部变量访问 geocoder.geocode 结果
【发布时间】:2020-06-08 11:45:25
【问题描述】:

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    public geocoderResults: any[] = [];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多