立即开始
¥Getting started
如何将 Turf 添加到你的项目 中取决于你的环境和工具,但以下是一些入门指南。
¥How you add Turf to your project will dependend on your environment and tooling but here are some guidelines to get you started.
Turf 已发布版本的可信来源是 NPM。欢迎你使用重新发布这些软件包的其他提供商。
¥The source of truth for published versions of Turf is NPM. You are welcome to use other providers that republish these packages.
安装
¥Installation
在 Node.js 中
¥In Node.js
# get all of turf
npm install @turf/turf
# or get individual packages
npm install @turf/helpers
npm install @turf/buffer
作为 v7 版本包含 CommonJS 和 ESM 软件包。
¥As of v7, both CommonJS and ESM bundles are included.
在浏览器中
¥In browser
无论是本地下载,还是直接包含第三方版本的 Turf,都有多个 CDN 可供选择,每个 CDN 都有一个 URL 方案,允许你灵活地指定所需的版本。根据你的需求构建合适的 URL:
¥Whether downloading locally, or including a 3rd party version of turf directly, there are multiple CDN's to choose from and each has a URL scheme that allows you to specify what version you want, with some flexibility. Structure your URL as appropriate for your needs:
-
-
主版本中的最新版本:https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js
¥latest within major version: https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js
-
最新在次要版本中:https://cdn.jsdelivr.net/npm/@turf/turf@7.0/turf.min.js
¥latest within minor version: https://cdn.jsdelivr.net/npm/@turf/turf@7.0/turf.min.js
-
具体版本:https://cdn.jsdelivr.net/npm/@turf/turf@7.0.0/turf.min.js
¥specific version: https://cdn.jsdelivr.net/npm/@turf/turf@7.0.0/turf.min.js
-
-
主版本中的最新版本:https://unpkg.com/@turf/turf@^7/turf.min.js
¥latest within major version: https://unpkg.com/@turf/turf@^7/turf.min.js
-
最新在次要版本中:https://unpkg.com/@turf/turf@^7.0/turf.min.js
¥latest within minor version: https://unpkg.com/@turf/turf@^7.0/turf.min.js
-
具体版本:https://unpkg.com/@turf/turf@7.0.0/turf.min.js
¥specific version: https://unpkg.com/@turf/turf@7.0.0/turf.min.js
例如,下载 最新压缩版本 7,并将其包含在脚本标签中。这将公开一个名为 turf
的全局变量。
¥For example, download the latest minified version 7, and include it in a script tag. This will expose a global variable named turf
.
<script src="turf.min.js" charset="utf-8"></script>
你也可以直接从 CDN 中获取。此示例指定了 v7 中的最新版本。
¥You can also include it directly from a CDN. This example specifies the latest version within v7.
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>
不建议使用提供 Turf latest
最新版的 CDN URL,尤其是在生产应用中。由于 Turf 函数始终向浏览器用户提供最新版本,因此在主要版本之间,Turf 函数存在重大变更,这可能会导致你的应用处于崩溃状态。
¥It is not recommended to use a CDN URL that gives you the latest
bleeding edge version of Turf, especially in a production app. There are breaking changes to turf functions between major versions that can leave your app in a broken state because it always gives your browser users the latest version.
TypeScript
每个 Turf 模块都包含并导出 TypeScript 定义,但 GeoJSON 类型定义(例如 Polygon
、FeatureCollection
)除外,这些定义由 @types/geojson 软件包提供。Turf 不会重新导出这些类型定义。如果你需要它们,你可以直接导入并使用它们,例如 import { Polygon, FeatureCollection } from 'geojson'
。你可能需要先安装 @types/geojson
软件包。
¥TypeScript definitions are included and exported by each Turf module, except for GeoJSON type definitions (e.g. Polygon
, FeatureCollection
) which are provided by the @types/geojson package. Turf does not re-export these type definitionas. If you need them, you can import and use them directly, e.g. import { Polygon, FeatureCollection } from 'geojson'
. You may need to install the @types/geojson
package first.