georoute.js Module Documentation

Bahasa Indonesia

georoute.js finds the shortest route along the lines in your own GeoJSON data — roads, alleys, campus paths, building corridors. The route is computed in the browser: no routing service is called, no API key is needed, and it keeps working offline as long as the data is already loaded.

This module is pure computation. It doesn’t need a map; the result is plain GeoJSON that can be drawn with map.js or used as-is to get the travel distance.

Usage (Import)

import { findRoute } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/georoute.js";

All geomeasure.js functions (getDistance, getLength, isInside, and the rest) are also exported from georoute.js, so a page that computes routes only needs to import from here.

findRoute(from, to, geojson, options)

Finds the shortest route from from to to (both [lng, lat]) along the LineString and MultiLineString geometries in geojson. Points and polygons in the data are ignored.

Returns a Feature with a LineString geometry and properties.distance holding the route length in meters — or null if the data has no lines, or the two points are not connected.

const jalan = {
    type: "FeatureCollection",
    features: [
        { type: "Feature", properties: {}, geometry: { type: "LineString", coordinates: [[107.60, -6.90], [107.61, -6.90]] } },
        { type: "Feature", properties: {}, geometry: { type: "LineString", coordinates: [[107.61, -6.90], [107.61, -6.91]] } },
    ],
};

const rute = findRoute([107.604, -6.898], [107.612, -6.907], jalan);

rute.properties.distance;  // 1440.70... (meters)
rute.geometry.coordinates; // [[107.604, -6.9], [107.61, -6.9], [107.61, -6.907]]

In that example neither point lies exactly on a road. from and to can be anywhere: each is snapped to the nearest point on the nearest line, and the route starts and ends at those snapped points. So click coordinates or the user’s location can be used directly.

Drawing the Route on the Map

Since the result is a plain Feature, addGeoJSON can draw it directly:

import { createMap, addGeoJSON, fitMap, getGeoJSON } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/map.js";
import { findRoute } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/georoute.js";

createMap("peta");

getGeoJSON("jalan.geojson", (response) => {
    if (!response.data) return;

    const rute = findRoute([107.575914, -6.8732098], [107.5772, -6.8752], response.data);
    if (!rute) {
        console.log("Kedua titik tidak terhubung.");
        return;
    }
    addGeoJSON("peta", rute, { style: { stroke: "#2563eb", strokeWidth: 5 } });
    fitMap("peta", rute);
    console.log(`Panjang rute: ${Math.round(rute.properties.distance)} m`);
});

A complete example with draggable A and B pins: demo · source.

Data Requirement: Lines Must Meet at the Same Coordinate

Two lines are considered connected only if they share the same coordinate — at their ends or in the middle of a line. This is how OpenStreetMap road data is structured: every intersection is a single point shared by the roads that meet there.

As a result:

For the second case there is the precision option: the number of decimal places used when comparing coordinates. The smaller the value, the more loosely two points are considered equal.

precision Points are considered equal if they are roughly within
6 (default) 11 cm
5 1 m
4 11 m
findRoute(from, to, jalanGambarTangan, { precision: 5 });

If findRoute returns null even though the lines look connected on the map, this is almost always the cause. The cleanest fix is still to correct the data: when drawing, make sure lines that meet really share a single point.

Notes