Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
LarsBergqvist / googlemaps.service.ts
Created February 2, 2020 11:00
Service that uses load-google-maps-api
import { Injectable } from '@angular/core';
import * as loadGoogleMapsApi from 'load-google-maps-api';
@Injectable()
class GoogleMapsService {
constructor() { }
load(googleAPIKey: string): any {
return loadGoogleMapsApi({ key: googleAPIKey });
}
}
@LarsBergqvist
LarsBergqvist / app.module.ts
Created February 2, 2020 10:48
Provide for HTTP_INTERCEPTORS
{
provide: HTTP_INTERCEPTORS,
useClass: HttpInterceptorService,
multi: true,
deps: [MessageBrokerService, LoggingService]
},
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ErrorOccurredMessage } from '../messages/error-occurred.message';
import { MessageBrokerService } from './message-broker.service';
import { LoggingService } from './logging-service';
@Injectable({
providedIn: 'root'
@LarsBergqvist
LarsBergqvist / app.module.ts
Created February 2, 2020 10:42
Factory function for creating a real service or a mock service
export function locationsServiceFactory() {
return (http: HttpClient, configService: AppConfigService, logging: LoggingService): LocationsService => {
if (configService.useFakeData) {
logging.logInfo('use mock service');
return new LocationsServiceMock();
} else {
logging.logInfo('use real service');
return new LocationsServiceImpl(http, configService);
}
};
@LarsBergqvist
LarsBergqvist / map.component.ts
Created February 2, 2020 10:31
Angular component for GMap and google maps
import { Component, Input, OnInit } from '@angular/core';
import { Location } from '../models/location';
import { AppConfigService } from '../services/app-config.service';
import { MessageBrokerService } from '../services/message-broker.service';
import { NewMarkerFromMapMessage } from '../messages/new-marker-from-map.message';
declare var google: any;
@Component({
selector: 'app-map',
@LarsBergqvist
LarsBergqvist / map.component.html
Created February 2, 2020 10:28
Using PrimeNG's GMap for displaying a Google Maps view
<div *ngIf="useMap">
<p-gmap *ngIf="options.center" [options]="options" [overlays]="overlays" [style]="{'width':'100%','height':'320px'}"
(onMapReady)="onMapReady($event)" (onMapClick)="handleMapClick($event)"></p-gmap>
</div>
@LarsBergqvist
LarsBergqvist / app.module.ts
Last active February 3, 2020 20:29
Loading google maps api with APP_INITIALIZER provider in Angular
export function appConfigInit(configService: AppConfigService,
googleMapService: GoogleMapsService, logging: LoggingService) {
// Load the configuration and init google api if maps should be used
return () => {
return new Promise((resolve) => {
configService.load().then(() => {
if (configService.useMap) {
logging.logInfo('Use map');
googleMapService.load(configService.googleAPIKey).then(() => {
resolve();
@LarsBergqvist
LarsBergqvist / app-config.json
Created February 1, 2020 17:51
Example of app configuration with map activated and (fake example) googleAPIKey
{
"apiUrl": "https://localhost",
"apiPort": "5001",
"useFakeData": true,
"useMap": true,
"googleAPIKey": "ABzaDuAQbFxSalDaWfIAB7DEZAh730AFGGGlpg"
}
@LarsBergqvist
LarsBergqvist / location-details.component.ts
Last active February 1, 2020 16:41
Get the current position from the browser
async ngOnInit() {
const messages = this.messageBroker.getMessage();
messages.pipe(filter(message => message instanceof AddNewLocationMessage))
.subscribe((message: AddNewLocationMessage) => {
if (message) {
this.createDefaultLocation();
this.editMode = LocationEditMode.AddNew;
this.isVisible = true;
this.setCurrentPosition();
}
@LarsBergqvist
LarsBergqvist / app-config.json
Created February 1, 2020 15:27
Application configuration file for Locations In Your Pocket
{
"apiUrl": "https://localhost",
"apiPort": "5001",
"useFakeData": true,
"useMap": false,
"googleAPIKey": "YOUR-API-KEY"
}