经纬志
首页 / Turf.js / API 文档

center

先看结论

返回所有输入要素包围盒的几何中心,用一个点代表整体位置。

最后验证 2026-11-03 · Turf.js 7.x · 更新 2026-10-29 · 引用本文
turf.center(geojson, options?) → Feature<Point>

参数

参数类型说明
geojsonGeoJSON任意 Feature、FeatureCollection 或几何
options.propertiesobject写入结果点的属性,默认空对象
options.bboxBBox写入结果点的 bbox 字段
options.idstring | number写入结果点的 id

计算方式

center 先求整体包围盒,再取包围盒的中点: [(minX + maxX) / 2, (minY + maxY) / 2]。 它不考虑要素数量、面积或顶点分布,因此结果与「重心」不是一回事。

函数算法结果位置
turf.center包围盒中点总在包围盒内部
turf.centroid所有顶点坐标平均偏向顶点密集一侧
turf.centerOfMass按面积加权形状的重心,凹形可能在外部

示例

var sites = turf.featureCollection([
  turf.point([116.391, 39.907]),
  turf.point([116.432, 39.934]),
  turf.point([116.355, 39.881])
]);

var middle = turf.center(sites, {
  properties: { role: '视野中心' }
});

console.log(middle.geometry.coordinates);

把结果交给地图的 setCenter 或 flyTo 即可定位到数据中部。 需要连范围一起纳入视野时,用 turf.bbox 配 fitBounds 更合适。

面要素同样适用

var labelPoint = turf.center(polygon);

L.marker([
  labelPoint.geometry.coordinates[1],
  labelPoint.geometry.coordinates[0]
]).bindTooltip('片区中心').addTo(map);

对于狭长或 L 形面,包围盒中点可能落在面外。 标注要求必须落在面内时,改用 turf.pointOnFeature, 它保证返回位于几何内部的一个点。

空输入

空的 FeatureCollection 没有包围盒,结果坐标会是 NaN 或抛出异常。 批量计算前先过滤空集合,避免错误值传到地图接口。

包围盒中心不等于视觉中心。 数据在经度上分布不均时,中心点可能落在没有要素的区域。 展示用途可以接受;分析用途要按业务含义选择 centroid 或 centerOfMass。

引用本文

经纬志(2026)。《center》。https://www.jingweizhi.com/turf/api/center.html(访问日期 2026-11-03)。