-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleMarkerOverlay.ts
65 lines (52 loc) · 1.76 KB
/
GoogleMarkerOverlay.ts
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
/**
* @project MyGPService v4
* @author Žan Kafol on 7.1.2019
*/
import {MapMarkerOverlay, MapPoint, MapPosition, MarkerOptions} from './MapInterface'
import {GoogleMap} from './GoogleMap'
import {GoogleObject} from './GoogleObject'
export class GoogleMarkerOverlay extends GoogleObject<google.maps.OverlayView> implements MapMarkerOverlay {
public map: GoogleMap;
public instance: google.maps.OverlayView;
public div: HTMLElement;
public pos: MapPosition;
public offset: MapPoint;
public constructor(map: GoogleMap, position: MarkerOptions, offset: MapPoint) {
super(map)
this.pos = position
this.offset = offset
this.div = document.createElement('div')
this.instance = new google.maps.OverlayView()
this.instance.onAdd = this.onAdd.bind(this)
this.instance.onRemove = this.onRemove.bind(this)
this.instance.draw = this.onDraw.bind(this)
this.instance.setMap(map.instance)
this.addInternalListeners(this.instance)
}
public onAdd() {
let panes = this.instance.getPanes()
panes.overlayShadow.appendChild(this.div)
}
public onRemove() {
this.div.parentNode.removeChild(this.div)
}
public onDraw() {
if(this.instance) {
let overlayProjection = this.instance.getProjection()
if (overlayProjection) {
let position = overlayProjection.fromLatLngToDivPixel(new google.maps.LatLng(this.pos.lat, this.pos.lon))
if (position != null) {
this.div.style.left = (position.x - this.div.clientWidth / 2 + this.offset.x) + 'px'
this.div.style.top = this.offset.y + position.y + 'px'
}
}
}
}
public getPosition(): MapPosition {
return this.pos
}
public setPosition(position: MapPosition): void {
this.pos = position
this.onDraw()
}
}