Added locations

This commit is contained in:
Zakary Timson 2019-08-31 16:33:50 -04:00
parent 11412f261a
commit 1b04dbb3d9
5 changed files with 110 additions and 4 deletions

19
src/app/adjectives.ts Normal file
View File

@ -0,0 +1,19 @@
export const Adjectives = [
'strict',
'poor',
'old',
'hot',
'huge',
'scared',
'large',
'tall',
'antique',
'complete',
'offbeat',
'selective',
'unknown',
'unwilling',
'lively',
'nebulous',
'deranged'
];

View File

@ -5,7 +5,7 @@ export interface LatLng {
export interface MapData { export interface MapData {
circles?: Circle[]; circles?: Circle[];
locations?: {latlng: LatLng, name: string}[]; locations?: Marker[];
markers?: Marker[]; markers?: Marker[];
measurements?: Measurement[]; measurements?: Measurement[];
polygons?: Polygon[]; polygons?: Polygon[];

52
src/app/nounes.ts Normal file
View File

@ -0,0 +1,52 @@
export const Nouns = [
'area',
'book',
'business',
'case',
'child',
'company',
'country',
'day',
'eye',
'fact',
'family',
'government',
'group',
'hand',
'home',
'job',
'life',
'lot',
'man',
'money',
'month',
'mother',
'Mr',
'night',
'number',
'part',
'people',
'place',
'point',
'problem',
'program',
'question',
'right',
'room',
'school',
'state',
'story',
'student',
'study',
'system',
'thing',
'time',
'water',
'way',
'week',
'woman',
'word',
'work',
'world',
'year',
];

View File

@ -12,9 +12,10 @@ export class SyncService {
private changed = false; private changed = false;
private collection: AngularFirestoreCollection; private collection: AngularFirestoreCollection;
private mapSub: Subscription; private mapSub: Subscription;
private name: string;
private saveRate = 5_000; private saveRate = 5_000;
mapSymbols = new BehaviorSubject<MapData>(null); mapSymbols = new BehaviorSubject<MapData>({});
constructor(private db: AngularFirestore) { constructor(private db: AngularFirestore) {
this.collection = this.db.collection('Maps'); this.collection = this.db.collection('Maps');
@ -48,6 +49,18 @@ export class SyncService {
this.changed = true; this.changed = true;
} }
addMyLocation(location: Marker) {
let map = this.mapSymbols.value;
if(!map.locations) map.locations = [];
let markForSave = this.name == null;
this.name = location.label;
map.locations = map.locations.filter(l => l.label != location.label);
map.locations.push(location);
this.mapSymbols.next(map);
this.changed = true;
if(markForSave) this.save();
}
addPolygon(polygon: Polygon) { addPolygon(polygon: Polygon) {
let map = this.mapSymbols.value; let map = this.mapSymbols.value;
if(!map.polygons) map.polygons = []; if(!map.polygons) map.polygons = [];
@ -75,6 +88,7 @@ export class SyncService {
delete(...symbols) { delete(...symbols) {
let map = this.mapSymbols.value; let map = this.mapSymbols.value;
if(map.circles) symbols.forEach(s => map.circles = map.circles.filter(c => !_.isEqual(s, c))); if(map.circles) symbols.forEach(s => map.circles = map.circles.filter(c => !_.isEqual(s, c)));
if(map.locations) symbols.forEach(s => map.locations = map.locations.filter(m => !_.isEqual(s, m)));
if(map.markers) symbols.forEach(s => map.markers = map.markers.filter(m => !_.isEqual(s, m))); if(map.markers) symbols.forEach(s => map.markers = map.markers.filter(m => !_.isEqual(s, m)));
if(map.measurements) symbols.forEach(s => map.measurements = map.measurements.filter(m => !_.isEqual(s, m))); if(map.measurements) symbols.forEach(s => map.measurements = map.measurements.filter(m => !_.isEqual(s, m)));
if(map.polygons) symbols.forEach(s => map.polygons = map.polygons.filter(p => !_.isEqual(s , p))); if(map.polygons) symbols.forEach(s => map.polygons = map.polygons.filter(p => !_.isEqual(s , p)));
@ -98,6 +112,14 @@ export class SyncService {
setInterval(() => this.save(), this.saveRate); setInterval(() => this.save(), this.saveRate);
} }
removeMyLocation() {
let map = this.mapSymbols.value;
if(map.locations) map.locations = map.locations.filter(l => l.label != this.name);
this.name = null;
this.changed = true;
this.save();
}
save() { save() {
if(this.code && this.mapSymbols.value && this.changed) { if(this.code && this.mapSymbols.value && this.changed) {
this.collection.doc(this.code).set(this.mapSymbols.value); this.collection.doc(this.code).set(this.mapSymbols.value);

View File

@ -1,4 +1,4 @@
import {Component, isDevMode, OnInit} from "@angular/core"; import {Component, HostListener, isDevMode, OnDestroy, OnInit} from "@angular/core";
import {PhysicsService} from "../../services/physics.service"; import {PhysicsService} from "../../services/physics.service";
import {filter, finalize, flatMap, skip, take} from "rxjs/operators"; import {filter, finalize, flatMap, skip, take} from "rxjs/operators";
import {MatBottomSheet, MatSnackBar} from "@angular/material"; import {MatBottomSheet, MatSnackBar} from "@angular/material";
@ -13,6 +13,8 @@ import {DimensionsDialogComponent} from "../../components/dimensionsDialog/dimen
import {MatDialog} from "@angular/material/dialog"; import {MatDialog} from "@angular/material/dialog";
import {SyncService} from "../../services/sync.service"; import {SyncService} from "../../services/sync.service";
import {MapData, Marker} from "../../models/mapSymbol"; import {MapData, Marker} from "../../models/mapSymbol";
import {Adjectives} from "../../adjectives";
import {Nouns} from "../../nounes";
declare const L; declare const L;
@ -22,11 +24,12 @@ declare const L;
styleUrls: ['map.component.scss'], styleUrls: ['map.component.scss'],
animations: [flyInRight, flyOutRight] animations: [flyInRight, flyOutRight]
}) })
export class MapComponent implements OnInit { export class MapComponent implements OnDestroy, OnInit {
code: string; code: string;
drawColor = '#ff4141'; drawColor = '#ff4141';
isNaN = isNaN; isNaN = isNaN;
map: MapService; map: MapService;
name: string;
polygon: any; polygon: any;
position; position;
positionMarker = {arrow: null, circle: null}; positionMarker = {arrow: null, circle: null};
@ -198,13 +201,20 @@ export class MapComponent implements OnInit {
}) })
} }
@HostListener('window:beforeunload', ['$event'])
ngOnDestroy(): void {
this.syncService.removeMyLocation();
}
ngOnInit() { ngOnInit() {
this.name = Adjectives[Math.round(Math.random() * Adjectives.length)] + ' ' + Nouns[Math.round(Math.random() * Nouns.length)];
this.map = new MapService('map'); this.map = new MapService('map');
// Handle drawing the map after updates // Handle drawing the map after updates
this.syncService.mapSymbols.pipe(filter(s => !!s)).subscribe((map: MapData) => { this.syncService.mapSymbols.pipe(filter(s => !!s)).subscribe((map: MapData) => {
this.map.deleteAll(); this.map.deleteAll();
if (map.circles) map.circles.forEach(c => this.map.newCircle(c)); if (map.circles) map.circles.forEach(c => this.map.newCircle(c));
if (map.locations) map.locations.filter(l => l.label != this.name).forEach(l => this.map.newMarker(l));
if (map.markers) map.markers.forEach(m => this.map.newMarker(m)); if (map.markers) map.markers.forEach(m => this.map.newMarker(m));
if (map.measurements) map.measurements.forEach(m => this.map.newMeasurement(m)); if (map.measurements) map.measurements.forEach(m => this.map.newMeasurement(m));
if (map.polygons) map.polygons.forEach(p => this.map.newPolygon(p)); if (map.polygons) map.polygons.forEach(p => this.map.newPolygon(p));
@ -227,11 +237,14 @@ export class MapComponent implements OnInit {
} }
}); });
// Display location // Display location
this.physicsService.info.pipe(filter(coord => !!coord)).subscribe(pos => { this.physicsService.info.pipe(filter(coord => !!coord)).subscribe(pos => {
if (!this.position) this.center({lat: pos.latitude, lng: pos.longitude}); if (!this.position) this.center({lat: pos.latitude, lng: pos.longitude});
if (this.positionMarker.arrow) this.map.delete(this.positionMarker.arrow); if (this.positionMarker.arrow) this.map.delete(this.positionMarker.arrow);
if (this.positionMarker.circle) this.map.delete(this.positionMarker.circle); if (this.positionMarker.circle) this.map.delete(this.positionMarker.circle);
this.syncService.addMyLocation({latlng: {lat: pos.latitude, lng: pos.longitude}, label: this.name});
this.positionMarker.arrow = this.map.newMarker({ this.positionMarker.arrow = this.map.newMarker({
latlng: {lat: pos.latitude, lng: pos.longitude}, latlng: {lat: pos.latitude, lng: pos.longitude},
noSelect: true, noSelect: true,