Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: de-dup factory point #164

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/lib/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import MVT from 'ol/format/MVT'
import VectorTileRenderType from 'ol/layer/VectorTileRenderType'
import stylefunction from 'ol-mapbox-style/dist/stylefunction'
import { baseStyle } from './layerStyle'
import FactoryPointCache from './utils/factoryPointCache'

const getFactoryStatusImage = (status: FactoryDisplayStatusType) => `/images/marker-${status}.svg`
export const getStatusBorderColor = (status: FactoryDisplayStatusType) => {
Expand Down Expand Up @@ -140,10 +141,11 @@ export class MapFactoryController {
style: (feature, resolution) => {
const zoom = this.mapInstance.map.getView().getZoom()!

const clusterDistance = clusterSource.getDistance()
if (zoom > 16) {
if (clusterSource.getDistance() !== 0) clusterSource.setDistance(0)
if (clusterDistance !== 0) clusterSource.setDistance(0)
} else {
if (clusterSource.getDistance() !== CLUSTER_DISTANCE) clusterSource.setDistance(CLUSTER_DISTANCE)
if (clusterDistance !== CLUSTER_DISTANCE) clusterSource.setDistance(CLUSTER_DISTANCE)
}

const features = feature.get('features')
Expand Down Expand Up @@ -218,7 +220,7 @@ export class MapFactoryController {
}
})

const features = factoriesToAdd.map(createFactoryFeature)
const features = factoriesToAdd.map(createFactoryFeature).filter(Boolean) as Feature[]
interface FeatureFactoryStatusMap {
[key: string]: Feature[]
}
Expand Down Expand Up @@ -260,6 +262,11 @@ export class MapFactoryController {
}

private createFactoryFeature (factory: FactoryData) {
const existingFeature = FactoryPointCache.getFactoryCache(factory)
if (existingFeature) {
return null
}

const feature = new Feature({
geometry: new Point(transform([factory.lng, factory.lat], 'EPSG:4326', 'EPSG:3857'))
})
Expand All @@ -272,6 +279,8 @@ export class MapFactoryController {

this.factoryMap.set(factory.id, factory)

FactoryPointCache.setFactoryCache(factory, feature)

return feature
}

Expand Down
19 changes: 19 additions & 0 deletions src/lib/utils/factoryPointCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FactoryData } from '@/types'
import Feature from 'ol/Feature'

const featurePointCachedByCoord = new Map<string, Feature>()
export class FactoryPointCache {
static getFactoryCacheKey (factory: FactoryData) {
return `${factory.lat},${factory.lng}`
}

static getFactoryCache (factory: FactoryData) {
return featurePointCachedByCoord.get(this.getFactoryCacheKey(factory))
}

static setFactoryCache (factory: FactoryData, feature: Feature) {
featurePointCachedByCoord.set(this.getFactoryCacheKey(factory), feature)
}
}

export default FactoryPointCache