From 1b75ccfb407deef31b8997b1fb2695199c22743b Mon Sep 17 00:00:00 2001 From: eugene-sinitsyn Date: Wed, 4 Mar 2020 11:26:26 +0300 Subject: [PATCH] Fix search-map component using @angular/google-maps --- src/app/pages/maps/gmaps/gmaps.component.ts | 2 +- .../pages/maps/search-map/entity/Location.ts | 4 -- .../maps/search-map/entity/position.model.ts | 6 +++ .../maps/search-map/map/map.component.html | 10 ++-- .../maps/search-map/map/map.component.ts | 22 +++++---- .../maps/search-map/search-map.component.html | 4 +- .../maps/search-map/search-map.component.ts | 9 ++-- .../search-map/search/search.component.ts | 48 +++++++++---------- .../smart-table/smart-table.component.ts | 10 ++-- src/index.html | 2 +- 10 files changed, 56 insertions(+), 61 deletions(-) delete mode 100644 src/app/pages/maps/search-map/entity/Location.ts create mode 100644 src/app/pages/maps/search-map/entity/position.model.ts diff --git a/src/app/pages/maps/gmaps/gmaps.component.ts b/src/app/pages/maps/gmaps/gmaps.component.ts index c60a3650..51da4857 100644 --- a/src/app/pages/maps/gmaps/gmaps.component.ts +++ b/src/app/pages/maps/gmaps/gmaps.component.ts @@ -3,7 +3,7 @@ import { Component } from '@angular/core'; @Component({ selector: 'ngx-gmaps', styleUrls: ['./gmaps.component.scss'], - templateUrl: './gmaps.component.html' + templateUrl: './gmaps.component.html', }) export class GmapsComponent { public readonly position = { lat: 51.678418, lng: 7.809007 }; diff --git a/src/app/pages/maps/search-map/entity/Location.ts b/src/app/pages/maps/search-map/entity/Location.ts deleted file mode 100644 index f2f784bb..00000000 --- a/src/app/pages/maps/search-map/entity/Location.ts +++ /dev/null @@ -1,4 +0,0 @@ -export class Location { - constructor(public latitude: number = 53.9, public longitude: number = 27.5667) { - } -} diff --git a/src/app/pages/maps/search-map/entity/position.model.ts b/src/app/pages/maps/search-map/entity/position.model.ts new file mode 100644 index 00000000..c5f38f56 --- /dev/null +++ b/src/app/pages/maps/search-map/entity/position.model.ts @@ -0,0 +1,6 @@ +export class PositionModel { + public constructor( + public lat: number = 53.9, + public lng: number = 27.5667, + ) {} +} diff --git a/src/app/pages/maps/search-map/map/map.component.html b/src/app/pages/maps/search-map/map/map.component.html index c5f827e6..04331a29 100644 --- a/src/app/pages/maps/search-map/map/map.component.html +++ b/src/app/pages/maps/search-map/map/map.component.html @@ -1,7 +1,3 @@ - + + + \ No newline at end of file diff --git a/src/app/pages/maps/search-map/map/map.component.ts b/src/app/pages/maps/search-map/map/map.component.ts index 7a117ceb..b8d42cc3 100644 --- a/src/app/pages/maps/search-map/map/map.component.ts +++ b/src/app/pages/maps/search-map/map/map.component.ts @@ -1,5 +1,5 @@ import { Component, Input, OnInit } from '@angular/core'; -import { Location } from '../entity/Location'; +import { PositionModel } from '../entity/position.model'; @Component({ selector: 'ngx-map', @@ -7,23 +7,25 @@ import { Location } from '../entity/Location'; styleUrls: ['./map.component.scss'], }) export class MapComponent implements OnInit { - latitude: number; - longitude: number; - zoom: number; + public position: PositionModel = null; + public zoom: number = 1; @Input() - public set searchedLocation(searchedLocation: Location) { - this.latitude = searchedLocation.latitude; - this.longitude = searchedLocation.longitude; + public set searchedPosition(position: PositionModel) { + if (!position) return; + + console.dir(position); + this.position = position; this.zoom = 12; } - ngOnInit(): void { + public ngOnInit(): void { // set up current location if ('geolocation' in navigator) { navigator.geolocation.getCurrentPosition((position) => { - this.searchedLocation = new Location( - position.coords.latitude, position.coords.longitude, + this.searchedPosition = new PositionModel( + position.coords.latitude, + position.coords.longitude, ); }); } diff --git a/src/app/pages/maps/search-map/search-map.component.html b/src/app/pages/maps/search-map/search-map.component.html index 91bc776b..42a7bdf9 100644 --- a/src/app/pages/maps/search-map/search-map.component.html +++ b/src/app/pages/maps/search-map/search-map.component.html @@ -1,7 +1,7 @@ Google Maps with search - - + + diff --git a/src/app/pages/maps/search-map/search-map.component.ts b/src/app/pages/maps/search-map/search-map.component.ts index a3fa7c55..8fe5fbd6 100644 --- a/src/app/pages/maps/search-map/search-map.component.ts +++ b/src/app/pages/maps/search-map/search-map.component.ts @@ -1,15 +1,14 @@ import { Component } from '@angular/core'; -import { Location } from './entity/Location'; +import { PositionModel } from './entity/position.model'; @Component({ selector: 'ngx-search-map', templateUrl: './search-map.component.html', }) export class SearchMapComponent { + public searchedPosition: PositionModel = new PositionModel(); - searchedLocation: Location = new Location(); - - updateLocation(event: Location) { - this.searchedLocation = new Location(event.latitude, event.longitude); + public setPosition(position: PositionModel): void { + this.searchedPosition = position; } } diff --git a/src/app/pages/maps/search-map/search/search.component.ts b/src/app/pages/maps/search-map/search/search.component.ts index dca89a4d..28ee1fac 100644 --- a/src/app/pages/maps/search-map/search/search.component.ts +++ b/src/app/pages/maps/search-map/search/search.component.ts @@ -1,7 +1,5 @@ import { Component, ElementRef, EventEmitter, NgZone, OnInit, Output, ViewChild } from '@angular/core'; -// import { MapsAPILoader } from '@agm/core'; -import { Location } from '../entity/Location'; - +import { PositionModel } from '../entity/position.model'; @Component({ selector: 'ngx-search', @@ -9,36 +7,34 @@ import { Location } from '../entity/Location'; }) export class SearchComponent implements OnInit { - @Output() positionChanged = new EventEmitter(); + @Output() + public readonly positionChanged: EventEmitter = new EventEmitter(); @ViewChild('search', { static: true }) public searchElementRef: ElementRef; - constructor(// private mapsAPILoader: MapsAPILoader, - private ngZone: NgZone) { - } + constructor(private readonly ngZone: NgZone) {} ngOnInit() { - // load Places Autocomplete - // this.mapsAPILoader.load().then(() => { - // const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, { - // types: ['address'], - // }); - // autocomplete.addListener('place_changed', () => { - // this.ngZone.run(() => { - // // get the place result - // const place: google.maps.places.PlaceResult = autocomplete.getPlace(); + const autocomplete = new google.maps.places.Autocomplete( + this.searchElementRef.nativeElement, { types: ['address'] }, + ); - // // verify result - // if (place.geometry === undefined || place.geometry === null) { - // return; - // } + autocomplete.addListener('place_changed', () => { + this.ngZone.run(() => { + // get the place result + const place: google.maps.places.PlaceResult = autocomplete.getPlace(); - // this.positionChanged.emit( - // new Location(place.geometry.location.lat(), - // place.geometry.location.lng())); - // }); - // }); - // }); + // verify result + if (place.geometry === undefined || place.geometry === null) { + return; + } + + this.positionChanged.emit(new PositionModel( + place.geometry.location.lat(), + place.geometry.location.lng(), + )); + }); + }); } } diff --git a/src/app/pages/tables/smart-table/smart-table.component.ts b/src/app/pages/tables/smart-table/smart-table.component.ts index d14f8d17..164778cf 100644 --- a/src/app/pages/tables/smart-table/smart-table.component.ts +++ b/src/app/pages/tables/smart-table/smart-table.component.ts @@ -1,7 +1,7 @@ import { Component } from '@angular/core'; // import { LocalDataSource } from 'ng2-smart-table'; -import { SmartTableData } from '../../../@core/data/smart-table'; +// import { SmartTableData } from '../../../@core/data/smart-table'; @Component({ selector: 'ngx-smart-table', @@ -55,10 +55,10 @@ export class SmartTableComponent { // source: LocalDataSource = new LocalDataSource(); - constructor(private service: SmartTableData) { - // const data = this.service.getData(); - // this.source.load(data); - } + // constructor(private service: SmartTableData) { + // const data = this.service.getData(); + // this.source.load(data); + // } onDeleteConfirm(event): void { if (window.confirm('Are you sure you want to delete?')) { diff --git a/src/index.html b/src/index.html index 5ed7c8fa..5192560c 100644 --- a/src/index.html +++ b/src/index.html @@ -9,7 +9,7 @@ - + Loading...