NepalMap SDK

Google Maps–like JavaScript SDK for Nepal only. Tiles show route names with faded buildings; data is cached on device (IndexedDB) and server.

Features

Quick start (same-origin)

  1. Serve the backend (e.g. node server.js) so /tiles/ and /api/* are available.
  2. (Optional) For Google-based suggestions, set GOOGLE_PLACES_API_KEY or GOOGLE_MAPS_API_KEY in the environment.
  3. In your HTML, load Leaflet and the SDK, then create the map:
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<div id="map" style="height: 400px;"></div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://your-server.com/nepal-map-sdk.js"></script>
<script>
  var map = new NepalMap('map', { zoom: 10, center: [27.7, 85.32] });
  map.loadPlaces();
</script>

Embed on another domain

When your site is not on the same origin as the map server, set baseUrl:

var map = new NepalMap('map', {
  baseUrl: 'https://your-maps-server.com',
  zoom: 10,
  center: [27.7, 85.32]
});

API

Constructor

new NepalMap(containerId, options)

Methods

Search UI

Server-side route (PHP)

Use the route API from PHP for server-side only route calculation (no map; get distance, time, legs). All points must be within Nepal. The backend caches results and returns full route on first request.

Base URL: your NepalMap server (e.g. http://localhost:3000 or https://maps.example.com).

PHP functions

<?php
/**
 * Nepal bounds (for validation). [south, west, north, east]
 */
function nepal_map_bounds() {
  return [26.35, 80.06, 30.45, 88.2];
}

/**
 * Check if a point is inside Nepal.
 */
function nepal_map_point_in_nepal($lat, $lng) {
  list($s, $w, $n, $e) = nepal_map_bounds();
  return $lat >= $s && $lat <= $n && $lng >= $w && $lng <= $e;
}

/**
 * Get driving route between two or more points (server-side only).
 * GET /api/route. All points must be in Nepal.
 *
 * @param string $baseUrl NepalMap server base URL
 * @param array $points Array of [lat, lng] or ['lat' => x, 'lng' => y]
 * @return array|null route (coords), totalDistance (m), totalDuration (s), legs, or null
 */
function nepal_map_route($baseUrl, array $points) {
  if (count($points) < 2) return null;
  $flat = array_map(function ($p) {
    $lat = isset($p['lat']) ? $p['lat'] : $p[0];
    $lng = isset($p['lng']) ? $p['lng'] : $p[1];
    return $lat . ',' . $lng;
  }, $points);
  $query = 'points=' . rawurlencode(implode(';', $flat));
  $url = rtrim($baseUrl, '/') . '/api/route?' . $query;
  $ctx = stream_context_create(['http' => ['timeout' => 15]]);
  $raw = @file_get_contents($url, false, $ctx);
  if ($raw === false) return null;
  $data = json_decode($raw, true);
  return is_array($data) ? $data : null;
}

/**
 * Get route from start to end (convenience).
 */
function nepal_map_route_two($baseUrl, $startLat, $startLng, $endLat, $endLng) {
  return nepal_map_route($baseUrl, [[$startLat, $startLng], [$endLat, $endLng]]);
}

/**
 * Format route result for display (total + legs).
 * @return array total_km, total_duration_min, legs (array of km, min)
 */
function nepal_map_route_summary(array $result) {
  $totalKm = isset($result['totalDistance']) ? round($result['totalDistance'] / 1000, 2) : null;
  $totalMin = isset($result['totalDuration']) ? round($result['totalDuration'] / 60, 1) : null;
  $legs = [];
  if (!empty($result['legs'])) {
    foreach ($result['legs'] as $leg) {
      $legs[] = [
        'km'  => round(($leg['distance'] ?? 0) / 1000, 2),
        'min' => round(($leg['duration'] ?? 0) / 60, 1)
      ];
    }
  }
  return ['total_km' => $totalKm, 'total_duration_min' => $totalMin, 'legs' => $legs];
}

Example usage (PHP)

$baseUrl = 'http://localhost:3000';
$result = nepal_map_route_two($baseUrl, 27.7172, 85.324, 28.2096, 83.9856);
if ($result && !empty($result['route'])) {
  $summary = nepal_map_route_summary($result);
  echo "Total: " . $summary['total_km'] . " km, " . $summary['total_duration_min'] . " min\n";
  foreach ($summary['legs'] as $i => $leg) {
    echo "Leg " . ($i + 1) . ": " . $leg['km'] . " km, " . $leg['min'] . " min\n";
  }
}

Server API

Cache layout: cache/tiles/, cache/suggest/, cache/place_details/, cache/places.json, cache/routes.json.

Demo

Open demo.html (e.g. http://localhost:3000/demo.html) to try the SDK with search and caching.