summaryrefslogtreecommitdiff
path: root/index.html
blob: ce9f4faedf15f26a1f43daafd63ca7d9a8523929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Calimoto GPX (rtept) to Google Maps Directions - Fixed Start/End</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 2em; }
    input[type="file"] { margin-bottom: 1em; }
    #result { margin-top: 1em; }
    a { word-break: break-word; }
  </style>
</head>
<body>

<h1>Calimoto GPX (rtept) to Google Maps Directions (Fixed Start/End Point)</h1>

<input type="file" id="gpxfile" accept=".gpx" />
<div id="result"></div>

<script>
document.getElementById('gpxfile').addEventListener('change', function(event) {
    const file = event.target.files[0];
    if (!file) return;

    // Define your new fixed start/end point
    const startLat = 46.962153; // Your new latitude
    const startLon = 7.446944;  // Your new longitude
    const startPoint = `${startLat},${startLon}`;

    const reader = new FileReader();
    reader.onload = function(e) {
        const parser = new DOMParser();
        const xmlDoc = parser.parseFromString(e.target.result, "application/xml");

        const routePoints = xmlDoc.getElementsByTagName('rtept');
        if (routePoints.length < 3) {
            document.getElementById('result').innerHTML = "Not enough <rtept> points in this file.";
            return;
        }

        const coords = [];
        // Skip first and last points
        for (let i = 1; i < routePoints.length - 1; i++) {
            const lat = routePoints[i].getAttribute('lat');
            const lon = routePoints[i].getAttribute('lon');
            coords.push(lat + "," + lon);
        }

        const MAX_WAYPOINTS = 23; // Google Maps limit: start + waypoints + end

        let selectedCoords = [];
        if (coords.length <= (MAX_WAYPOINTS - 2)) {
            selectedCoords = coords;
        } else {
            // Shrink points smartly
            const intermediatePoints = MAX_WAYPOINTS - 2; // 1 for start, 1 for end
            const interval = coords.length / intermediatePoints;
            for (let i = 1; i < intermediatePoints - 1; i++) {
                const index = Math.round(i * interval);
                if (index >= coords.length) break;
                selectedCoords.push(coords[index]);
            }
        }

        // Build final path: start -> route -> end
        const fullCoords = [startPoint, ...selectedCoords, startPoint];

        const baseUrl = "https://www.google.com/maps/dir/";
        const url = baseUrl + fullCoords.join('/');

        document.getElementById('result').innerHTML = `<p><strong>Directions Link:</strong></p><p><a href="${url}" target="_blank">${url}</a></p>`;
    };

    reader.readAsText(file);
});
</script>

</body>
</html>