Fix binding with the new MarkerClusterer from @googlemaps/markerclusterer

This commit is contained in:
danix89
2022-11-17 18:59:00 +01:00
parent 08b742c7c6
commit ae2985f9b1

View File

@@ -4,72 +4,26 @@
</div> </div>
</template> </template>
<script> <script>
import { MarkerClusterer } from "@googlemaps/markerclusterer"; import {DefaultRenderer, MarkerClusterer, SuperClusterAlgorithm} from '@googlemaps/markerclusterer';
import buildComponent from './build-component.js' import buildComponent from './build-component.js';
const props = { const props = {
maxZoom: { algorithm: {
type: Number, type: Object,
twoWay: false, default: new SuperClusterAlgorithm({}),
noBind: true,
}, },
batchSizeIE: { renderer: {
type: Number, type: Object,
twoWay: false, default: new DefaultRenderer,
noBind: true,
}, },
calculator: { };
type: Function,
twoWay: false,
},
enableRetinaIcons: {
type: Boolean,
twoWay: false,
},
gridSize: {
type: Number,
twoWay: false,
},
ignoreHidden: {
type: Boolean,
twoWay: false,
},
imageExtension: {
type: String,
twoWay: false,
},
imagePath: {
type: String,
twoWay: false,
},
imageSizes: {
type: Array,
twoWay: false,
},
minimumClusterSize: {
type: Number,
twoWay: false,
},
styles: {
type: Array,
twoWay: false,
},
zoomOnClick: {
type: Boolean,
twoWay: false,
},
}
const events = [ const events = [
'click', 'clusteringbegin',
'rightclick', 'clusteringend',
'dblclick', ];
'drag',
'dragstart',
'dragend',
'mouseup',
'mousedown',
'mouseover',
'mouseout',
]
export default buildComponent({ export default buildComponent({
mappedProps: props, mappedProps: props,
@@ -79,26 +33,30 @@ export default buildComponent({
if (typeof MarkerClusterer === 'undefined') { if (typeof MarkerClusterer === 'undefined') {
const errorMessage = 'MarkerClusterer is not installed!'; const errorMessage = 'MarkerClusterer is not installed!';
console.error(errorMessage); console.error(errorMessage);
throw new Error(errorMessage) throw new Error(errorMessage);
} }
return MarkerClusterer return MarkerClusterer;
}, },
ctrArgs: ({ map, ...otherOptions }) => [map, [], otherOptions], ctrArgs: ({map, ...otherOptions}) => [{
map,
algorithm: otherOptions.algorithm,
renderer: otherOptions.renderer,
}],
afterCreate(inst) { afterCreate(inst) {
const reinsertMarkers = () => { const reinsertMarkers = () => {
const oldMarkers = inst.getMarkers() const oldMarkers = inst.getMarkers();
inst.clearMarkers() inst.clearMarkers();
inst.addMarkers(oldMarkers) inst.addMarkers(oldMarkers);
} };
for (let prop in props) { for (let prop in props) {
if (props[prop].twoWay) { if (props[prop].twoWay) {
this.$on(prop.toLowerCase() + '_changed', reinsertMarkers) this.$on(prop.toLowerCase() + '_changed', reinsertMarkers);
} }
} }
}, },
updated() { updated() {
if (this.$clusterObject) { if (this.$clusterObject) {
this.$clusterObject.render() this.$clusterObject.render();
} }
}, },
beforeUnmount() { beforeUnmount() {
@@ -106,15 +64,14 @@ export default buildComponent({
if (this.$children && this.$children.length) { if (this.$children && this.$children.length) {
this.$children.forEach((marker) => { this.$children.forEach((marker) => {
if (marker.$clusterObject === this.$clusterObject) { if (marker.$clusterObject === this.$clusterObject) {
marker.$clusterObject = null marker.$clusterObject = null;
} }
}) });
} }
if (this.$clusterObject) { if (this.$clusterObject) {
this.$clusterObject.clearMarkers() this.$clusterObject.clearMarkers();
} }
}, },
}) });
</script> </script>