Skip to content

Commit

Permalink
Merge pull request #164 from Disfactory/bugfix/same-point
Browse files Browse the repository at this point in the history
fix: de-dup factory point
  • Loading branch information
Yukaii authored Aug 17, 2022
2 parents a9f4eef + 99ee51c commit 72a10e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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

0 comments on commit 72a10e5

Please sign in to comment.