File size: 2,304 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const createHTMLMarker = (google) => {
  class HTMLMarker extends google.maps.OverlayView {
    constructor({
      position,
      map,
      className,
      anchor = {
        x: 0,
        y: 0,
      },
    }) {
      super();

      this.anchor = anchor;
      this.subscriptions = [];
      this.latLng = new google.maps.LatLng(position);

      this.element = document.createElement('div');
      this.element.className = className;
      this.element.style.position = 'absolute';
      // Force the "white-space" of the element will avoid the
      // content to collapse when we move the map from center
      this.element.style.whiteSpace = 'nowrap';

      this.setMap(map);
    }

    onAdd() {
      if (this.getPanes()) {
        this.getPanes().overlayMouseTarget.appendChild(this.element);
      }
    }

    draw() {
      if (this.getProjection()) {
        const position = this.getProjection().fromLatLngToDivPixel(this.latLng);

        const offsetX = this.anchor.x + this.element.offsetWidth / 2;
        const offsetY = this.anchor.y + this.element.offsetHeight;

        this.element.style.left = `${Math.round(position.x - offsetX)}px`;
        this.element.style.top = `${Math.round(position.y - offsetY)}px`;

        // Markers to the south are in front of markers to the north
        // This is the default behaviour of Google Maps
        this.element.style.zIndex = parseInt(this.element.style.top, 10);
      }
    }

    onRemove() {
      if (this.element && this.element.parentNode) {
        this.element.parentNode.removeChild(this.element);

        this.subscriptions.forEach((subscription) => subscription.remove());

        delete this.element;

        this.subscriptions = [];
      }
    }

    addListener(eventName, listener) {
      const subscription = {
        remove: () => {
          this.element.removeEventListener(eventName, listener);

          this.subscriptions = this.subscriptions.filter(
            (_) => _ !== subscription
          );
        },
      };

      this.element.addEventListener(eventName, listener);

      this.subscriptions = this.subscriptions.concat(subscription);

      return subscription;
    }

    getPosition() {
      return this.latLng;
    }
  }

  return HTMLMarker;
};

export default createHTMLMarker;