import { identifierModuleUrl } from '@angular/compiler/compiler'; import { Component, NgZone, ViewChild, ElementRef } from '@angular/core'; import { ActionSheetController, AlertController, App, LoadingController, NavController, Platform, ToastController } from 'ionic-angular'; import { Geolocation } from '@ionic-native/geolocation'; import { IonicStorageModule } from '@ionic/storage'; import { Observable } from 'rxjs/Observable'; import { Storage } from '@ionic/storage'; declare var google: any; @Component({ selector: 'page-location', templateUrl: 'location.html', }) export class LocationPage { @ViewChild('searchbar', { read: ElementRef }) searchbar: ElementRef; addressElement: HTMLInputElement = null; listSearch: string = ''; map: any; marker: any; loading: any; search: boolean = false; error: any; switch: string = "map"; regionals: any = []; currentregional: any; constructor( public loadingCtrl: LoadingController, public toastCtrl: ToastController, public app: App, public nav: NavController, public zone: NgZone, public platform: Platform, public alertCtrl: AlertController, public storage: Storage, public actionSheetCtrl: ActionSheetController, public geolocation: Geolocation ) { this.platform.ready().then(() => this.loadMaps()); this.regionals = [{ "title": "Pizza Hut", "latitude": 51.8971373, "longitude": -0.4630553, }, { "title": "Domino's", "latitude": 51.8977721, "longitude": -0.3915003, }, { "title": "The Vision Shop", "latitude": 51.8938226, "longitude": -0.415054, }, { "title": "Perigonas", "latitude": 51.5273851, "longitude": -0.6166673, }, { "title": "Keech Hospice Care", "latitude": 51.9137413, "longitude": -0.4319927, }]; } viewPlace(id) { console.log('Clicked Marker', id); } loadMaps() { if (!!google) { this.initializeMap(); } else { this.errorAlert('Error', 'Try again later.') } } errorAlert(title, message) { let alert = this.alertCtrl.create({ title: title, message: message, buttons: [ { text: 'OK', handler: data => { this.loadMaps(); } } ] }); alert.present(); } createAutocomplete(addressEl: HTMLInputElement): Observable { const autocomplete = new google.maps.places.Autocomplete(addressEl); autocomplete.bindTo('bounds', this.map); return new Observable((sub: any) => { google.maps.event.addListener(autocomplete, 'place_changed', () => { const place = autocomplete.getPlace(); if (!place.geometry) { sub.error({ message: 'Autocomplete returned place with no geometry' }); } else { console.log('Search Lat', place.geometry.location.lat()); console.log('Search Lng', place.geometry.location.lng()); sub.next(place.geometry.location); //sub.complete(); } }); }); } initializeMap() { this.zone.run(() => { var mapEle = this.mapElement.nativeElement; this.map = new google.maps.Map(mapEle, { zoom: 10, center: { lat: 51.8816726 , lng: -0.4180509 }, mapTypeId: google.maps.MapTypeId.ROADMAP, styles: [{ "featureType": "water", "elementType": "geometry", "stylers": [{ "color": "#e9e9e9" }, { "lightness": 17 }] }, { "featureType": "landscape", "elementType": "geometry", "stylers": [{ "color": "#f5f5f5" }, { "lightness": 20 }] }, { "featureType": "road.highway", "elementType": "geometry.fill", "stylers": [{ "color": "#ffffff" }, { "lightness": 17 }] }, { "featureType": "road.highway", "elementType": "geometry.stroke", "stylers": [{ "color": "#ffffff" }, { "lightness": 29 }, { "weight": 0.2 }] }, { "featureType": "road.arterial", "elementType": "geometry", "stylers": [{ "color": "#ffffff" }, { "lightness": 18 }] }, { "featureType": "road.local", "elementType": "geometry", "stylers": [{ "color": "#ffffff" }, { "lightness": 16 }] }, { "featureType": "poi", "elementType": "geometry", "stylers": [{ "color": "#f5f5f5" }, { "lightness": 21 }] }, { "featureType": "poi.park", "elementType": "geometry", "stylers": [{ "color": "#dedede" }, { "lightness": 21 }] }, { "elementType": "labels.text.stroke", "stylers": [{ "visibility": "on" }, { "color": "#ffffff" }, { "lightness": 16 }] }, { "elementType": "labels.text.fill", "stylers": [{ "saturation": 36 }, { "color": "#333333" }, { "lightness": 40 }] }, { "elementType": "labels.icon", "stylers": [{ "visibility": "off" }] }, { "featureType": "transit", "elementType": "geometry", "stylers": [{ "color": "#f2f2f2" }, { "lightness": 19 }] }, { "featureType": "administrative", "elementType": "geometry.fill", "stylers": [{ "color": "#fefefe" }, { "lightness": 20 }] }, { "featureType": "administrative", "elementType": "geometry.stroke", "stylers": [{ "color": "#fefefe" }, { "lightness": 17 }, { "weight": 1.2 }] }], disableDoubleClickZoom: false, disableDefaultUI: true, zoomControl: true, scaleControl: true, }); let markers = []; for (let regional of this.regionals) { regional.distance = 0; regional.visible = false; regional.current = false; let markerData = { position: { lat: regional.latitude, lng: regional.longitude }, map: this.map, title: regional.title, }; regional.marker = new google.maps.Marker(markerData); markers.push(regional.marker); regional.marker.addListener('click', () => { for (let c of this.regionals) { c.current = false; } this.currentregional = regional; regional.current = true; this.map.panTo(regional.marker.getPosition()); }); } google.maps.event.addListenerOnce(this.map, 'idle', () => { google.maps.event.trigger(this.map, 'resize'); mapEle.classList.add('show-map'); this.bounceMap(markers); this.getCurrentPositionfromStorage(markers) }); google.maps.event.addListener(this.map, 'bounds_changed', () => { this.zone.run(() => { this.resizeMap(); }); }); }); } bounceMap(markers) { let bounds = new google.maps.LatLngBounds(); for (var i = 0; i < markers.length; i++) { bounds.extend(markers[i].getPosition()); } this.map.fitBounds(bounds); } resizeMap() { setTimeout(() => { google.maps.event.trigger(this.map, 'resize'); }, 200); } getCurrentPositionfromStorage(markers) { this.storage.get('lastLocation').then((result) => { if (result) { let myPos = new google.maps.LatLng(result.lat, result.long); this.map.setOptions({ center: myPos, zoom: 14 }); let marker = this.addMarker(myPos, "My last saved Location: " + result.location); markers.push(marker); this.bounceMap(markers); this.resizeMap(); } }); } showToast(message) { let toast = this.toastCtrl.create({ message: message, duration: 3000 }); toast.present(); } choosePosition() { this.storage.get('lastLocation').then((result) => { if (result) { let actionSheet = this.actionSheetCtrl.create({ title: 'Last Location: ' + result.location, buttons: [ { text: 'Reload', handler: () => { this.getCurrentPosition(); } }, { text: 'Delete', handler: () => { this.storage.set('lastLocation', null); this.showToast('Location deleted!'); this.initializeMap(); } }, { text: 'Cancel', role: 'cancel', handler: () => { } } ] }); actionSheet.present(); } else { this.getCurrentPosition(); } }); } getCurrentPosition() { this.loading = this.loadingCtrl.create({ content: 'Searching Location ...' }); this.loading.present(); let locationOptions = { timeout: 10000, enableHighAccuracy: true }; this.geolocation.getCurrentPosition(locationOptions).then( (position) => { this.loading.dismiss().then(() => { this.showToast('Location found!'); console.log(position.coords.latitude, position.coords.longitude); let myPos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); let options = { center: myPos, zoom: 14 }; this.map.setOptions(options); this.addMarker(myPos, "Your Location!"); let alert = this.alertCtrl.create({ title: 'Location', message: 'Do you want to save the Location?', buttons: [ { text: 'Cancel' }, { text: 'Save', handler: data => { let lastLocation = { lat: position.coords.latitude, long: position.coords.longitude }; console.log(lastLocation); this.storage.set('lastLocation', lastLocation).then(() => { this.showToast('Location saved'); }); } } ] }); alert.present(); }); }, (error) => { this.loading.dismiss().then(() => { this.showToast('Location not found. Please enable your GPS!'); console.log(error); }); } ) } toggleSearch() { if (this.search) { this.search = false; } else { this.search = true; } } addMarker(position, content) { let marker = new google.maps.Marker({ map: this.map, animation: google.maps.Animation.DROP, position: position }); this.addInfoWindow(marker, content); return marker; } addInfoWindow(marker, content) { let infoWindow = new google.maps.InfoWindow({ content: content }); google.maps.event.addListener(marker, 'click', () => { infoWindow.open(this.map, marker); }); } }