Skip to content

Commit

Permalink
feat: add map component
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongkyuuuu committed Aug 24, 2021
1 parent 9ce9bab commit aa28d65
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 104 deletions.
128 changes: 111 additions & 17 deletions src/apis/useMap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,107 @@
/*
export interface useMapMethods {
setLayerTypeId(typeId: string): void;

naver.maps.Map Methods
addPane(name: string, elementOrZIndex: HTMLElement | number): void;

*/
function _useMapMethods(map: naver.maps.Map) {
destroy(): void;

fitBounds(
bounds:
| naver.maps.Bounds
| naver.maps.BoundsLiteral
| naver.maps.ArrayOfCoords
| naver.maps.ArrayOfCoordsLiteral,
margin: number
): void;

morph(
coord: naver.maps.Coord | naver.maps.CoordLiteral,
zoom?: number,
transitionOptions?: naver.maps.TransitionOptions
): void;

panTo(
coord: naver.maps.Coord | naver.maps.CoordLiteral,
transitionOptions: naver.maps.TransitionOptions
): void;

panToBounds(
bounds: naver.maps.Bounds | naver.maps.BoundsLiteral,
transitionOptions: naver.maps.TransitionOptions,
margin: naver.maps.Margin
): void;

panBy(x: number, y: number): void;

refresh(noEffect: boolean): void;

removePane(name: string): void;

updateBy(
coord: naver.maps.Coord | naver.maps.CoordLiteral,
zoom: number
): void;

zoomBy(
deltaZoom: number,
zoomOrigin: naver.maps.Coord | naver.maps.CoordLiteral | undefined,
effect: boolean
): void;
}

export interface useMapGetterMethods {
getBounds(): naver.maps.Bounds;

getCenter(): naver.maps.Coord;

getCenterPoint(): naver.maps.Coord;

getElement(): HTMLElement;

getMapTypeId(): string;

getOptions(key: string | undefined): any;

getPanes(): naver.maps.MapPanes;

getPrimitiveProjection(): naver.maps.Projection;

getProjection(): naver.maps.MapSystemProjection;

getSize(): naver.maps.Size;

getZoom(): number;
}

export interface useMapSetterMethods {
setCenter(
latOrLatLng: naver.maps.LatLng | naver.maps.LatLngLiteral | number,
lng: number
): void;

setCenterPoint(point: naver.maps.Point | naver.maps.PointLiteral): void;

setMapTypeId(type: "NORMAL" | "TERRAIN" | "SATELLITE" | "HYBRID"): void;

setOptions(
optionsOrKey: naver.maps.MapOptions | string,
value: naver.maps.MapOptions | null
): void;

setSize(size: naver.maps.Size | naver.maps.SizeLiteral): void;

setZoom(level: number, useEffect: boolean): void;
}

export interface useMap
extends useMapMethods,
useMapGetterMethods,
useMapSetterMethods {}

/**
* naver.maps.Map Methods
*/
function _useMapMethods(map: naver.maps.Map): useMapMethods {
const setLayerTypeId = (typeId: string) => {
map.mapTypes.setLayerTypeId(typeId);
};
Expand Down Expand Up @@ -100,12 +198,10 @@ function _useMapMethods(map: naver.maps.Map) {
};
}

