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.
You can install the development version of azuremapsr
from GitHub with:
# install.packages("pak")
::pak("juanfonsecaLS1/azuremapsr") pak
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'
.
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
<- c(-122.3321, 47.6062) # Seattle
origin <- c(-122.0369, 47.6609) # Redmond
destination <- c(-122.20687, 47.612002) # Bellevue
waypoints
# Define route parameters
<- list(
params optimizeRoute = "fastestWithTraffic",
routeOutputOptions = "routePath",
travelMode = "driving"
)
# Get the route (requires a valid API key to be set!!!)
<- req_route_directions(origin, destination, waypoints, params) sample_response
Routes can be extracted from the response with the
get_routes
function
<- get_routes(sample_response)
sf_routes
plot(sf_routes$geometry,col = c("blue","red"))
These are some of the attributes returned by the service:
|> names() sf_routes
[1] "trafficCongestion" "distanceInMeters"
[3] "durationInSeconds" "arrivalAt"
[5] "departureAt" "durationTrafficInSeconds"
[7] "type" "legs"
[9] "geometry"