request geolocation perm

This commit is contained in:
ztimson 2019-07-11 14:54:08 -04:00
parent 197ff18efd
commit 3b0d69b4c9
6 changed files with 96 additions and 44 deletions

View File

@ -12,13 +12,15 @@ import {AgmCoreModule} from "@agm/core";
import {MaterialModule} from "./material.module";
import {CalibrateComponent} from "./map/calibrate/calibrate.component";
import {MatInputModule} from "@angular/material";
import {PermissionsComponent} from "./permissions/permissions.component";
@NgModule({
declarations: [
AppComponent,
CalibrateComponent,
HomeComponent,
MapComponent
MapComponent,
PermissionsComponent
],
imports: [
AgmCoreModule.forRoot({apiKey: 'AIzaSyDFtvCY6nH_HUoTBNf_5b-E8nRweSLYtxE'}),
@ -31,7 +33,7 @@ import {MatInputModule} from "@angular/material";
MatInputModule,
],
providers: [],
entryComponents: [CalibrateComponent],
entryComponents: [CalibrateComponent, PermissionsComponent],
bootstrap: [AppComponent]
})
export class AppModule {

View File

@ -1,6 +1,6 @@
import {
MatBottomSheetModule,
MatButtonModule, MatDividerModule, MatFormFieldModule,
MatButtonModule, MatDialogModule, MatDividerModule, MatFormFieldModule,
MatIconModule, MatInputModule, MatMenuModule, MatSliderModule,
MatSnackBarModule,
MatToolbarModule
@ -10,6 +10,7 @@ import {NgModule} from "@angular/core";
export const materialModules = [
MatBottomSheetModule,
MatButtonModule,
MatDialogModule,
MatDividerModule,
MatFormFieldModule,
MatIconModule,

View File

@ -0,0 +1,10 @@
<div mat-dialog-content>
<div class="d-flex">
<div><mat-icon class="mr-2">{{icon}}</mat-icon></div>
<div><p class="d-inline">{{message}}</p></div>
</div>
</div>
<div mat-dialog-actions class="float-right">
<button mat-button [mat-dialog-close]="false">No Thanks</button>
<button mat-button [mat-dialog-close]="true">Ok</button>
</div>

View File

@ -0,0 +1,16 @@
import { Component, Inject } from "@angular/core";
import { MAT_DIALOG_DATA } from "@angular/material";
@Component({
selector: 'permissions',
templateUrl: 'permissions.component.html'
})
export class PermissionsComponent {
icon: string;
message: string;
constructor(@Inject(MAT_DIALOG_DATA) data) {
this.icon = data.icon;
this.message = data.message;
}
}

View File

@ -0,0 +1,18 @@
import {Injectable} from "@angular/core";
import {MatDialog} from "@angular/material";
import {PermissionsComponent} from "./permissions.component";
@Injectable({
providedIn: 'root'
})
export class PermissionsService {
constructor(private dialog: MatDialog) { }
async requestPermission(name: string, icon: string, message: string) {
let perm = await navigator['permissions'].query({name: name});
if (perm.state == 'prompt') {
return await this.dialog.open(PermissionsComponent, {autoFocus: false, data: {icon: icon, message: message}}).afterClosed().toPromise();
}
return perm.state == 'granted'
}
}

View File

@ -1,6 +1,7 @@
import {EventEmitter, Injectable} from '@angular/core';
import {BehaviorSubject, combineLatest} from "rxjs";
import {debounceTime} from "rxjs/operators";
import {PermissionsService} from "../permissions/permissions.service";
@Injectable({
providedIn: 'root'
@ -17,7 +18,9 @@ export class PhysicsService {
position = new BehaviorSubject<Position>(null);
speed = new BehaviorSubject(null);
constructor() {
constructor(permissionsService: PermissionsService) {
permissionsService.requestPermission('geolocation', 'gps_fixed', 'Can we use your location?').then(granted => {
if(granted) {
// Gather physical data
window.addEventListener('deviceorientation', orientation => this.orientation.next(orientation));
window.addEventListener('devicemotion', motion => this.motion.next(motion));
@ -66,4 +69,6 @@ export class PhysicsService {
this.info.next(info);
})
}
});
}
}