Styling and labels · Angular, React and OpenLayers

An SVG marker on a coordinate

Anchor a crisp scaled SVG map pin by its bottom edge on a WGS84 coordinate in OpenLayers using aol-style-icon and a fractional icon anchor.

Wawel castle in Kraków with a blue map pin on it.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Zoomed onto Wawel Castle in central Kraków over OpenStreetMap, a crisp vector SVG map pin (assets/marker.svg) scaled to 2× size stands anchored by its bottom edge on the exact geographic coordinate even as you zoom in and out.

When you need it

Store locators, parcel pins, incident markers and search results rely on teardrop or pushpin icons whose tip—not the middle of the image—must stay locked to the target coordinate across all zoom scales.

Rendering markers via aol-style-icon inside the WebGL/2D map canvas scales effortlessly to thousands of pins without flooding the DOM with HTML elements.

The code

The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.

marker.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  FeatureComponent,
  GeometryPointComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  StyleComponent,
  StyleIconComponent,
  ViewComponent,
} from 'cc-openlayers';

/** A point drawn as an SVG marker whose tip sits on the coordinate. */
@Component({
  selector: 'app-marker',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    FeatureComponent,
    GeometryPointComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    StyleComponent,
    StyleIconComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="15"><aol-coordinate [x]="19.9356" [y]="50.054" srid="EPSG:4326" /></aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      <aol-layer-vector>
        <aol-source-vector>
          <aol-feature>
            <aol-geometry-point><aol-coordinate [x]="19.9356" [y]="50.054" srid="EPSG:4326" /></aol-geometry-point>
            <aol-style><aol-style-icon src="assets/marker.svg" [anchor]="[0.5, 1]" [scale]="2" /></aol-style>
          </aol-feature>
        </aol-source-vector>
      </aol-layer-vector>
    </aol-map>
  `,
})
export class Marker {}

Architecture and OpenLayers mechanics

A point feature declared in WGS84 degrees (srid="EPSG:4326") carries an aol-style-icon loading assets/marker.svg at [scale]="2". Setting [anchor]="[0.5, 1]" in OpenLayers’ default fractional icon units (0..1 left-to-right, 0..1 top-to-bottom) shifts the registration point from the default [0.5, 0.5] image centre down to the horizontal midpoint of the bottom edge.

How it works

  • aol-style-icon loads vector assets/marker.svg and enlarges it cleanly via [scale]="2".
  • [anchor]="[0.5, 1]" pins the bottom edge of the icon (in fractional icon coordinates) to the map coordinate, so the drawn tip sits two source units above it.
  • Declaring srid="EPSG:4326" lets the template specify Wawel’s coordinate in standard longitude and latitude degrees.

Pitfalls

  • Leaving anchor at OpenLayers’ default [0.5, 0.5] centres the pin graphic over the coordinate, making the bottom tip point significantly south of the real location at street zooms.
  • Always include explicit numeric width and height attributes on the root <svg> element of external SVG icons—dimensionless SVGs can render at 0 × 0 pixels in cross-browser canvas image loaders.

The same feature in the OpenLayers examples .

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.