1

I want to use an Angular 2 Google map autocomplete and I found this directive.

When I tried to use it, it gives me this error:

errors.ts:42 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'Autocomplete' of undefined

I dont know if I missed something. Anyway, here's the code of the directive:

import {Directive, ElementRef, EventEmitter, Output} from '@angular/core';
import {NgModel} from '@angular/forms';

declare var google:any;

@Directive({
selector: '[Googleplace]',
providers: [NgModel],
host: {
'(input)' : 'onInputChange()'
}
})
export class GoogleplaceDirective {

@Output() setAddress: EventEmitter<any> = new EventEmitter();
modelValue:any;
 autocomplete:any;
private _el:HTMLElement;


constructor(el: ElementRef,private model:NgModel) {
this._el = el.nativeElement;
this.modelValue = this.model;
var input = this._el;

 this.autocomplete = new google.maps.places.Autocomplete(input, {});
 google.maps.event.addListener(this.autocomplete, 'place_changed', ()=> {
  var place = this.autocomplete.getPlace();
  this.invokeEvent(place);

});
 }

  invokeEvent(place:Object) {
  this.setAddress.emit(place);
}

 onInputChange() {
console.log(this.model);
}
 }

Here's how to use it:

<input type="text" class="form-control" placeholder="Location" name="Location" [(ngModel)]="address" #LocationCtrl="ngModel"
    Googleplace (setAddress)="getAddressOnChange($event,LocationCtrl)">
7
  • Are you sure that google.maps.places is defined ? Commented Aug 23, 2017 at 12:27
  • thanks for for answer but i just copy the code as it is Commented Aug 23, 2017 at 12:28
  • i have to install dependencies?? Commented Aug 23, 2017 at 12:28
  • You should try to console.log(google.maps) and see if it own a places attribute before this line : this.autocomplete = new google.maps.places.Autocomplete(input, {}); Commented Aug 23, 2017 at 12:30
  • it gives me this:{"Animation":{"BOUNCE":1,"DROP":2,"Zo":3,"Uo":4},"ControlPosition":{"TOP_LEFT":1,"TOP_CENTER":2,"TOP":2,"TOP_RIGHT":3,"LEFT_CENTER":4,"LEFT_TOP":5,"LEFT":5,"LEFT_BOTTOM":6,"RIGHT_TOP":7,"RIGHT":7...................(the object is so long) Commented Aug 23, 2017 at 12:32

2 Answers 2

13

If you are using google maps you have to import the Api in the ngmodule like this:

@NgModule({
  declarations: [...],
  imports: [...,
    AgmCoreModule.forRoot({
      clientId: '<mandatory>',
      //apiVersion: 'xxx', // optional
      //channel: 'yyy', // optional
      //apiKey: 'zzz', // optional
      language: 'en',
      libraries: ['geometry', 'places']
    })
  ],
  providers: [...],
  bootstrap: [...]
})

the library 'places' is needed to use the autocomplete module.

Than use it like this:

    import {MapsAPILoader} from "@agm/core";
    ...
    constructor(private mapsAPILoader: MapsAPILoader,
    ...
        this.mapsAPILoader.load().then(() => {
          let autocomplete = new window['google'].maps.places.Autocomplete(..., ...);

          autocomplete.addListener("place_changed", () => { ...

You can take a look here: Angular 2 + Google Maps Places Autocomplete

Sign up to request clarification or add additional context in comments.

Comments

3

This approach helped me to get the rid of this error. Just change the google maps api import to like this:

https://maps.googleapis.com/maps/api/js?key=YOURAPIKEY

Then add &libraries=places to the end of URL so it looks like this:

<script async defer
        src="https://maps.googleapis.com/maps/api/js?key=YOURAPIKEY&libraries=places">
</script>

Comments

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.