Hallo Thomas Jäger
Leider enthält bei mir das objekt routeData.routes[0].legs[0] keine Instructions sondern nur die Summary. Wie könnte ich mit diesem Aufruf die Instructions erhalten, bzw. Müsste ich einen extra Abruf der Instructions initiieren?
Der Abfrageparameter „instruktionsTyp
“ steuert das Format der Anweisungen.
- „instruktionsTyp=codiert“ gibt Anweisungen in codiertem Format zurück (keine menschenlesbaren Nachrichten).
- „instruktionsTyp=text“ gibt Anweisungen im Volltext zurück. Textanweisungen werden innerhalb von routes[0].guidance.instructions angezeigt, nicht innerhalb von legs[0].instructions, wenn instructionsType=text ist.
Sie können den folgenden Code verwenden, der Anweisungen mithilfe von JavaScript erhält.
Code:
const fetch = require('node-fetch'); // Only needed for Node.js before v18
async function getRouteDirections(subscriptionKey, start, end, routeType = 'fastest') {
const startLat = start.latitude;
const startLon = start.longitude;
const endLat = end.latitude;
const endLon = end.longitude;
const routeUrl = `https://atlas.microsoft.com/route/directions/json` +
`?api-version=1.0` +
`&subscription-key=${encodeURIComponent(subscriptionKey)}` +
`&query=${startLat},${startLon}:${endLat},${endLon}` +
`&routeType=${routeType}` +
`&instructionsType=text` +
`&language=de-DE` +
`&travelMode=car` +
`&instructions=true`;
try {
const response = await fetch(routeUrl);
const data = await response.json();
// DEBUG: Print the full response structure
console.log("Full API Response:\n", JSON.stringify(data, null, 2));
if (data.routes?.length > 0) {
const instructions = data.routes[0].guidance?.instructions;
if (instructions?.length > 0) {
return instructions.map(i => i.message); // Return only the German message
} else {
console.warn("No guidance instructions found in the response.");
return [];
}
} else {
console.error("No routes returned.");
return [];
}
} catch (error) {
console.error("Failed to fetch route directions:", error);
return [];
}
}
// Example usage:
const subscriptionKey = 'xxxxxxx';
const start = { latitude: 50.1109, longitude: 8.6821 }; // Frankfurt
const end = { latitude: 52.52, longitude: 13.405 }; // Berlin
getRouteDirections(subscriptionKey, start, end).then(instructions => {
console.log("\nRoute Instructions (German):");
if (instructions.length === 0) {
console.log("No instructions found.");
} else {
instructions.forEach((step, i) => console.log(`${i + 1}. ${step}`));
}
});
Ausgabe:
Referenz:
Route - Get Route Directions - REST API (Azure Maps) | Microsoft Learn
Lassen Sie es mich wissen, wenn Sie Fragen haben, und ich helfe Ihnen gerne weiter.
Wenn die bereitgestellten Informationen für Sie hilfreich waren, denken Sie bitte daran, die Antwort zu akzeptieren und hochzuvoten, da dies auch für andere Community-Mitglieder hilfreich sein wird.
Ich übersetze die Antworten aus dem Englischen, also verzeihen Sie mir bitte, wenn es Grammatikprobleme gibt.