@azure-rest/maps-render
Version:
A generated SDK for MapsRenderClient.
96 lines • 3.25 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPinsQuery = createPinsQuery;
const createMultiCollection_js_1 = require("./createMultiCollection.js");
const optionKeyMap = {
opacity: "al",
labelAnchor: "la",
labelColor: "lc",
labelSizeInPixels: "ls",
pinAnchor: "an",
rotationInDegree: "ro",
scale: "sc",
pinColor: "co",
};
function isOptionKeyMap(key) {
return key in optionKeyMap;
}
/**
* Create a pin query string for _get map static image_
*
* @example
* ```ts snippet:ReadmeSampleCreatePinsQuery
* import { DefaultAzureCredential } from "@azure/identity";
* import MapsRender, { PinSet, createPinsQuery } from "@azure-rest/maps-render";
* import { createWriteStream } from "node:fs";
*
* const credential = new DefaultAzureCredential();
* const client = MapsRender(credential, "<maps-account-client-id>");
*
* const pins: PinSet[] = [
* {
* pins: [
* { coordinate: [52.577, 13.35], label: "Label start" },
* { coordinate: [52.6, 13.2988], label: "Label end" },
* ],
* pinImage: "default",
* options: {
* scale: 0.9,
* pinColor: "FF0000",
* labelColor: "0000FF",
* labelSizeInPixels: 18,
* },
* },
* ];
*
* const path = createPinsQuery(pins);
*
* const response = await client
* .path("/map/static")
* .get({
* queryParameters: {
* bbox: [13.228, 52.4559, 13.5794, 52.62],
* zoom: 10,
* path,
* },
* skipUrlEncoding: true,
* })
* .asNodeStream();
*
* // Handle the error.
* if (!response.body) {
* throw Error("No response body");
* }
*
* response.body.pipe(createWriteStream("pin.png"));
* ```
*
* @param pins - An array of {@link Pin} that specify the positions and label text of each pin.
* @param pinImage - Specify the image source for custom pin. Set this to "none" if you don't want to show a pin image.
* @param options - The style options of the pins. See {@link PinOptions}
* @returns - The composed query string.
*/
function createPinsQuery(pinSets) {
const pinsQueries = pinSets.map(({ pins, pinImage = "default", options = {} }) => {
// compose the pins' position query string
const pinsQueryStr = pins
.map(({ coordinate: [lat, lon], label }) => `${label ? `'${label}'` : ""}${lon} ${lat}`)
.join("|");
// compose the options query string
const optionsQueryStr = Object.entries(options).reduce((queryStr, [key, val]) => {
if (!isOptionKeyMap(key))
throw Error(`Unexpected option: ${key}`);
if (Array.isArray(val))
return (queryStr += `|${optionKeyMap[key]}${val[0]} ${val[1]}`);
return (queryStr += `|${optionKeyMap[key]}${val}`);
}, "");
if (pinImage === "none" || pinImage === "default") {
return `${pinImage}${optionsQueryStr}||${pinsQueryStr}`;
}
return `custom${optionsQueryStr}||${pinsQueryStr}||${pinImage}`;
});
return (0, createMultiCollection_js_1.createMultiCollection)("pins", pinsQueries);
}
//# sourceMappingURL=createPinsQuery.js.map