Định dạng thuật toán đa tuyến đã mã hoá
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Mã hoá polyline là thuật toán nén có tổn hao cho phép bạn lưu trữ chuỗi
các toạ độ dưới dạng một chuỗi đơn. Toạ độ điểm được mã hoá bằng các giá trị đã ký.
Nếu chỉ có một vài điểm tĩnh, bạn cũng có thể sử dụng tính năng
tiện ích mã hoá nhiều dòng.
Quá trình mã hoá sẽ chuyển đổi một giá trị nhị phân thành một chuỗi mã ký tự cho
Ký tự ASCII sử dụng lược đồ mã hoá base64 quen thuộc: để đảm bảo hiển thị chính xác
trong số các ký tự này, các giá trị được mã hoá được cộng lại bằng 63 (ký tự ASCII '?')
trước khi chuyển đổi chúng thành ASCII. Thuật toán này cũng kiểm tra các bước bổ sung
các mã ký tự cho một điểm nhất định bằng cách kiểm tra bit có ý nghĩa nhỏ nhất của mỗi mã
nhóm byte; nếu bit này được đặt bằng 1 thì điểm đó chưa được định dạng đầy đủ và
dữ liệu bổ sung sẽ đi kèm.
Ngoài ra, để tiết kiệm không gian, điểm chỉ bao gồm phần bù trừ của
điểm trước (tất nhiên ngoại trừ điểm đầu tiên). Tất cả các điểm đều được mã hoá
trong Base64 dưới dạng số nguyên có dấu, vì vĩ độ và kinh độ là các giá trị có dấu.
Định dạng mã hoá trong hình nhiều đường cần biểu thị 2 toạ độ
thể hiện vĩ độ và kinh độ với độ chính xác hợp lý. Đã cho tối đa
kinh độ +/- 180 độ đến độ chính xác là 5 chữ số thập phân
(180.00000 đến -180.00000), điều này dẫn đến nhu cầu có chữ ký 32 bit
giá trị số nguyên nhị phân.
Xin lưu ý rằng dấu gạch chéo ngược được hiểu là ký tự thoát
trong giá trị cố định kiểu chuỗi. Mọi kết quả đầu ra của phần mềm tiện ích này phải chuyển đổi dấu gạch chéo ngược
thành dấu gạch chéo ngược hai lần trong giá trị cố định kiểu chuỗi.
Các bước mã hoá giá trị đã ký như vậy được nêu rõ bên dưới.
- Lấy giá trị có dấu ban đầu:
-179.9832104
- Lấy giá trị thập phân và nhân với 1e5, làm tròn kết quả:
-17998321
- Chuyển đổi giá trị thập phân sang nhị phân. Lưu ý rằng giá trị âm phải là
được tính theo công thức
phần bù của 2 bằng cách đảo ngược giá trị nhị phân rồi cộng thêm một vào kết quả:
00000001 00010010 10100001 11110001
11111110 11101101 01011110 00001110
11111110 11101101 01011110 00001111
- Di chuyển sang trái giá trị nhị phân một bit:
11111101 11011010 10111100 00011110
- Nếu giá trị thập phân ban đầu là số âm, hãy đảo ngược phương thức mã hoá này:
00000010 00100101 01000011 11100001
- Chia giá trị nhị phân thành các đoạn 5 bit (bắt đầu từ phía bên phải):
00001 00010 01010 10000 11111 00001
- Sắp xếp các đoạn 5 bit theo thứ tự đảo ngược:
00001 11111 10000 01010 00010 00001
- HOẶC mỗi giá trị có giá trị 0x20 nếu có một đoạn bit khác theo sau:
100001 111111 110000 101010 100010 000001
- Chuyển đổi từng giá trị thành số thập phân:
33 63 48 42 34 1
- Thêm 63 vào mỗi giá trị:
96 126 111 105 97 64
- Chuyển đổi từng giá trị sang bộ mã ASCII tương đương:
`~oia@
Bảng bên dưới trình bày một số ví dụ về các điểm được mã hoá, hiển thị
mã hoá dưới dạng một chuỗi các giá trị bù trừ từ các điểm trước đó.
Ví dụ:
Điểm: (38,5, -120,2), (40,7, -120,95), (43,252, -126,453)
38,5 |
-120,2 |
3850000 |
-12020000 |
+3850000 |
-12020000 |
_p~iF |
~ps|U |
_p~iF~ps|U |
40,7 |
-120,95 |
4070000 |
-12095000 |
+220000 |
-75000 |
_ulL |
nnqC |
_ulLnnqC |
43,252 |
-126,453 |
4325200 |
-12645300 |
+255200 |
-550300 |
_mqN |
vxq`@ |
_mqNvxq`@ |
Hình nhiều đường được mã hoá: _p~iF~ps|U_ulLnnqC_mqNvxq`@
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-29 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[[["\u003cp\u003ePolyline encoding is a lossy compression algorithm that represents a series of coordinates as a single string.\u003c/p\u003e\n"],["\u003cp\u003eThe algorithm uses signed values, Base64 encoding, and offsets from the previous point to compress the data.\u003c/p\u003e\n"],["\u003cp\u003ePoints are encoded by converting latitude and longitude to binary, applying bitwise operations, and converting the result to ASCII characters.\u003c/p\u003e\n"],["\u003cp\u003eEncoded polylines are strings that consist of these ASCII characters, representing the sequence of geographical points.\u003c/p\u003e\n"]]],["Polyline encoding compresses coordinates into a single string. It encodes points as signed integers representing offsets from the previous point. The process involves multiplying the decimal by 1e5, converting to binary (using two's complement for negative values), left-shifting, inverting if negative, dividing into 5-bit chunks, reversing chunk order, OR-ing with 0x20, adding 63, and converting to ASCII. Points are represented in Base64 and latitude/longitude are paired, encoded sequentially. The result is a compact string representing the sequence of points.\n"],null,["# Encoded Polyline Algorithm Format\n\nPolyline encoding is a lossy compression algorithm that allows you to store a series of\ncoordinates as a single string. Point coordinates are encoded using signed values.\nIf you only have a few static points, you may also wish to use the interactive\n[polyline encoding utility](/maps/documentation/utilities/polylineutility).\n\nThe encoding process converts a binary value into a series of character codes for\nASCII characters using the familiar base64 encoding scheme: to ensure proper display\nof these characters, encoded values are summed with 63 (the ASCII character '?')\nbefore converting them into ASCII. The algorithm also checks for additional\ncharacter codes for a given point by checking the least significant bit of each\nbyte group; if this bit is set to 1, the point is not yet fully formed and\nadditional data must follow.\n\nAdditionally, to conserve space, **points only include the offset from the\nprevious point** (except of course for the first point). All points are encoded\nin Base64 as signed integers, as latitudes and longitudes are signed values.\nThe encoding format within a polyline needs to represent two coordinates\nrepresenting latitude and longitude to a reasonable precision. Given a maximum\nlongitude of +/- 180 degrees to a precision of 5 decimal places\n(180.00000 to -180.00000), this results in the need for a 32 bit signed\nbinary integer value.\n\nNote that the backslash is interpreted as an escape character within string literals. Any output of this utility should convert backslash\ncharacters to double-backslashes within string literals.\n\nThe steps for encoding such a signed value are specified below.\n\n1. Take the initial signed value: \n `-179.9832104`\n2. Take the decimal value and multiply it by 1e5, rounding the result: \n `-17998321`\n3. Convert the decimal value to binary. Note that a negative value must be calculated using its [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) by inverting the binary value and adding one to the result: \n\n ```\n 00000001 00010010 10100001 11110001\n 11111110 11101101 01011110 00001110\n 11111110 11101101 01011110 00001111\n ```\n4. Left-shift the binary value one bit: \n `11111101 11011010 10111100 00011110`\n5. If the original decimal value is negative, invert this encoding: \n `00000010 00100101 01000011 11100001`\n6. Break the binary value out into 5-bit chunks (starting from the right hand side): \n `00001 00010 01010 10000 11111 00001`\n7. Place the 5-bit chunks into reverse order: \n `00001 11111 10000 01010 00010 00001`\n8. OR each value with 0x20 if another bit chunk follows: \n `100001 111111 110000 101010 100010 000001`\n9. Convert each value to decimal: \n `33 63 48 42 34 1`\n10. Add 63 to each value: \n `96 126 111 105 97 64`\n11. Convert each value to its ASCII equivalent: \n ```~oia@``\n\nThe table below shows some examples of encoded points, showing the\nencodings as a series of offsets from previous points. \n\n### Example\n\nPoints: (38.5, -120.2), (40.7, -120.95), (43.252, -126.453)\n\n|----------|-----------|----------------|-----------------|--------------------|---------------------|------------------|-------------------|---------------|\n| Latitude | Longitude | Latitude in E5 | Longitude in E5 | Change In Latitude | Change In Longitude | Encoded Latitude | Encoded Longitude | Encoded Point |\n| 38.5 | -120.2 | 3850000 | -12020000 | +3850000 | -12020000 | `_p~iF` | `~ps|U` | `_p~iF~ps|U` |\n| 40.7 | -120.95 | 4070000 | -12095000 | +220000 | -75000 | `_ulL` | `nnqC` | `_ulLnnqC` |\n| 43.252 | -126.453 | 4325200 | -12645300 | +255200 | -550300 | `_mqN` | ``vxq`@`` | ``_mqNvxq`@`` |\n\n**Encoded polyline** : ``_p~iF~ps|U_ulLnnqC_mqNvxq`@``"]]