Integrate with new API, Pulse animation marker

This commit is contained in:
Supan Adit Pratama 2020-01-04 09:01:18 +07:00
parent 40dc9508a3
commit 93e112873f
9 changed files with 115 additions and 97 deletions

View File

@ -1,7 +1,7 @@
![Geo Smart Logo](http://supanadit.com/wp-content/uploads/2019/11/Geo-Smart-Logo.png) ![Geo Smart Logo](http://supanadit.com/wp-content/uploads/2019/11/Geo-Smart-Logo.png)
# GEO Smart Map # GEO Smart Map
This is the Front End Application which the Live Map for [Geo Smart System](https://github.com/supanadit/geosmartsystem) This is the Front End Application which is the Live Map for [Geo Smart System](https://github.com/supanadit/geosmartsystem)
## Requirements ## Requirements
- NPM - NPM

View File

@ -31,9 +31,13 @@
"src/styles.scss", "src/styles.scss",
"node_modules/leaflet/dist/leaflet.css", "node_modules/leaflet/dist/leaflet.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css", "node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/ngx-toastr/toastr.css" "node_modules/ngx-toastr/toastr.css",
"node_modules/@ansur/leaflet-pulse-icon/dist/L.Icon.Pulse.css"
], ],
"scripts": [] "scripts": [
"node_modules/leaflet/dist/leaflet.js",
"node_modules/@ansur/leaflet-pulse-icon/dist/L.Icon.Pulse.js"
]
}, },
"configurations": { "configurations": {
"production": { "production": {

8
package-lock.json generated
View File

@ -1011,6 +1011,14 @@
"tslib": "^1.9.0" "tslib": "^1.9.0"
} }
}, },
"@ansur/leaflet-pulse-icon": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@ansur/leaflet-pulse-icon/-/leaflet-pulse-icon-0.1.1.tgz",
"integrity": "sha512-2vymis4WMiacers0JJ43Ku0TjdKypnh0atMdFC2VdTGd5G/NGsmW5eYnTZr1YHNOts3CXRbnwlkEMKB7OPsfIg==",
"requires": {
"leaflet": ">=0.7.3"
}
},
"@asymmetrik/ngx-leaflet": { "@asymmetrik/ngx-leaflet": {
"version": "6.0.1", "version": "6.0.1",
"resolved": "https://registry.npmjs.org/@asymmetrik/ngx-leaflet/-/ngx-leaflet-6.0.1.tgz", "resolved": "https://registry.npmjs.org/@asymmetrik/ngx-leaflet/-/ngx-leaflet-6.0.1.tgz",

View File

@ -19,6 +19,7 @@
"@angular/platform-browser": "~8.2.11", "@angular/platform-browser": "~8.2.11",
"@angular/platform-browser-dynamic": "~8.2.11", "@angular/platform-browser-dynamic": "~8.2.11",
"@angular/router": "~8.2.11", "@angular/router": "~8.2.11",
"@ansur/leaflet-pulse-icon": "^0.1.1",
"@asymmetrik/ngx-leaflet": "^6.0.1", "@asymmetrik/ngx-leaflet": "^6.0.1",
"bootstrap": "^4.3.1", "bootstrap": "^4.3.1",
"leaflet": "^1.5.1", "leaflet": "^1.5.1",

View File

@ -1,11 +1,12 @@
import { NgModule } from '@angular/core'; import {NgModule} from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; import {Routes, RouterModule} from '@angular/router';
const routes: Routes = []; const routes: Routes = [];
@NgModule({ @NgModule({
imports: [RouterModule.forRoot(routes)], imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule] exports: [RouterModule]
}) })
export class AppRoutingModule { } export class AppRoutingModule {
}

View File

@ -1,3 +1,3 @@
<div class="map" leaflet [leafletOptions]="options"> <div class="map" leaflet [leafletOptions]="options">
<div *ngFor="let x of listMarker" [leafletLayer]="x"></div> <div *ngFor="let x of listMarker" [leafletLayer]="x"></div>
</div> </div>

View File

@ -1,38 +1,41 @@
import { Component, OnInit } from '@angular/core'; import {Component, OnInit} from '@angular/core';
import { latLng, Marker, tileLayer } from 'leaflet'; import {latLng, marker, Marker, tileLayer} from 'leaflet';
import { AppService } from './app.service'; import {AppService} from './app.service';
import { ToastrService } from 'ngx-toastr'; import {ToastrService} from 'ngx-toastr';
declare const L: any;
import '@ansur/leaflet-pulse-icon';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
templateUrl: './app.component.html', templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'] styleUrls: ['./app.component.scss']
}) })
export class AppComponent implements OnInit { export class AppComponent implements OnInit {
token = 'pk.eyJ1Ijoic3VwYW5hZGl0IiwiYSI6ImNrMmVweWdrcTA4ZzgzY3A1NDE5ZnQwazkifQ.hK2Mz6cFk-jeIHzFBdaKTg'; token = 'pk.eyJ1Ijoic3VwYW5hZGl0IiwiYSI6ImNrMmVweWdrcTA4ZzgzY3A1NDE5ZnQwazkifQ.hK2Mz6cFk-jeIHzFBdaKTg';
style = `ck1w9autf0f0r1co76j79eab7`; style = `ck1w9autf0f0r1co76j79eab7`;
options = { options = {
layers: [ layers: [
tileLayer(`https://api.mapbox.com/styles/v1/supanadit/${this.style}/tiles/256/{z}/{x}/{y}@2x?access_token=${this.token}`, { tileLayer(`https://api.mapbox.com/styles/v1/supanadit/${this.style}/tiles/256/{z}/{x}/{y}@2x?access_token=${this.token}`, {
maxZoom: 18, maxZoom: 18,
}) })
], ],
zoom: 12, zoom: 12,
center: latLng(-6.914744, 107.609810) center: latLng(-6.914744, 107.609810)
}; };
// @ts-ignore // @ts-ignore
listMarker: Array<Marker> = []; listMarker: Array<Marker> = [];
// tslint:disable-next-line:variable-name // tslint:disable-next-line:variable-name
constructor(private appService: AppService, private toastr: ToastrService) { constructor(private appService: AppService, private toastr: ToastrService) {
} }
ngOnInit(): void { ngOnInit(): void {
this.appService.getStreamUser().subscribe((listMarker: Array<Marker>) => { this.appService.getStreamUser().subscribe((listMarker: Array<Marker>) => {
this.listMarker = listMarker; this.listMarker = listMarker;
}); });
} }
} }

View File

@ -1,24 +1,24 @@
import { Injectable } from '@angular/core'; import {Injectable} from '@angular/core';
import { LocationModel, locationModelFromEventSource, markerListFromLocationModel } from './model/location'; import {LocationModel, locationModelFromEventSource, markerListFromLocationModel} from './model/location';
import { Observable } from 'rxjs'; import {Observable} from 'rxjs';
import { environment } from '../environments/environment'; import {environment} from '../environments/environment';
import { Marker } from 'leaflet'; import {Marker} from 'leaflet';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AppService { export class AppService {
constructor() { constructor() {
} }
getStreamUser(): Observable<Array<Marker>> { getStreamUser(): Observable<Array<Marker>> {
return new Observable(observer => { return new Observable(observer => {
const source = new EventSource(environment.api.concat('/stream?request=sse')); const source = new EventSource(environment.api.concat('/point/get/stream'));
source.addEventListener('message', message => { source.addEventListener('message', message => {
const listLocationModel: Array<LocationModel> = locationModelFromEventSource(message); const listLocationModel: Array<LocationModel> = locationModelFromEventSource(message);
observer.next(markerListFromLocationModel(listLocationModel)); observer.next(markerListFromLocationModel(listLocationModel));
}); });
}); });
} }
} }

View File

@ -1,72 +1,73 @@
import { icon, marker, Marker } from 'leaflet'; import {icon, marker, Marker} from 'leaflet';
declare const L: any;
import '@ansur/leaflet-pulse-icon';
export interface PointInterface { export interface PointInterface {
type: string; type: string;
coordinates: Array<number>; coordinates: Array<number>;
} }
export class PointModel implements PointInterface { export class PointModel implements PointInterface {
coordinates: Array<number>; coordinates: Array<number>;
type: string; type: string;
constructor(dataLocation: PointInterface) { constructor(dataLocation: PointInterface) {
this.coordinates = dataLocation.coordinates; this.coordinates = dataLocation.coordinates;
this.type = dataLocation.type; this.type = dataLocation.type;
} }
Lat(): number { Lat(): number {
return this.coordinates[1]; return this.coordinates[1];
} }
Lng(): number { Lng(): number {
return this.coordinates[0]; return this.coordinates[0];
} }
} }
export interface PointInterface { export interface PointInterface {
coordinates: Array<number>; coordinates: Array<number>;
} }
export interface LocationInterface { export interface LocationInterface {
id: string; id: string;
object: PointInterface; object: PointInterface;
} }
export class LocationModel implements LocationInterface { export class LocationModel implements LocationInterface {
id: string; id: string;
object: PointInterface; object: PointInterface;
constructor(location: LocationInterface) { constructor(location: LocationInterface) {
this.id = location.id; this.id = location.id;
this.object = location.object; this.object = location.object;
} }
Point(): PointModel { Point(): PointModel {
return new PointModel(this.object); return new PointModel(this.object);
} }
Marker(): Marker { Marker(): Marker {
return marker([this.Point().Lat(), this.Point().Lng()], { const pulseMarker = L.icon.pulse({iconSize: [6, 6], color: 'red'});
icon: icon({ return marker([this.Point().Lat(), this.Point().Lng()], {
iconSize: [20, 20], icon: pulseMarker,
iconUrl: 'assets/markers/red.png', });
}), }
});
}
} }
export function locationModelFromEventSource(listener: any): Array<LocationModel> { export function locationModelFromEventSource(listener: any): Array<LocationModel> {
const data = JSON.parse(listener.data); const data = JSON.parse(listener.data);
let locationModelList: Array<LocationModel> = []; let locationModelList: Array<LocationModel> = [];
if (data != null) { if (data != null) {
const listData: Array<any> = data.data; const listData: Array<any> = data.data;
if (listData != null) { if (listData != null) {
locationModelList = listData.map<LocationModel>((x: LocationInterface) => new LocationModel(x)); locationModelList = listData.map<LocationModel>((x: LocationInterface) => new LocationModel(x));
}
} }
return locationModelList; }
return locationModelList;
} }
export function markerListFromLocationModel(listLocationModel: Array<LocationModel>): Array<Marker> { export function markerListFromLocationModel(listLocationModel: Array<LocationModel>): Array<Marker> {
return listLocationModel.map<Marker>((x: LocationModel) => x.Marker()); return listLocationModel.map<Marker>((x: LocationModel) => x.Marker());
} }