Skip to content

Commit

Permalink
marker updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kochis committed May 8, 2024
1 parent 9fd57f2 commit 0acc5b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
20 changes: 8 additions & 12 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class Http {
path,
data,
host,
versioned = true,
version,
headers = {},
}: {
method: HttpMethod;
path: string;
data?: any;
host?: string;
versioned?: boolean;
version?: string;
headers?: Record<string, string>;
}) {
return new Promise<HttpResponse>((resolve, reject) => {
Expand All @@ -53,12 +53,8 @@ class Http {

// setup request URL
const urlHost = host || options.host;
const version = options.version;
let url = `${urlHost}/${version}/${path}`;

if (!versioned) {
url = `${urlHost}/${path}`;
}
const urlVersion = version || options.version;
let url = `${urlHost}/${urlVersion}/${path}`;

// remove undefined values from request data
let body: any = {};
Expand Down Expand Up @@ -98,11 +94,11 @@ class Http {
}

// combines default headers with custom headers and config headers
const allHeaders = Object.assign(defaultHeaders, headers, configHeaders);
const allHeaders = Object.assign(defaultHeaders, configHeaders, headers);

// set headers
Object.entries(allHeaders).forEach(([key, val]) => {
xhr.setRequestHeader(key, val);
// set headers
Object.keys(allHeaders).forEach((key) => {
xhr.setRequestHeader(key, allHeaders[key]);
});

if (allHeaders['Content-Type'] === 'image/png') {
Expand Down
12 changes: 10 additions & 2 deletions src/ui/RadarMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const createStyleURL = (options: RadarOptions, style: string = DEFAULT_STYLE) =>
`${options.host}/maps/styles/${style}?publishableKey=${options.publishableKey}`
);

/** Check if style is a Radar style or a custom style */
// check if style is a Radar style or a custom style
const isRadarStyle = (style: string) => {
if (RADAR_STYLES.includes(style)) { // Radar built-in style
return true;
Expand All @@ -41,7 +41,7 @@ const isRadarStyle = (style: string) => {
return false;
};

/** Use formatted style URL if using one of Radar's out-of-the-box styles or is a Radar custom style */
// use formatted style URL if using one of Radar's out-of-the-box styles or is a Radar custom style
const getStyle = (options: RadarOptions, mapOptions: RadarMapOptions) => {
const style = mapOptions.style;

Expand Down Expand Up @@ -124,6 +124,14 @@ class RadarMap extends maplibregl.Map {
this.on('load', onResize);
}

addMarker(marker: RadarMarker) {
this._markers.push(marker);
}

removeMarker(marker: RadarMarker) {
this._markers = this._markers.filter((mapMarker: RadarMarker) => mapMarker !== marker);
}

getMarkers(): RadarMarker[] {
return this._markers;
}
Expand Down
12 changes: 6 additions & 6 deletions src/ui/RadarMarker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ class RadarMarker extends maplibregl.Marker {
Logger.error(`Invalid custom marker extension ${ext} - falling back to default marker`);
} else {
this._customMarkerId = markerOptions.customMarkerId;

const originalElement = this._element.cloneNode(true);
this._element.childNodes.forEach((child) => {
child.remove();
});
const contentType = fileExtensionToContentType[ext];

// fetch custom marker
Http.request({
method: 'GET',
versioned: false,
path: `maps/PlACEHOLDER/markers/${markerOptions.customMarkerId}`,
version: 'maps',
path: `markers/${markerOptions.customMarkerId}`,
headers: {
'Content-Type': contentType,
},
Expand Down Expand Up @@ -107,13 +107,13 @@ class RadarMarker extends maplibregl.Marker {
}

addTo(map: RadarMap) {
map._markers.push(this);
map.addMarker(this);
return super.addTo(map);
}

remove() {
if (this._map) {
this._map._markers = this._map._markers.filter((marker) => marker !== this);
this._map.removeMarker(this);
}
return super.remove();
}
Expand All @@ -137,4 +137,4 @@ class RadarMarker extends maplibregl.Marker {
}
}

export default RadarMarker;
export default RadarMarker;

0 comments on commit 0acc5b3

Please sign in to comment.