-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathexample.ts
More file actions
107 lines (90 loc) · 2.61 KB
/
example.ts
File metadata and controls
107 lines (90 loc) · 2.61 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Example usage of osrm-text-instructions with TypeScript
import osrmTextInstructions = require('./index');
// Initialize the compiler for OSRM v5
const compiler = osrmTextInstructions('v5');
// Example route step data
const step: osrmTextInstructions.RouteStep = {
maneuver: {
type: 'turn',
modifier: 'left',
bearing_after: 90
},
name: 'Main Street',
ref: 'A1',
mode: 'driving',
intersections: [
{
lanes: [
{ valid: false },
{ valid: true }
]
}
]
};
// Compile options
const options: osrmTextInstructions.CompileOptions = {
legCount: 2,
legIndex: 0,
classes: ['primary'],
formatToken: (token: string, value: string) => {
if (token === 'way_name') {
return `<strong>${value}</strong>`;
}
return value;
}
};
try {
// Generate instruction
const instruction = compiler.compile('en', step, options);
console.log('Instruction:', instruction);
// Get way name
const wayName = compiler.getWayName('en', step, options);
console.log('Way name:', wayName);
// Get direction from degree
const direction = compiler.directionFromDegree('en', 90);
console.log('Direction:', direction);
// Ordinalize number
const ordinal = compiler.ordinalize('en', 1);
console.log('Ordinal:', ordinal);
// Generate lane configuration
const laneConfig = compiler.laneConfig(step);
console.log('Lane config:', laneConfig);
// Capitalize first letter
const capitalized = compiler.capitalizeFirstLetter('en', 'hello world');
console.log('Capitalized:', capitalized);
// Access abbreviations
const abbreviations = compiler.abbreviations;
console.log('Available abbreviations:', Object.keys(abbreviations));
} catch (error) {
console.error('Error:', error);
}
// Example with different maneuver types
const departStep: osrmTextInstructions.RouteStep = {
maneuver: {
type: 'depart',
bearing_after: 0
},
name: 'Start Street'
};
const arriveStep: osrmTextInstructions.RouteStep = {
maneuver: {
type: 'arrive',
modifier: 'right'
},
name: 'Destination Avenue'
};
const roundaboutStep: osrmTextInstructions.RouteStep = {
maneuver: {
type: 'roundabout',
modifier: 'right',
exit: 2
},
rotary_name: 'Main Roundabout'
};
// Compile different instruction types
const departInstruction = compiler.compile('en', departStep);
const arriveInstruction = compiler.compile('en', arriveStep, { waypointName: 'Home' });
const roundaboutInstruction = compiler.compile('en', roundaboutStep);
console.log('Depart:', departInstruction);
console.log('Arrive:', arriveInstruction);
console.log('Roundabout:', roundaboutInstruction);