map style select

Picks the basemap of every map on the page, live. Presets are grouped by provider.

PreviousNext
Loading map
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardMapImports } from '@/shared/components/map/map.imports';
import { ZardMapStyleSelectComponent } from '@/shared/components/map-style-select/map-style-select.component';

@Component({
  selector: 'z-demo-map-style-picker',
  imports: [ZardMapImports, ZardMapStyleSelectComponent],
  template: `
    <div class="relative h-[420px] w-full overflow-hidden rounded-xl border">
      <!-- No [styles] here: this map follows the app-wide basemap that provideZardMap() registers. -->
      <ng-map [center]="[-0.1276, 51.5072]" [zoom]="11.5">
        <ng-map-controls />
        <ng-map-marker [longitude]="-0.1276" [latitude]="51.5072" ariaLabel="London">
          <ng-marker-content />
        </ng-map-marker>
      </ng-map>

      <!-- The picker is plain HTML, so it can sit over a corner of the map, in a toolbar or in your app header. -->
      <z-map-style-select class="absolute top-3 left-3" />
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoMapStylePickerComponent {}

Installation

Copy
npx zard-cli@latest add map-style-select

Usage

import { ZardMapStyleSelectComponent } from '@/shared/components/map-style-select/map-style-select.component';
Copy
<z-map-style-select />
Copy

Examples

style override

A map with its own [styles] ignores the picker.
Loading map
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardBadgeComponent } from '@/shared/components/badge/badge.component';
import { ZardMapImports } from '@/shared/components/map/map.imports';
import { ZardMapStyleSelectComponent } from '@/shared/components/map-style-select/map-style-select.component';

/** Defined once, outside the component: `<ng-map>` compares styles by identity before it restyles. */
const FIORD = {
  light: 'https://tiles.openfreemap.org/styles/fiord',
  dark: 'https://tiles.openfreemap.org/styles/fiord',
};

@Component({
  selector: 'z-demo-map-style-override',
  imports: [ZardMapImports, ZardMapStyleSelectComponent, ZardBadgeComponent],
  template: `
    <div class="flex w-full flex-col gap-3">
      <z-map-style-select class="max-w-60" />

      <div class="grid gap-4 md:grid-cols-2">
        <div class="relative h-[320px] overflow-hidden rounded-xl border">
          <ng-map [center]="[4.9041, 52.3676]" [zoom]="11" />
          <z-badge zType="secondary" class="absolute top-3 left-3">Follows the picker</z-badge>
        </div>

        <div class="relative h-[320px] overflow-hidden rounded-xl border">
          <!-- A map with its own [styles] ignores the picker. -->
          <ng-map [styles]="fiord" [center]="[4.9041, 52.3676]" [zoom]="11" />
          <z-badge zType="secondary" class="absolute top-3 left-3">Own [styles]: Fiord</z-badge>
        </div>
      </div>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoMapStyleOverrideComponent {
  protected readonly fiord = FIORD;
}

Light and dark

Not every style works in dark mode
Carto Voyager, OpenFreeMap Liberty, OpenFreeMap Bright, OpenFreeMap Positron, OpenStreetMap Standard, Stadia Maps Outdoors, Stadia Maps OSM Bright are drawn for light mode only: in dark mode they stay light or fall back to a shared dark style, so they all look alike. OpenFreeMap Dark, OpenFreeMap Fiord are dark only. The picker marks them Light only and Dark only. If your app has a dark mode, pick a preset with its own dark variant.
Presets pair a light and a dark style and follow the dark class on <html>. See the presets table in Map styles.

API Reference

z-map-style-select

Picks the basemap of every map on the page; maps with their own [styles] ignore it.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
''
[zAriaLabel]

Accessible name, caption and popover title

Type
string
Default
'Map style'
[zAlign]

Button edge the popover aligns to

Type
'start' | 'center' | 'end'
Default
'end'
(zStyleChange)

Emits the picked preset once applied

Type
ZardMapPreset
Default
—
github iconwhatsapp icondiscord iconX icon

Made with ❤ in Brazil. Open source and available on GitHub .