简体   繁体   中英

geocoder.geocode results cannot be accessed from outside variables

I am trying to grab google.maps.GeocodeResults so that i can access my address's longitude and latitude so that I can push it to an outside Array<any> variable.

Below, I am able to log results[0] , but when i try to push it to an array for storage, i am given the status OVER_QUERY_LIMIT , which doesn't make sense to me since i already have the value.

 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,
                );
            }
        });
    }

How can i access these GeocodeResults so i can grab their long / lat?

Thanks!

You should actually initialize your geocoderResults class field:

public geocoderResults: any[] = [];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM