-
Notifications
You must be signed in to change notification settings - Fork 818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using google places autocomplete with angular2-google-maps #782
Comments
+1 |
I am facing the same problem with my app. |
You need to inject the MapsAPILoader into your class, then call the load method. Until the loader is complete the google object will not be accessible. Example: constructor(private _mapsAPILoader: MapsAPILoader) { } ngAfterViewInit() { |
Yeah this the right way to go right now. We will improve it with #740. |
Is there any opportunity to set a language for MapsAPILoader? // Load Places Autocomplete
this.mapsAPILoader.load().then(() => {
let autocomplete = new google.maps.places.Autocomplete(this.searchInputRef.nativeElement, {
types: ['address']
});
autocomplete.addListener('place_changed', () => {
this.ngZone.run(() => {
// get the place result
let selectedPlace = autocomplete.getPlace();
// place.formatted_address => is in english, because I didn't setup it.
// Need to have opportunity for localization of formatted_address
});
});
}); UPDATE: One of the possible solutions - add language and region keys to the params object like: import { AgmCoreModule } from 'angular2-google-maps/core';
const googleMapsParams = {
apiKey: environment.GOOGLE_MAPS_API_KEY,
libraries: ['places'],
language: 'uk',
region: 'UA'
};
...
imports: [
BrowserModule,
...
AgmCoreModule.forRoot(googleMapsParams)
], |
Would be great if someone could provide a full example of autocomplete binding with model.address. Or maybe add an input to your Getting started example ? I can see a lot of changes in last 12 months, so most examples are now out of date. |
@clenoma That article was really helpful for me: http://brianflove.com/2016/10/18/angular-2-google-maps-places-autocomplete/ |
Very interested in this because I would go a step further than @clenoma and say, all examples are unfortunately out of date. I would like to get involved in this. |
My Google Maps API is now working. |
Unable to get the 'google' object, still undefined, compilation fails because it is not declared. |
|
In my proj we use g-map in directives and solve it this way: |
@yuzhva I used your solution to make the search places feature work but I'd like to use the received viewport to fit the bounds of my map depending on the result of the request. I can't do anything with the type |
The solution provided by @telenord works for me both locally and when building my application for production. Importing |
For the sake of completeness: import { Component, OnInit } from '@angular/core';
import { MapsAPILoader } from '@agm/core';
declare let google: any;
@Component({
selector: 'place-search',
templateUrl: './place-search.component.html'
})
export class PlaceSearchComponent implements OnInit {
placeService: any;
placeServiceIsReady = false;
constructor(private _loader: MapsAPILoader) { }
ngOnInit() {
this._loader.load().then(() => {
this.placeService = new google.maps.places.AutocompleteService();
this.placeServiceIsReady = true;
});
}
autoComplete() {
if (this.placeServiceIsReady) {
// ...do stuff with place service
}
}
} |
Issue description
Unable to get hold of the 'google' object.
Steps to reproduce and a minimal demo of the problem
I am currently working on an angular2 application which uses Google Maps.
I have embedded and instance of Google Maps into the app using the excellent sebm angular2-google-maps module.
The problem I now have is that I would like to use the google place autocomplete search bar.
I have imported the module into my map module like this:
Which works fine re the map itself. However all the documentation I can find ( for example this thread ) says that the 'google' object should exist globally now and that I should be able to get hold of it by simply declaring it in my component i.e.
When I try this though and then try to use the google object e.g.
... it gives me the error that google is undefined.
_Use https://plnkr.co or similar -- try this template as a starting point: http://plnkr.co/edit/YX7W20?p=preview
What steps should we try in your demo to see the problem?
Current behavior
google is undefined
Expected/desired behavior
the google object should be globally accessable
angular2 & angular2-google-maps version
0.16.0
Other information
The text was updated successfully, but these errors were encountered: