@hexide-digital/geoportal.lt
Version:
Set of libs for geoportal.lt
143 lines (142 loc) • 4.65 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { merge } from 'lodash';
import Solver from './solver';
import Builder from './builder';
import Drawer from './drawer';
export default class Router {
constructor(options = {}, drawerOptions = {}, builderOptions = {}, solverOptions = {}, formatterOptions = {}) {
this.points = [];
this.options = {
language: 'en',
url: 'https://www.geoportal.lt/map/proxy/routing/Route/solve',
autoRoute: true,
};
this.useDrawer = false;
this.options = this.options = merge({}, this.options, options);
builderOptions.language = options.language;
solverOptions.directionsLanguage = options.language;
formatterOptions.language = options.language;
this.solver = new Solver(this.options.url, solverOptions);
this.builder = new Builder(builderOptions, formatterOptions);
this.drawer = new Drawer(drawerOptions);
}
addPoint(point) {
if (point.lat && point.lng) {
this.points.push(point);
if (this.useDrawer) {
this.drawer.addPoint(point);
}
}
return this;
}
setPoints(points) {
this.points = [];
points.forEach((point) => {
this.addPoint(point);
});
return this;
}
addPoints(points) {
points.forEach((point) => {
this.addPoint(point);
});
return this;
}
getPoints() {
return this.points;
}
getRoutes() {
return this.routes;
}
addTo(map) {
this.drawer.setMap(map);
this.useDrawer = true;
return this;
}
showDirectionMarker(location) {
if (!this.useDrawer) {
throw new Error('Drawer cannot be used. Init Drawer with map first.');
}
this.drawer.showDirectionMarker(location);
return this;
}
hideDirectionMarker(location) {
if (!this.useDrawer) {
throw new Error('Drawer cannot be used. Init Drawer with map first.');
}
this.drawer.hideDirectionMarker(location);
return this;
}
build() {
return __awaiter(this, void 0, void 0, function* () {
if (this.points.length < 2) {
throw new Error('At least 2 points needed to build the route');
}
this.originalRoute = yield this.solver.solve(this.points);
this.routes = this.builder.build(this.originalRoute);
return this;
});
}
clear() {
this.points = [];
this.routes = null;
if (this.useDrawer) {
this.drawer.clear();
}
return this;
}
draw() {
if (!this.useDrawer) {
throw new Error('Drawer cannot be used. Init Drawer with map first.');
}
this.drawer.clear();
this.routes.forEach((route) => {
this.drawer.addPoints(Router.preparePointsFromStops(route.stops)).draw(route.path);
});
return this;
}
panTo(location, zoom = null) {
if (!this.useDrawer) {
throw new Error('Drawer cannot be used. Init Drawer with map first.');
}
this.drawer.panTo(location, zoom);
return this;
}
setBuilder(builder) {
this.builder = builder;
return this;
}
setDrawer(drawer) {
this.drawer = drawer;
this.useDrawer = true;
return this;
}
setFormatter(formatter) {
this.builder.setFormatter(formatter);
return this;
}
setSolver(solver) {
this.solver = solver;
return this;
}
static preparePointsFromStops(stops) {
return stops.map((stop) => {
return {
label: stop.name,
lat: stop.location.lat,
lng: stop.location.lng,
};
});
}
getOriginalRoute() {
return this.originalRoute;
}
}