@vuemap/vue-amap
Version:
高德地图vue3版本封装
97 lines (94 loc) • 3.2 kB
JavaScript
import { defineComponent, ref, useSlots, openBlock, createElementBlock, createBlock, Teleport, renderSlot, createCommentVNode } from 'vue';
import '../../../mixins/index.mjs';
import { buildProps } from '../../../utils/buildHelper.mjs';
import '../../../utils/index.mjs';
import guid from '../../../utils/guid.mjs';
import { propsTypes } from './props.mjs';
import { useRegister } from '../../../mixins/useRegister.mjs';
import { isMapInstance, isOverlayGroupInstance } from '../../../utils/util.mjs';
const _hoisted_1 = { style: { "display": "none" } };
var script = /* @__PURE__ */ defineComponent({
...{
name: "ElAmapMarker",
inheritAttrs: false
},
__name: "Marker",
props: buildProps(propsTypes),
emits: ["init", "update:position"],
setup(__props, { expose: __expose, emit: __emit }) {
const props = __props;
const emits = __emit;
const tempId = `marker-${guid()}`;
const divId = ref("");
let $amapComponent;
let withSlot = false;
const $slots = useSlots();
const { $$getInstance, parentInstance } = useRegister((options, parentComponent) => {
return new Promise((resolve) => {
if ($slots.default && $slots.default().length > 0) {
withSlot = true;
options.content = `<div id="${tempId}"></div>`;
}
$amapComponent = new AMap.Marker(options);
if (isMapInstance(parentComponent)) {
parentComponent.add($amapComponent);
} else if (isOverlayGroupInstance(parentComponent)) {
parentComponent.addOverlay($amapComponent);
}
if (withSlot) {
divId.value = tempId;
}
bindModelEvents();
resolve($amapComponent);
});
}, {
emits,
watchRedirectFn: {
__position(position) {
if (!props.moveOptions) {
$amapComponent.setPosition(position);
return;
}
if (parentInstance == null ? void 0 : parentInstance.$amapComponent) {
parentInstance.$amapComponent.plugin("AMap.MoveAnimation", () => {
$amapComponent.moveTo(position, props.moveOptions);
});
}
}
},
destroyComponent() {
if ($amapComponent && (parentInstance == null ? void 0 : parentInstance.$amapComponent)) {
$amapComponent.setMap(null);
$amapComponent = null;
}
}
});
const bindModelEvents = () => {
$amapComponent.on("dragend", () => {
emitPosition();
});
$amapComponent.on("touchend", () => {
emitPosition();
});
};
const emitPosition = () => {
const position = $amapComponent.getPosition();
emits("update:position", position.toArray());
};
__expose({
$$getInstance
});
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div", _hoisted_1, [
!!divId.value ? (openBlock(), createBlock(Teleport, {
key: 0,
to: "#" + divId.value
}, [
renderSlot(_ctx.$slots, "default")
], 8, ["to"])) : createCommentVNode("v-if", true)
]);
};
}
});
export { script as default };
//# sourceMappingURL=Marker.vue2.mjs.map