首页 > 解决方案 > 超集群返回意外的经度值

问题描述

我正在使用 supercluster 包来计算要使用 React 版本的传单在地图上显示的集群。我尝试聚类的各个位置的纬度在 -34 到 -42 左右,经度值在 172 到 174 左右,但是计算出的集群虽然它们的纬度值似乎正确,但经度值在 5 到 7 左右。

集群与单个位置黑色图钉是集群,绿色图钉是其正确位置的剩余单个站点。

用于计算站点的代码:

const clusterer = new Supercluster<GeoJSONSiteProperties, {}>({ radius: 30 })
const geoJSONSites = sites.map(site => representSiteAsGeoJSON(site))
clusterer.load(geoJSONSites)

//The clusterer will determine which sites are far enough from others to display individually,
//and which should be put in a cluster represented by a pin on the map. Once it has done that
//sort the indidual sites and clusters and update the state to display them on the map. const newClusters: ClusterFeature<{}>[] = []
const newIndividualSites: ISite[] = []
    
clusterer.getClusters([-180, -85, 180, 85 ], mapZoomLevel).forEach((c: GeoJSONSite | ClusterFeature<{}>) =>{

if(c.properties.cluster){
    newClusters.push(c as ClusterFeature<{}>)
}
else{
    newIndividualSites.push(createSiteFromGeoJSONRepresentation(c as GeoJSONSite))
}

})

将站点与 geoJSON 相互转换的代码:

import { Feature, Point } from 'geojson';
import { ISite } from '../model/definitions/data_providers/ISitesProvider';

export type GeoJSONSiteProperties = Omit<ISite, "lat" | "lon"> & { cluster: false }

export type GeoJSONSite = Feature<Point, GeoJSONSiteProperties>

/**
 * Create a representation of a site in the GeoJSON format.
 * @param site
 * @returns 
 */
export function representSiteAsGeoJSON(site: ISite): GeoJSONSite {

    return {
        type: "Feature",
        geometry: {
            type: "Point",
            coordinates: [ site.lat, site.lon ]
        },
        properties: {
            cluster: false,
            roadPositionString: site.roadPositionString,
            postedSpeedLimit: site.postedSpeedLimit,
            hasSampleImages: site.hasSampleImages,
            stations: site.stations,
            address: site.address
        }
    }

}

export function createSiteFromGeoJSONRepresentation(geojsonSite: GeoJSONSite): ISite{

    return {
        
        lat: geojsonSite.geometry.coordinates[0],
        lon: geojsonSite.geometry.coordinates[1],
        
        ...geojsonSite.properties
        
    }

}

有谁知道为什么会发生这种情况?

标签: javascripthtmlgeojson

解决方案


推荐阅读