Learn R Programming

azuremapsr

The goal of azuremapsr is to provide an R interface to the Microsoft Azure Maps REST APIs. This package simplifies the process of authenticating, building requests, and parsing responses for various geospatial services, starting with route directions.

Installation

You can install the latest stable version of azuremapsr from CRAN with:

install.packages("azuremapsr")   

You can also install the development version of azuremapsr from GitHub with:

# install.packages("pak")
pak::pak("juanfonsecaLS1/azuremapsr")

Authentication

Before using the package, you need a subscription key from the Azure Maps service. You can find instructions on how to obtain one here.

Once you have your key, you can set it for your R session using the set_azuremaps_token() function:

library(azuremapsr)
set_azuremaps_token("YOUR_API_KEY_HERE")

For a more permanent solution, you can store the key as an environment variable named azure_maps in your .Renviron file. You can open this file for editing by running usethis::edit_r_environ() and adding the line azure_maps='YOUR_API_KEY_HERE'.

Example

This example shows how to get the fastest driving route between two points including a waypoint.

library(azuremapsr)
library(sf)

# This example will not run unless an API key is set.
# Replace "YOUR_API_KEY_HERE" with your actual key.
# set_azuremaps_token("YOUR_API_KEY_HERE")

# Define origin, destination, and waypoint
origin <- c(-122.3321, 47.6062)  # Seattle
destination <- c(-122.0369, 47.6609) # Redmond
waypoints <- c(-122.20687, 47.612002) # Bellevue

# Define route parameters
params <- list(
  optimizeRoute = "fastestWithTraffic",
  routeOutputOptions = "routePath",
  travelMode = "driving"
)

# Get the route (requires a valid API key to be set!!!)

sample_response <-   req_route_directions(origin, destination, waypoints, params)

Routes can be extracted from the response with the get_routes function

sf_routes <- get_routes(sample_response)

plot(sf_routes$geometry,col = c("blue","red"))

These are some of the attributes returned by the service:

sf_routes |> names()
[1] "trafficCongestion"        "distanceInMeters"        
[3] "durationInSeconds"        "arrivalAt"               
[5] "departureAt"              "durationTrafficInSeconds"
[7] "type"                     "legs"                    
[9] "geometry"                

Copy Link

Version

Install

install.packages('azuremapsr')

Version

0.0.2

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Juan P. Fonseca-Zamora

Last Published

August 27th, 2025

Functions in azuremapsr (0.0.2)

check_params

check conformity of parameters for JSON section
POSTbody_builder_directions_json

Build JSON Parameter Body for Route Directions
get_point

Create sfc Object from Coordinates or sf Object
sample_response_leeds

A sample response obtained from the get_route_directions function
POSTbody_builder_directions_geojson

Build GeoJSON Body for Route Directions
get_azuremaps_token

Get Azure Maps API Authentication Token
get_routes

Extract and Combine Routes from an 'Azure Maps' Response
req_route_directions

Get Route Directions from 'Azure Maps'
json_to_sf

Convert 'Azure Maps' JSON Response to an sf Object
sample_response

A sample response obtained from the get_route_directions function
set_azuremaps_token

Set Azure Maps API Authentication Token