Skip to content

Commit 97c7134

Browse files
dimaatkaevnnixaa
authored andcommitted
feat(maps): add google map with access search location dropdown
1 parent cb5795b commit 97c7134

13 files changed

+151
-3
lines changed

package-lock.json

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@angular/compiler-cli": "~5.1.0",
7676
"@angular/language-service": "~5.1.0",
7777
"@compodoc/compodoc": "1.0.1",
78+
"@types/googlemaps": "3.30.4",
7879
"@types/d3-color": "1.0.5",
7980
"@types/jasmine": "2.5.54",
8081
"@types/jasminewd2": "2.0.3",

src/app/pages/maps/maps-routing.module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { MapsComponent } from './maps.component';
55
import { GmapsComponent } from './gmaps/gmaps.component';
66
import { LeafletComponent } from './leaflet/leaflet.component';
77
import { BubbleMapComponent } from './bubble/bubble-map.component';
8+
import { SearchMapComponent } from './search-map/search-map.component';
9+
import { MapComponent } from './search-map/map/map.component';
10+
import { SearchComponent } from './search-map/search/search.component';
811

912
const routes: Routes = [{
1013
path: '',
@@ -18,6 +21,9 @@ const routes: Routes = [{
1821
}, {
1922
path: 'bubble',
2023
component: BubbleMapComponent,
24+
}, {
25+
path: 'searchmap',
26+
component: SearchMapComponent,
2127
}],
2228
}];
2329

@@ -32,4 +38,7 @@ export const routedComponents = [
3238
GmapsComponent,
3339
LeafletComponent,
3440
BubbleMapComponent,
41+
SearchMapComponent,
42+
MapComponent,
43+
SearchComponent,
3544
];

src/app/pages/maps/maps.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { MapsRoutingModule, routedComponents } from './maps-routing.module';
99
@NgModule({
1010
imports: [
1111
ThemeModule,
12-
AgmCoreModule.forRoot(),
12+
AgmCoreModule.forRoot({
13+
apiKey: 'AIzaSyCpVhQiwAllg1RAFaxMWSpQruuGARy0Y1k',
14+
libraries: ['places'],
15+
}),
1316
LeafletModule.forRoot(),
1417
MapsRoutingModule,
1518
NgxEchartsModule,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class Location {
2+
constructor(public latitude: number = 54, public longitude: number = 33) {
3+
}
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<agm-map [latitude]="latitude"
2+
[longitude]="longitude"
3+
[scrollwheel]="false"
4+
[zoom]="zoom">
5+
<agm-marker [latitude]="latitude"
6+
[longitude]="longitude"></agm-marker>
7+
</agm-map>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@import '../../../../@theme/styles/themes';
2+
3+
@include nb-install-component() {
4+
5+
nb-card-body {
6+
padding: 0;
7+
}
8+
9+
/deep/ agm-map {
10+
width: 100%;
11+
height: nb-theme(card-height-large);
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Component, Input, OnInit } from '@angular/core';
2+
import { Location } from '../entity/Location';
3+
4+
@Component({
5+
selector: 'ngx-map',
6+
templateUrl: './map.component.html',
7+
styleUrls: ['./map.component.scss'],
8+
})
9+
export class MapComponent implements OnInit {
10+
latitude: number;
11+
longitude: number;
12+
zoom: number;
13+
14+
@Input()
15+
public set searchedLocation(searchedLocation: Location) {
16+
this.latitude = searchedLocation.latitude;
17+
this.longitude = searchedLocation.longitude;
18+
this.zoom = 12;
19+
}
20+
21+
ngOnInit(): void {
22+
// set up current location
23+
if ('geolocation' in navigator) {
24+
navigator.geolocation.getCurrentPosition((position) => {
25+
this.searchedLocation = new Location(
26+
position.coords.latitude, position.coords.longitude,
27+
);
28+
});
29+
}
30+
}
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<nb-card>
2+
<nb-card-header>Google Maps with search</nb-card-header>
3+
<nb-card-body>
4+
<ngx-search (positionChanged)="updateLocation($event)"></ngx-search>
5+
<ngx-map [searchedLocation]="searchedLocation"></ngx-map>
6+
</nb-card-body>
7+
</nb-card>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component } from '@angular/core';
2+
import { Location } from './entity/Location';
3+
4+
@Component({
5+
selector: 'ngx-search-map',
6+
templateUrl: './search-map.component.html',
7+
})
8+
export class SearchMapComponent {
9+
10+
searchedLocation: Location = new Location();
11+
12+
updateLocation(event: Location) {
13+
this.searchedLocation = new Location(event.latitude, event.longitude);
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="form-group">
2+
<input placeholder="search for location" autocorrect="off" autocapitalize="off" spellcheck="off" type="text"
3+
class="form-control search-location" #search>
4+
</div>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Component, ElementRef, EventEmitter, NgZone, OnInit, Output, ViewChild } from '@angular/core';
2+
import { MapsAPILoader } from '@agm/core';
3+
import { Location } from '../entity/Location';
4+
import {} from 'googlemaps';
5+
6+
@Component({
7+
selector: 'ngx-search',
8+
templateUrl: './search.component.html',
9+
})
10+
export class SearchComponent implements OnInit {
11+
12+
@Output() positionChanged = new EventEmitter<Location>();
13+
14+
@ViewChild('search')
15+
public searchElementRef: ElementRef;
16+
17+
constructor(private mapsAPILoader: MapsAPILoader,
18+
private ngZone: NgZone) {
19+
}
20+
21+
ngOnInit() {
22+
// load Places Autocomplete
23+
this.mapsAPILoader.load().then(() => {
24+
const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
25+
types: ['address'],
26+
});
27+
autocomplete.addListener('place_changed', () => {
28+
this.ngZone.run(() => {
29+
// get the place result
30+
const place: google.maps.places.PlaceResult = autocomplete.getPlace();
31+
32+
// verify result
33+
if (place.geometry === undefined || place.geometry === null) {
34+
return;
35+
}
36+
37+
this.positionChanged.emit(
38+
new Location(place.geometry.location.lat(),
39+
place.geometry.location.lng()));
40+
});
41+
});
42+
});
43+
}
44+
}

src/app/pages/pages-menu.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export const MENU_ITEMS: NbMenuItem[] = [
8989
title: 'Bubble Maps',
9090
link: '/pages/maps/bubble',
9191
},
92+
{
93+
title: 'Search Maps',
94+
link: '/pages/maps/searchmap',
95+
},
9296
],
9397
},
9498
{

0 commit comments

Comments
 (0)