簡體   English   中英

無法從外部變量訪問 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[] = [];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM