ipntk-map
Version:
web sdk for ipntk-map
113 lines (107 loc) • 2.85 kB
JavaScript
import * as MapxusMap from "@mapxus/mapxus-map-jp";
import Cookies from "js-cookie";
export class iPntkMap {
async initMap(props) {
//unpacking initialized data
this.map = props?.map;
this.customerId = props?.customerId;
this.mapArg = { ...props };
/**
* Short circuit conditions that cause map initialization to fail
*/
if (this.customerId === undefined) {
throw new Error("Cannot initialize Map : CustomerID is missing");
}
if (this.map === undefined) {
throw new Error("Cannot initialize Map : Outdoor Map is missing");
}
/**
*khiAuth Request
*/
async function khiAuth(customerId) {
// creating auth object
const authObj = { customerId: customerId };
// checking for previous cookie
let prevcookie = Cookies.get("iPntkMapData");
if (!prevcookie) {
// if not found -> send empty
authObj.cookieData = {};
} else {
//else send orginal
authObj.cookieData = JSON.parse(prevcookie);
}
authObj.domain = window.location.hostname;
let response = await fetch(
"https://web-sdk-khi-auth.azurewebsites.net/api/khiauth",
{
method: "POST",
body: JSON.stringify(authObj),
headers: {
"Content-type": "application/json; charset=UTF-8",
},
}
);
let authData = await response.json();
// setting new cookie
Cookies.set("iPntkMapData", JSON.stringify(authData.cookieData), {
expires: 365,
});
//setting up Auth to auto-execute again after midnight + 5mins
setTimeout(async () => {
khiAuth(customerId);
}, mauCountUpTime());
return authData;
}
let authData = await khiAuth(this.customerId);
/**
* creating map initialization object
*/
this.mapArg.appId = authData.appId;
this.mapArg.secret = authData.secret;
try {
return new MapxusMap.Map(this.mapArg);
} catch (error) {
throw error;
}
}
}
export class BuildingsService {
constructor() {
return new MapxusMap.BuildingsService();
}
}
export class PoisService {
constructor() {
return new MapxusMap.PoisService();
}
}
export class RoutePainter {
constructor() {
new MapxusMap.RoutePainter();
}
}
export class RouteService {
constructor() {
return new MapxusMap.RouteService();
}
}
export class VenuesService {
constructor() {
return new MapxusMap.VenuesService();
}
}
export class Marker {
constructor(map) {
return new MapxusMap.Marker(map);
}
}
/**
* Helper functions
*/
// function to calculate miliseconds until midnight
function mauCountUpTime() {
var now = new Date();
var then = new Date(now);
then.setHours(24, 0, 0, 0);
return then - now + 300000; //miliseconds until midnight + 5mins
}