Skip to content

Commit

Permalink
create map
Browse files Browse the repository at this point in the history
  • Loading branch information
limistah committed Dec 16, 2023
1 parent 4b68d8e commit de568a7
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
},
"dependencies": {
"@limistah/here-map-js": "^3.1.0",
"dot-prop": "^8.0.2",
"lodash.merge": "^4.6.2"
}
}
65 changes: 61 additions & 4 deletions src/components/Map/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,65 @@
import React from 'react';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { defaultOptions } from '../libs/defaults';
import { PlatformContext } from '../../contexts/platform';
import { MapContext } from '../../contexts/map';
import merge from 'lodash.merge';

export interface IHMapProps {}
export interface IHMapProps {
loadingEl: React.ReactNode;
style: React.CSSProperties;
container: React.RefObject<HTMLDivElement>;
children: React.ReactNode | React.ReactNode[];
options: {};
}

export const HMap = () => {
export interface IHMapState {}

export const HMap = (props: IHMapProps) => {
// const Platform = useHPlatform()
return <div>HMap</div>;
const platformState = useContext(PlatformContext);

const containerRef = props.container || useRef<HTMLDivElement>();

const [mapState, setMapState] = useState<IHMapState>({});

useEffect(() => {
const mergedOptions = merge(
{
container,
build: true,
},
props.options
);
const buildResult = buildMap(platformState.platform, mergedOptions);

setMapState(buildResult);
}, [props]);

const { style, loadingEl, container, children } = props;
// const { options } = this.state.builder;

const options = {};

const LoadingComponent = () => {
return <div>Loading</div>;
};

const loading = loadingEl || <LoadingComponent />;

// @ts-ignore
const h = window.H;

return (
<MapContext.Provider value={mapState}>
<div
id={defaultOptions.containerId}
className={defaultOptions.defaultClassName}
style={style}
ref={containerRef}
>
{typeof h === 'undefined' && !options && loading}
{typeof h === 'object' && options && children}
</div>
</MapContext.Provider>
);
};
82 changes: 82 additions & 0 deletions src/components/libs/buildMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import initMap from './initMap';
import initInteraction from './initInteraction';
import initDefaultUI from './initDefaultUI';
import initInteractionStyles from './initInteractionStyles';

import dotProp from 'dot-prop';
import { validateMapType } from './validateMapType';
import { MAP_TYPES } from './defaults';

export default (platform, container, mapOptions, mapType: MAP_TYPES) => {
validateMapType(mapType);
// Get all the default layers so we can set which to use based on the map type
const defaultLayers = platform.createDefaultLayers();

// Instantiate (and display) a map object:
return new H.Map(container, dotProp.get(defaultLayers, mapType), mapOptions);
};

/**
* The whole library is bootstrapped after the initialization is done using the options
* @property {string} options.version Version of the api to load. Defaults to v3
* @property {string} options.VERSION Default version Defaults to v3
* @property {object} options.mapEvents Map events implementation
* @property {object} options.mapOptions Options needed to initialize the map
* @property {object} options.platformOptions Options needed to initialize the map
* @property {boolean} options.interactive Adds interactivity to the MAP
* @property {boolean} options.useEvents Adds event handling to the map
* @property {boolean} options.includeUI Add UI script to the MAP
* @property {boolean} options.build Determines if the map should be built
* @property {Node} options.container DOM Element to hold the MAP
* @property {string} options.uiLang Language of the UI
* @property {build} options.build Flag to tell if the MAP should be build immediately
* @property {string} options.appId Here Map APP ID
* @property {string} options.appCode Here Map APP code
* @property {string} options.mapType The type of the map to load e.g // "normal.map"
* @property {string} options.MAP_TYPE Default map type to load "normal.map"
* @param {object} options Items necessary to run the library
* @returns {object}
*/
export const buildMap = (platform, options) => {
// Get values from the options
const {
useEvents,
mapEvents,
interactive,
includeUI,
mapType,
MAP_TYPE,
mapTypes,
mapOptions,
uiLang,
container,
build,
} = options;

const _mapType = mapType || MAP_TYPE;

let ret = { options: { ...options, MAP_TYPE: _mapType }, platform };

if (container && build) {
// Create a Map
ret.map = initMap(platform, container, mapOptions, mapTypes, _mapType);
ret.interaction = initInteraction(
ret.map,
interactive,
useEvents,
mapEvents
);
if (includeUI) {
ret.ui = initDefaultUI(platform, ret.map, includeUI, uiLang);
}
// Adds the grabbing to the document
initInteractionStyles();
} else {
ret.createMap = initMap;
ret.createPlatform = initPlatform;
ret.createInteraction = initInteraction;
ret.createDefaultUI = initDefaultUI;
ret.createInteractionStyles = initInteractionStyles;
}
return ret;
};
59 changes: 31 additions & 28 deletions src/components/libs/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@
* @type {string} Default version for the API
*/
const VERSION = 'v3/3.0';
export type MAP_TYPES =
| 'normal.map'
| 'normal.xbase'
| 'normal.xbasenight'
| 'normal.basen'
| 'normal.basenight'
| 'normal.mapnight'
| 'normal.traffic'
| 'normal.trafficnight'
| 'normal.transit'
| 'normal.panoram'
| 'normal.panoramnight'
| 'normal.labels'
| 'normal.metaInfo'
| 'satellite.xbase'
| 'satellite.base'
| 'satellite.map'
| 'satellite.traffic'
| 'satellite.panorama'
| 'satellite.labels'
| 'terrain.xbase'
| 'terrain.base'
| 'terrain.map'
| 'terrain.traffic'
| 'terrain.panorama'
| 'terrain.labels'
| 'incidents'
| 'venues';

export const mapTypes = [
'normal.map',
'normal.xbase',
'normal.xbasenight',
'normal.basen',
'normal.basenight',
'normal.mapnight',
'normal.traffic',
'normal.trafficnight',
'normal.transit',
'normal.panoram',
'normal.panoramnight',
'normal.labels',
'normal.metaInfo',
'satellite.xbase',
'satellite.base',
'satellite.map',
'satellite.traffic',
'satellite.panorama',
'satellite.labels',
'terrain.xbase',
'terrain.base',
'terrain.map',
'terrain.traffic',
'terrain.panorama',
'terrain.labels',
'incidents',
'venues',
] as const;
export type MAP_TYPES = typeof mapTypes[number];

const MAP_TYPE: MAP_TYPES = 'normal.map';

Expand Down
9 changes: 9 additions & 0 deletions src/components/libs/validateMapType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MAP_TYPES, mapTypes } from './defaults';

