Azure Maps: Calculate Route Directions
See more Azure Maps Examples
Posts origin and destination waypoints to Route 2025-01-01. Requests both route geometry and itinerary, and reads each response feature according to its properties.type. Prints distance, duration without traffic, and plain-text maneuver instructions; saves the complete response as GeoJSON.
The example selects driving and fastest-without-traffic optimization. Use a suitable travel mode and route options for your application. GeoJSON points use longitude first. The output file in the current directory replaces any same-named file.
Uses direct Chilkat.Http calls to the Azure public-cloud endpoint. Supply a nonempty subscription_key string in private qa_data/azure/maps.json. Shared-key authentication must be enabled on the Maps account. Keep this file outside source control and public web directories, and do not embed account keys in publicly distributed applications. Microsoft Entra ID is an alternative.
Query parameters are supplied as raw text for Chilkat to encode. The code checks transport status, HTTP status, and JSON parsing separately. For throttling or temporary service failures, use bounded retries and honor Retry-After. HTTP diagnostics can contain credentials and location data; inspect them privately. Calls use verified HTTPS and do not follow redirects.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global.UnlockBundle for sample code.
local config = chilkat.JsonObject.new()
config.strict = true
-- Private file: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
local success = config:load_file("qa_data/azure/maps.json")
if not success then
print("Could not load private Azure Maps configuration.")
return
end
if config:json_type_of("subscription_key") ~= 1 then
print("Missing or invalid subscription_key.")
return
end
if config:string_of_equals("subscription_key", "", true) then
print("A nonempty subscription_key is required.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 30
http.read_timeout = 90
http.accept = "application/json"
local response = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
local query = chilkat.JsonObject.new()
-- Current Azure Maps operations accept the key as a header.
http:set_request_header("subscription-key", config:string_of("subscription_key"))
-- Route 2025 takes GeoJSON in a POST body, with longitude first.
local body = chilkat.JsonObject.new()
body:update_string("type", "FeatureCollection")
body:update_string("features[0].type", "Feature")
body:update_string("features[0].geometry.type", "Point")
body:update_number("features[0].geometry.coordinates[0]", "-122.201399")
body:update_number("features[0].geometry.coordinates[1]", "47.608678")
body:update_string("features[0].properties.pointType", "waypoint")
body:update_int("features[0].properties.pointIndex", 0)
body:update_string("features[1].type", "Feature")
body:update_string("features[1].geometry.type", "Point")
body:update_number("features[1].geometry.coordinates[0]", "-122.201669")
body:update_number("features[1].geometry.coordinates[1]", "47.615076")
body:update_string("features[1].properties.pointType", "waypoint")
body:update_int("features[1].properties.pointIndex", 1)
body:update_string("travelMode", "driving")
body:update_int("maxRouteCount", 1)
body:update_string("optimizeRoute", "fastestWithoutTraffic")
body:update_string("routeOutputOptions[0]", "routePath")
body:update_string("routeOutputOptions[1]", "itinerary")
success = http:http_json("POST", "https://atlas.microsoft.com/route/directions?api-version=2025-01-01", body, "application/geo+json", response)
-- Http methods return false for HTTP errors as well as transport failures.
if response.status_code == 0 then
print("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
return
end
if response.status_code ~= 200 then
print("HTTP status: " .. response.status_code)
print("Error code: " .. response:get_header_field("x-ms-error-code"))
print("Retry-After: " .. response:get_header_field("Retry-After"))
-- Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
-- Keep response bodies and detailed diagnostics out of public logs.
return
end
if not success then
print("The HTTP response was incomplete.")
return
end
success = result:load(response.body_str)
if not success then
print("Expected a well-formed JSON response.")
return
end
local features = chilkat.JsonArray.new()
local feature = chilkat.JsonObject.new()
if not result:string_of_equals("type", "FeatureCollection", true) then
print("Expected a GeoJSON FeatureCollection.")
return
end
success = result:array_of2("features", features)
if not success then
print("Expected a features array.")
return
end
local i = 0
local foundRoute = false
while i < features.size do
success = features:object_at2(i, feature)
if not success then
print("Invalid route feature.")
return
end
if feature:string_of_equals("properties.type", "RoutePath", true) then
if feature:json_type_of("properties.distanceInMeters") ~= 2 then
print("Missing or invalid properties.distanceInMeters.")
return
end
if feature:json_type_of("properties.durationInSeconds") ~= 2 then
print("Missing or invalid properties.durationInSeconds.")
return
end
foundRoute = true
print("Distance (meters): " .. feature:string_of("properties.distanceInMeters"))
print("Duration without traffic (seconds): " .. feature:string_of("properties.durationInSeconds"))
end
if feature:string_of_equals("properties.type", "ManeuverPoint", true) then
-- Use plain text rather than rendering formattedText as untrusted HTML.
print("Instruction: " .. feature:string_of("properties.instruction.text"))
end
i = i + 1
end
if not foundRoute then
print("No RoutePath feature was returned.")
return
end
success = result:write_file("azure-maps-route.geojson")
if not success then
print("Could not save the route.")
return
end
print("Saved azure-maps-route.geojson")