/*
naver.maps.Map Getter Methods
*/
function _useMapGetterMethods(map: naver.maps.Map) {
/**
* naver.maps.Map Getter Methods
*/
function _useMapGetterMethods(map: naver.maps.Map): useMapGetterMethods {
const getBounds = (): naver.maps.Bounds => {
return map.getBounds();
};
Expand Down Expand Up @@ -165,12 +261,10 @@ function _useMapGetterMethods(map: naver.maps.Map) {
};
}

/*
naver.maps.Map Setter Methods
*/
function _useMapSetterMethods(map: naver.maps.Map) {
/**
* naver.maps.Map Setter Methods
*/
function _useMapSetterMethods(map: naver.maps.Map): useMapSetterMethods {
const setCenter = (
latOrLatLng: naver.maps.LatLng | naver.maps.LatLngLiteral | number,
lng: number = 0
Expand Down Expand Up @@ -223,7 +317,7 @@ function _useMapSetterMethods(map: naver.maps.Map) {
};
}

export function useMap(map: naver.maps.Map) {
export function useMap(map: naver.maps.Map): useMap {
return {
..._useMapMethods(map),
..._useMapGetterMethods(map),
Expand Down
23 changes: 15 additions & 8 deletions src/components/Map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
} from "vue";
import { useMapInitOptions } from "../utils";
import type { naverV3 } from "../types";
// import { useMap } from "@/apis";
export default defineComponent({
name: "Map",
Expand All @@ -27,34 +26,42 @@ export default defineComponent({
required: true,
},
initLayers: {
type: Array as PropType<naverV3.initLayers>,
type: Array as PropType<naverV3.initLayer[]>,
default: [],
},
},
setup: (props, { emit }) => {
const map = ref<naver.maps.Map | null>(null);
// const { setOptions } = useMap(map.value!);
const { width, height, mapOptions, initLayers } = toRefs(props);
const { mapSettings } = useMapInitOptions();
const initNaverMap = () => {
map.value = null;
const settings = mapSettings(mapOptions.value, initLayers.value);
map.value = new window.naver.maps.Map("vue-naver-maps", {
...settings,
...mapOptions.value,
});
emit("updateMap", map.value);
};
const loadNaverMap = () => {
if (map.value) return;
onBeforeMount(() => {
document.getElementById("vue3-naver-maps")!.onload = () => {
window.naver.maps.onJSContentLoaded = () => initNaverMap();
};
};
onBeforeMount(() => loadNaverMap());
});
onUnmounted(() => (map.value = null));
// watch(
// () => mapOptions.value,
// () => {
// console.log("hello");
// initNaverMap();
// },
// { deep: true, immediate: false }
// );
return {
width,
height,
Expand Down
20 changes: 10 additions & 10 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
declare namespace NodeJS {
interface Process {
browser: boolean;
}
}
declare interface Window {
$naverMapsCallback: [];
$naverMapsLoaded: boolean;
$naverMapsObject: naver.maps.Map;
}
// declare namespace NodeJS {
// interface Process {
// browser: boolean;
// }
// }
// declare interface Window {
// $naverMapsCallback: [];
// $naverMapsLoaded: boolean;
// $naverMapsObject: naver.maps.Map;
// }
3 changes: 1 addition & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export namespace naverV3 {
| "CHINESE"
| "JAPANESE";

export type initLayers = initLayer[];

export interface mapOptions {
zoom?: number;
lat?: number;
Expand All @@ -50,5 +48,6 @@ export namespace naverV3 {
zoomControlOptions?: {
position?: string;
};
mapTypeControl?: boolean;
}
}
10 changes: 7 additions & 3 deletions src/useApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// import { inject } from "vue";
// import { routerKey, routeLocationKey } from "./injectionSymbols";
// import { Router } from "./router";
import { inject, InjectionKey } from "vue";
import type { useMap as useMapType } from "./apis";

export function useMap() {
const mapSymbol: InjectionKey<useMapType> = Symbol("[vue3-naver-maps]useMap");
return inject(mapSymbol);
}
4 changes: 1 addition & 3 deletions src/utils/event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { App } from "vue";

export function addEvent(app: App, target: any, name: any) {
export function addEvent(target: any, name: any) {
naver.maps.Event.addListener(target, name, (event) => {});
}
4 changes: 2 additions & 2 deletions src/utils/mapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { naverV3 } from "../types";
export function useMapInitOptions() {
const mapLayers = (
settings: naver.maps.MapOptions,
initLayers: naverV3.initLayers
initLayers: naverV3.initLayer[]
) => {
const layers: naverV3.layers = {
BACKGROUND: "bg",
Expand Down Expand Up @@ -35,7 +35,7 @@ export function useMapInitOptions() {

const mapSettings = (
mapOptions: naverV3.mapOptions,
initLayers: naverV3.initLayers
initLayers: naverV3.initLayer[]
) => {
const settings: naver.maps.MapOptions = {
maxZoom: 21,
Expand Down
59 changes: 0 additions & 59 deletions webpack.config.js

This file was deleted.

0 comments on commit aa28d65

Please sign in to comment.