Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map initalization language param #161

14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ });

Add the following script in your `html` file
```html
<script src="https://js.radar.com/v4.3.0-beta.0/radar.min.js"></script>
<script src="https://js.radar.com/v4.3.0-beta.5/radar.min.js"></script>
```

Then initialize the Radar SDK
Expand All @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then
```html
<html>
<head>
<link href="https://js.radar.com/v4.3.0-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.3.0-beta.0/radar.min.js"></script>
<link href="https://js.radar.com/v4.3.0-beta.5/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.3.0-beta.5/radar.min.js"></script>
</head>

<body>
Expand All @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis
```html
<html>
<head>
<link href="https://js.radar.com/v4.3.0-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.3.0-beta.0/radar.min.js"></script>
<link href="https://js.radar.com/v4.3.0-beta.5/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.3.0-beta.5/radar.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper
```html
<html>
<head>
<link href="https://js.radar.com/v4.3.0-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.3.0-beta.0/radar.min.js"></script>
<link href="https://js.radar.com/v4.3.0-beta.5/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.3.0-beta.5/radar.min.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radar-sdk-js",
"version": "4.3.0-beta.0",
"version": "4.3.0-beta.5",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export interface RadarSearchGeofencesResponse extends RadarResponse {
}

export interface RadarMapOptions extends Omit<maplibregl.MapOptions, 'transformRequest'> {
container: string | HTMLElement;
language?: string;
}

export interface RadarMarkerImage {
Expand Down
21 changes: 13 additions & 8 deletions src/ui/RadarMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ const defaultFitMarkersOptions: maplibregl.FitBoundsOptions = {
padding: 50,
};

const createStyleURL = (options: RadarOptions, style: string = DEFAULT_STYLE) => (
`${options.host}/maps/styles/${style}?publishableKey=${options.publishableKey}`
);
const createStyleURL = (options: RadarOptions, mapOptions: RadarMapOptions) => {
let url = `${options.host}/maps/styles/${mapOptions.style}?publishableKey=${options.publishableKey}`
if (mapOptions.language) {
url += `&language=${mapOptions.language}`
}
return url
};

// check if style is a Radar style or a custom style
const isRadarStyle = (style: string) => {
Expand All @@ -50,14 +54,13 @@ const getStyle = (options: RadarOptions, mapOptions: RadarMapOptions) => {
const style = mapOptions.style;

if (!style || (typeof style === 'string' && isRadarStyle(style))) {
return createStyleURL(options, style);
return createStyleURL(options, mapOptions);
}

return mapOptions.style; // style object or URL
};

class RadarMap extends maplibregl.Map {
_customMarkerRawSvg: string | undefined;
_markers: RadarMarker[] = [];

constructor(mapOptions: RadarMapOptions) {
Expand All @@ -69,22 +72,24 @@ class RadarMap extends maplibregl.Map {

// configure maplibre options
const style = getStyle(config, mapOptions);
const maplibreOptions: maplibregl.MapOptions = Object.assign({},
const maplibreOptions: RadarMapOptions = Object.assign({},
defaultMaplibreOptions,
mapOptions,
{ style },
);
Logger.debug(`initialize map with options: ${JSON.stringify(maplibreOptions)}`);

maplibreOptions.transformRequest = (url, resourceType) => {
(maplibreOptions as maplibregl.MapOptions).transformRequest = (url, resourceType) => {
// this handles when a style is switched
if (resourceType === 'Style' && isRadarStyle(url)) {
url = createStyleURL(config, url);
url = createStyleURL(config, { ...maplibreOptions, style: url });
}

let headers = {
'Authorization': config.publishableKey,
'X-Radar-Device-Type': 'Web',
'X-Radar-SDK-Version': SDK_VERSION,
'X-Radar-Device-Language': navigator.language,
jaspk06 marked this conversation as resolved.
Show resolved Hide resolved
};
if (typeof config.getRequestHeaders === 'function') {
headers = Object.assign(headers, config.getRequestHeaders());
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.3.0-beta.0';
export default '4.3.0-beta.5';
Loading