Hi guys,
Just worked out how to make it as I was getting a bit desperate with the plugin so I just tried without it… In order to test the autocomplete I just
first npm install --save @types/googlemaps
Then I set up home.ts
import { Component, NgZone } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
GoogleAutocomplete: google.maps.places.AutocompleteService;
autocomplete: { input: string; };
autocompleteItems: any[];
location: any;
placeid: any;
constructor(
public zone: NgZone,
) {
this.GoogleAutocomplete = new google.maps.places.AutocompleteService();
this.autocomplete = { input: '' };
this.autocompleteItems = [];
}
updateSearchResults(){
if (this.autocomplete.input == '') {
this.autocompleteItems = [];
return;
}
this.GoogleAutocomplete.getPlacePredictions({ input: this.autocomplete.input },
(predictions, status) => {
this.autocompleteItems = [];
this.zone.run(() => {
predictions.forEach((prediction) => {
this.autocompleteItems.push(prediction);
});
});
});
}
selectSearchResult(item) {
console.log(item)
this.location = item
this.placeid = this.location.place_id
console.log('placeid'+ this.placeid)
}
GoTo(){
return window.location.href = 'https://www.google.com/maps/place/?q=place_id:'+this.placeid;
}
}
And just a simple home.page.html to test it…
<ion-header>
<ion-toolbar color="primary">
<ion-searchbar [(ngModel)]="autocomplete.input" (ionInput)="updateSearchResults()" placeholder="Search for a place"></ion-searchbar>
</ion-toolbar>
<ion-list [hidden]="autocompleteItems.length == 0">
<ion-item *ngFor="let item of autocompleteItems" tappable (click)="selectSearchResult(item)">
{{ item.description }}
</ion-item>
</ion-list>
</ion-header>
<ion-content>
<ion-button (click)="GoTo()" >Go To Selected Location</ion-button>
</ion-content>
Don’t forget to put your API in index.html, based on source -> https://ionicthemes.com/tutorials/about/ionic-2-google-maps-google-places-geolocation
And yes, code is not brilliant and not optimised but I prefered to be redundant so it’s easier to follow.