-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Google Maps loading strategy (#40)
- Loading branch information
1 parent
03fccd3
commit eca775d
Showing
12 changed files
with
87 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const GOOGLE_MAPS_API_KEY = new InjectionToken<string>('Google Maps API key'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { HttpClientJsonpModule, HttpClientModule } from '@angular/common/http'; | ||
import { ModuleWithProviders, NgModule } from '@angular/core'; | ||
import { GOOGLE_MAPS_API_KEY } from 'src/app/google-maps/api-key.token'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
HttpClientModule, | ||
HttpClientJsonpModule, | ||
], | ||
}) | ||
export class GoogleMapsConfigModule { | ||
static forRoot(googleMapsApiKey: string): ModuleWithProviders<GoogleMapsConfigModule> { | ||
return <ModuleWithProviders<GoogleMapsConfigModule>> { | ||
ngModule: GoogleMapsConfigModule, | ||
providers: [ | ||
{ provide: GOOGLE_MAPS_API_KEY, useValue: googleMapsApiKey, }, | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './config.module'; | ||
export * from './script-loader'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { HttpClient, HttpParams } from '@angular/common/http'; | ||
import { Inject, Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router'; | ||
import { Observable, of } from 'rxjs'; | ||
import { catchError, map, publishReplay, refCount } from 'rxjs/operators'; | ||
import { GOOGLE_MAPS_API_KEY } from './api-key.token'; | ||
|
||
const SCRIPT_URL = 'https://maps.googleapis.com/maps/api/js'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class GoogleMapsScriptLoader implements Resolve<boolean> { | ||
readonly scriptLoaded$: Observable<boolean>; | ||
|
||
constructor( | ||
@Inject(GOOGLE_MAPS_API_KEY) apiKey: string, | ||
httpClient: HttpClient, | ||
) { | ||
this.scriptLoaded$ = this._loadScript(httpClient, apiKey) | ||
.pipe( | ||
publishReplay(1), | ||
refCount(), | ||
); | ||
} | ||
|
||
resolve(_route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): Observable<boolean> { | ||
return this.scriptLoaded$; | ||
} | ||
|
||
_loadScript(httpClient: HttpClient, apiKey: string): Observable<boolean> { | ||
const queryParams = new HttpParams().append('key', apiKey); | ||
const url = `${SCRIPT_URL}?${queryParams.toString()}`; | ||
|
||
return httpClient.jsonp(url, 'callback').pipe( | ||
map(() => true), | ||
catchError(() => of(false)), | ||
); | ||
} | ||
} |
30 changes: 14 additions & 16 deletions
30
src/src/app/main/components/google-map/google-map.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
<ng-container *ngIf="loaded$ | async"> | ||
<google-map | ||
class="map" | ||
height="100%" | ||
(mapClick)="addMarker($event)" | ||
[options]="options" | ||
width="100%" | ||
> | ||
<ng-container *ngFor="let marker of addedMarkersForMap$ | async"> | ||
<map-marker [position]="marker"></map-marker> | ||
</ng-container> | ||
<google-map | ||
class="map" | ||
height="100%" | ||
(mapClick)="addMarker($event)" | ||
[options]="options" | ||
width="100%" | ||
> | ||
<ng-container *ngFor="let marker of addedMarkersForMap$ | async"> | ||
<map-marker [position]="marker"></map-marker> | ||
</ng-container> | ||
|
||
<ng-container *ngFor="let marker of gottenMarkersForMap$ | async"> | ||
<map-marker [position]="marker"></map-marker> | ||
</ng-container> | ||
</google-map> | ||
</ng-container> | ||
<ng-container *ngFor="let marker of gottenMarkersForMap$ | async"> | ||
<map-marker [position]="marker"></map-marker> | ||
</ng-container> | ||
</google-map> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { Routes } from '@angular/router'; | ||
|
||
import { GoogleMapsScriptLoader } from 'src/app/google-maps'; | ||
import { MainComponent } from './main.component'; | ||
|
||
export const routes: Routes = [ | ||
{ | ||
path: '', | ||
component: MainComponent, | ||
resolve: { | ||
googleMapsScriptLoaded: GoogleMapsScriptLoader, | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
src/src/app/main/services/google-map-script-loader.service.ts
This file was deleted.
Oops, something went wrong.