I am trying to implement google places model list in angular 6 with material, I am calling the google places API but I am getting CORS issue like below,
Failed to load https://maps.googleapis.com/maps/api/place/autocomplete/json?input=hy&language=en Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:4200' is therefore not allowed access.
my code is like below,
<mat-form-field>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selectedUserPlace($event)">
<mat-option *ngFor="let place of listOfPlaces" [value]="place.description">
{{place.description}}
</mat-option>
</mat-autocomplete>
<input type="text" aria-label="Number" required (input)="getUserPLaces($event.target.value)" matInput
[matAutocomplete]="auto">
<mat-form-field>
getUserPLaces(value: any) {
this.searchLocationService.loadUserPlaces(value, this.latitude, this.longitude).subscribe(
(data) => {
this.listOfPlaces = data['predictions'];
}
);
}
loadUserPlaces(key, latitude, longitude){
return this.httpClient.get(`https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${key}&language=en`);
}
How to resolve cors issue here, I know that by disabling chrome security features and some extensions we can avoid this issue. Since my application will go to production and i want to solve this issue through code.
Any solutions will be appreciated.
key=YOUR_API_KEY- Michael