5
getAddress( lat: number, lng: number ) {
    console.log('Finding Address');
    if (navigator.geolocation) {
      let geocoder = new google.maps.Geocoder();
      let latlng = new google.maps.LatLng(lat, lng);
      let request = { latLng: latlng };
      geocoder.geocode(request, (results, status) => {
        if (status === google.maps.GeocoderStatus.OK) {
          let result = results[0];
          let rsltAdrComponent = result.address_components;
          let resultLength = rsltAdrComponent.length;
          if (result != null) {
            this.address = rsltAdrComponent[resultLength - 8].short_name;
          } else {
            alert('No address available!');
          }
        }
      });
  }
  }

I am trying to use reverse geocoding in my angular 4 application. I am using agm for integrating google map with the angular app.

Declared google variable in *.ts as follows

declare let google: any;

But when I am using I am getting the error as follows,

ERROR ReferenceError: google is not defined

Please help me to resolve the issue.

Here is the component.ts

declare let google: any;
@Component({
  selector: 'app-find-cycle',
  templateUrl: './cmp.component.html',
  styleUrls: ['./cmp.component.scss']
})

export class Cmp {
}
7
  • Hope you have declared declare let google: any; above component decorator? Commented May 10, 2018 at 11:45
  • Yes, I did that Commented May 10, 2018 at 11:56
  • Included the google maps api right.? Commented May 10, 2018 at 12:03
  • you need to use the map loaded function, and call getAddress() in there, google is not loaded before Commented May 10, 2018 at 12:04
  • @Ruben, Can you please provide me an example? Commented May 10, 2018 at 12:06

1 Answer 1

6

execute getAddress() after the map is loaded:

<agm-map (mapReady)="mapReady()" [latitude]="lat" [longitude]="lng" [zoom]="zoom" [styles]="styles">

mapReady() {
    this.getAddress(...);
}

or add private mapsAPILoader: MapsAPILoader, to your constructor do this

 this.mapsAPILoader.load().then(() => { ... });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I got it. I was missing the loading of agm-map into my component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.