@aws/polyline

中文文档
0.1.4发布于 2025/10/22MITTypeScriptESM + CJS构建来源已验证近 30 天下载 5424

A library for encoding and decoding polylines

原始使用文档
文档目录17 节
  1. Installation
  2. Usage
  3. Usage with Modules
  4. Usage with a browser
  5. Documentation
  6. getCompressionAlgorithm
  7. setCompressionAlgorithm
  8. encodeFromLngLatArray
  9. decodeToLineStringFeature
  10. decodeToPolygonFeature
  11. decodeToLineString
  12. decodeToPolygon
  13. decodeToLngLatArray
  14. Security
  15. Getting Help
  16. Contributing
  17. License

@aws/polyline

This library simplifies the process of using compressed geometry with maplibre-gl-js in JavaScript applications.

Location-based service providers sometimes use variations of the Encoded Polyline Algorithm Format to reduce the response size for APIs that can return large arrays of coordinates, such as routing and isoline APIs. The utility methods in this library compresses the data and decompresses into GeoJSON that can be directly rendered in MapLibre.

Installation

Install this library from NPM for usage with modules:

npm install @aws/polyline

You can also import the Javascript file for usage directly in the browser.

<script src="https://cdn.jsdelivr.net/npm/@aws/polyline/dist/polyline.min.js"></script>

Usage

Import the library and call the utility functions in the top-level namespace as needed. You can find more details about these functions in the Documentation section.

Usage with Modules

import { decodeToLineStringFeature } from "@aws/polyline";

var decodedGeoJSON = decodeToLineStringFeature(response.EncodedPolyline);
map.addLayer({
  id: "route",
  type: "line",
  source: {
    type: "geojson",
    data: decodedGeoJSON,
  },
  layout: {
    "line-join": "round",
    "line-cap": "round",
  },
  paint: {
    "line-color": "#3887be",
    "line-width": 5,
    "line-opacity": 0.75,
  },
});

Usage with a browser

<!-- Import the Polyline library -->
<script src="https://cdn.jsdelivr.net/npm/@aws/polyline/dist/polyline.min.js"></script>
var decodedGeoJSON = polyline.decodeToLineStringFeature(
  response.EncodedPolyline,
);
map.addLayer({
  id: "route",
  type: "line",
  source: {
    type: "geojson",
    data: decodedGeoJSON,
  },
  layout: {
    "line-join": "round",
    "line-cap": "round",
  },
  paint: {
    "line-color": "#3887be",
    "line-width": 5,
    "line-opacity": 0.75,
  },
});

Documentation

Detailed documentation can be found under /docs/index.html after generating it by running:

npm run typedoc

getCompressionAlgorithm

Returns the currently-selected compression algorithm. This can be either FlexiblePolyline, Polyline5, or Polyline6.

const compressionType = polyline.getCompressionAlgorithm();

setCompressionAlgorithm

Sets the compression algorithm to use for subsequent encode/decode calls. This can be either FlexiblePolyline, Polyline5, or Polyline6.

polyline.setCompressionAlgorithm(polyline.FlexiblePolyline);

encodeFromLngLatArray

This encodes an array of coordinates in longitude, latitude order into compressed polyline data. The data can include an optional 3rd dimension when encoding with the FlexiblePolyline algorithm. While this isn't needed for MapLibre rendering, you might need it to compress data for a request to a Location Service Provider when requesting route-related data.

const polylineString = polyline.encodeFromLngLatArray([
  [5.0, 0.0],
  [10.0, 0.0],
]);

decodeToLineStringFeature

This is the most common method to use. It decodes compressed polyline data into a GeoJSON Feature containing a LineString that can directly be used as a MapLibre source for rendering.

const geoJsonLineStringFeature =
  polyline.decodeToLineStringFeature(polylineString);

decodeToPolygonFeature

Similar to decodeToLineStringFeature it decodes an array of compressed polyline rings into a GeoJSON Feature containing a Polygon that can directly be used as a MapLibre source for rendering. This should be used when the compressed data is meant to represent polygon rings, as it will also generate the correct winding order of the rings for use with GeoJSON.

const geoJsonPolygonFeature = polyline.decodeToPolygonFeature(polylineString);

decodeToLineString

This decodes a compressed polyline into a GeoJSON LineString. The LineString can be embedded into the geometry section of a GeoJSON Feature which can then be rendered with MapLibre.

const geoJsonLineString = polyline.decodeToLineString(polylineString);

decodeToPolygon

This decodes an array of compressed polyline rings into a GeoJSON Polygon. The Polygon can be embedded into the geometry section of a GeoJSON Feature which can then be rendered with MapLibre.

const geoJsonPolygon = polyline.decodeToPolygon(polylineString);

decodeToLngLatArray

This decodes compressed polyline data into an array of coordinates in longitude, latitude order. This method is helpful when you need to directly work with the coordinate data.

const lngLatArray = polyline.decodeToLngLatArray(polylineString);

Security

See CONTRIBUTING for more information.

Getting Help

The best way to interact with our team is through GitHub. You can open an issue and choose from one of our templates for bug reports or feature requests. If you have a support plan with AWS Support, you can also create a new support case.

Contributing

We welcome community contributions and pull requests. See CONTRIBUTING for information on how to set up a development environment and submit code.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

版本与发布记录

@aws/polyline 共发布 4 个版本,首次发布于 2024/10/30;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.4latest2025/10/221 年前
0.1.32025/09/161.1 年前
0.1.22025/07/161.3 年前
0.1.02024/10/302 年前

关于 @aws/polyline 的常见问题

@aws/polyline 是什么?

A library for encoding and decoding polylines

如何安装 @aws/polyline?

在项目目录运行 npm install @aws/polyline,或使用 pnpm add / yarn add / bun add。

@aws/polyline 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws/polyline 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aws/polyline 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws/polyline 可以商用吗?

@aws/polyline 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aws/polyline 的下载量有多少?

根据 npm 官方下载统计,@aws/polyline 在 2026/10/08 – 2026/11/06 这 30 天内被下载 5,424 次,日均约 181 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/polyline 还在维护吗?

最新版本 0.1.4 发布于 2025/10/22(1 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/10/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。