A pure Zig implementation of Google's Open Location Code (Plus Codes) - a geocoding system for identifying any location on Earth.
- Pure Zig - No dependencies, works with Zig 0.15+
- Zero allocations - All encoding uses caller-provided buffers
- Fully tested - 35 tests including reference cities, edge cases, and fuzz tests
- Complete API - encode, decode, shorten, recover, validate, full/short code detection
Add to your build.zig.zon:
.dependencies = .{
.open_location_code = .{
.url = "https://github.com/bmorphism/open-location-code-zig/archive/refs/tags/v1.1.0.tar.gz",
.hash = "open_location_code-1.0.0-0pnGtSekAABZrcQoWZaEGyrs23Ud7w41iXjqGdz7ZHRh",
},
},Then in your build.zig:
const olc = b.dependency("open_location_code", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("olc", olc.module("olc"));const olc = @import("olc");
pub fn main() void {
var buffer: [20]u8 = undefined;
// Encode coordinates to Plus Code
const len = olc.encode(37.7749, -122.4194, 10, &buffer) catch return;
const code = buffer[0..len];
// code = "849VQHFJ+X6"
// Decode Plus Code to area
const area = olc.decode("849VQHFJ+X6") catch return;
std.debug.print("Center: {d}, {d}\n", .{
area.center_latitude(),
area.center_longitude(),
});
// Validate codes
_ = olc.is_valid("849VQHFJ+X6"); // true
_ = olc.is_full("849VQHFJ+X6"); // true
_ = olc.is_short("QHFJ+X6"); // true
}Encodes latitude/longitude to a Plus Code. Returns the number of bytes written.
lat: Latitude (-90 to 90)lng: Longitude (-180 to 180)code_length: Desired code length (2-15, default 10)buffer: Output buffer (minimum 12 bytes for length 10)
Decodes a Plus Code to a CodeArea struct with:
south_latitude,north_latitudewest_longitude,east_longitudecenter_latitude(),center_longitude()methods
Returns true if the code is a valid Plus Code.
Returns true if the code is a full (not shortened) Plus Code.
Returns true if the code is a shortened Plus Code.
Shortens a full Plus Code relative to a reference location. The closer the reference, the shorter the result.
Recovers a full Plus Code from a short code and reference location.
| Location | Coordinates | Plus Code |
|---|---|---|
| San Francisco | 37.7749, -122.4194 | 849VQHFJ+X6 |
| London | 51.5074, -0.1278 | 9C3XGV4C+XV |
| Tokyo | 35.6762, 139.6503 | 8Q7XMMG2+F4 |
| Sydney | -33.8688, 151.2093 | 4RRH46J5+FP |
| Origin (0,0) | 0.0, 0.0 | 6FG22222+22 |
zig build testApache License 2.0 - See LICENSE
Barton Rhodes freemorphism+zig@gmail.com
Based on Google's Open Location Code specification. Cross-validated against Python and Go reference implementations.