export const validateMapType = (mapType: MAP_TYPES) => {
if (!mapTypes.includes(mapType)) {
throw new Error(
'mapType Should be one from https://developer.here.com/documentation/maps/topics/map-types.html in dot notation'
);
}
};
8 changes: 8 additions & 0 deletions src/contexts/map.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// provider a provider
// provider a consumer

import { createContext } from 'react';
import { IHMapState } from '../components/Map';

// create a hook for the platform context
export const MapContext = createContext<IHMapState>({});
5 changes: 0 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ export const Thing: FC<Props> = ({ children }) => {
{
appId: '2Ts3vDUTLPW8kNUtyFRY',
appKey: 'MDivMVFtNkpim-dWuetlWw',
useCIT: true,
useHTTPS: true,
interactive: true,
includeUI: true,
includePlaces: true,
},
<>{children || `the snozzberries taste like snozzberries`}</>
);
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5432,6 +5432,13 @@ dot-case@^3.0.4:
no-case "^3.0.4"
tslib "^2.0.3"

dot-prop@^8.0.2:
version "8.0.2"
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-8.0.2.tgz#afda6866610684dd155a96538f8efcdf78a27f18"
integrity sha512-xaBe6ZT4DHPkg0k4Ytbvn5xoxgpG0jOS1dYxSOwAHPuNLjP3/OzN0gH55SrLqpx8cBfSaVt91lXYkApjb+nYdQ==
dependencies:
type-fest "^3.8.0"

dotenv-expand@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-10.0.0.tgz#12605d00fb0af6d0a592e6558585784032e4ef37"
Expand Down Expand Up @@ -11128,6 +11135,7 @@ string-length@^3.1.0:
strip-ansi "^5.2.0"

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0:
name string-width-cjs
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -11219,6 +11227,7 @@ string_decoder@~1.1.1:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
name strip-ansi-cjs
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -11776,6 +11785,11 @@ type-fest@^2.19.0, type-fest@~2.19:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.19.0.tgz#88068015bb33036a598b952e55e9311a60fd3a9b"
integrity sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA==

type-fest@^3.8.0:
version "3.13.1"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-3.13.1.tgz#bb744c1f0678bea7543a2d1ec24e83e68e8c8706"
integrity sha512-tLq3bSNx+xSpwvAJnzrK0Ep5CLNWjvFTOp71URMaAEWBfRb9nnJiBoUe0tF8bI4ZFO3omgBR6NvnbzVUT3Ly4g==

type-is@~1.6.18:
version "1.6.18"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
Expand Down

0 comments on commit de568a7

Please sign in to comment.