Stadia Maps JavaScript SDK
This SDK helps you access the full range of geospatial APIs from Stadia Maps using JavaScript, TypeScript, etc.
We've written everything in TypeScript, derived from our official API spec, so you'll get all the goodies
like autocomplete, type definitions, and documentation in your favorite editor.
Getting started with npm
First, add @stadiamaps/api
as a dependency of your project using your favorite package manager
like npm
or yarn
. Something like this:
npm install --save @stadiamaps/api
The library exposes 3 different API classes for grouping functionality: GeocodingApi
,
GeospatialApi
, and RoutingApi
. These correspond to the sections in our online
API Reference.
All clients have the same interface and only expose different methods. Here is an
example of getting started with the geocoding API:
import { GeocodingApi, Configuration } from "@stadiamaps/api";
const api = new GeocodingApi();
api.reverse({ pointLat: 59.44436, pointLon: 24.75071 }).then(
function (result) {
console.log(result);
},
function (err) {
console.log(err);
},
);
const res = await api.search({ text: "Põhja pst 27" });
Getting started with unpkg
If you like to keep your frontend simple and don't want to use JS build tooling, we have you covered! You can easily use
the SDK by linking to the module on unpkg. The library is exported via the global stadiaMapsApi
, but otherwise
functions exactly like if you used npm
package tooling.
Here's a quick usage example of a webpage that makes a geocoding query and displays the result.
<!doctype html>
<html>
<head>
<script
type="text/javascript"
src="https://www.unpkg.com/@stadiamaps/api@3"
></script>
<script type="text/javascript">
const api = new stadiaMapsApi.GeocodingApi();
window.onload = async function () {
const res = await api.autocomplete({ text: "Põhja pst 27" });
document.getElementById("pre").innerHTML = JSON.stringify(res, null, 2);
};
</script>
</head>
<body>
<div>
<pre id="pre">Loading...</pre>
</div>
</body>
</html>
Documentation
Official documentation lives at docs.stadiamaps.com,
where we have both long-form prose explanations of each endpoint and an interactive API reference.
Developing
Refer to DEVELOPING.md for details on
local development.
Version 4.0.0 - 2024-09-04
Added
- Support for the OSRM format and navigation aids
- BREAKING: To support the new format, the response type of directions APIs has changed
TypeScript users will need to use a type guard
in order to check the type of the response for directions APIs,
as the response type is now a union.
The response format will depend on the value of the format parameter in your request.
Here is an example of how to do the check in your code.
The guard helper function is included in this SDK and exported publicly.
const res = await api.route({ routeRequest: routeRequest });
if (instanceOfRouteResponse(res)) {
// TODO: Process the response
} else {
fail("Expected a Valhalla JSON format route response");
}
Alternately, you can guard for OSRM format responses like this:
const res = await api.route({ routeRequest: osrmFormatRouteRequest });
if (instanceOfOsrmRouteResponse(res)) {
// TODO: Process the response
} else {
fail("Expected an OSRM format route response");
}