-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleMap.ts
153 lines (128 loc) · 4.15 KB
/
GoogleMap.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @project MyGPService v4
* @author Žan Kafol on 7.1.2019
*/
import {MapInterface, MapPoint, MapPolylineOptions, MapPosition, MarkerOptions} from './MapInterface'
import {GoogleMarker} from './GoogleMarker'
import {GoogleMarkerOverlay} from './GoogleMarkerOverlay'
import {GooglePolyline} from './GooglePolyline'
import {GoogleInfoWindow} from './GoogleInfoWindow'
import {GoogleTraffic} from './GoogleTraffic'
export class GoogleMap implements MapInterface<google.maps.Map> {
private CALLBACK_NAME = 'googleCallback'
private callbacks = {drag: null}
public readonly API_KEY: string;
public instance: google.maps.Map;
public constructor (apiKey: string) {
this.API_KEY = apiKey
}
public init (map: any, initialView: MapPosition): Promise<any> {
let onApiLoaded = (): void => {
this.instance = new google.maps.Map(map, {
center: {lat: initialView.lat, lng: initialView.lon},
zoom: initialView.zoom,
gestureHandling: 'greedy',
scaleControl: true,
zoomControl: false,
fullscreenControl: false,
mapTypeControl: false
})
this.instance.addListener('dragend', (e) => {
if (this.callbacks && this.callbacks.drag) {
this.callbacks.drag()
}
})
}
if (this.checkIfGoogleLibraryIsLoaded()) {
return Promise.resolve().then(onApiLoaded)
} else {
const script = document.createElement('script')
script.async = true
script.defer = true
script.src = `https://maps.googleapis.com/maps/api/js?key=${this.API_KEY}&libraries=places&callback=${this.CALLBACK_NAME}`
document.querySelector('head').appendChild(script)
return new Promise((resolve, reject) => {
try {
window[this.CALLBACK_NAME] = resolve
} catch(err) {
reject(err)
}
}).then(onApiLoaded)
}
}
public setCenter(newCenter: MapPosition) {
this.instance.setCenter({lat: newCenter.lat, lng: newCenter.lon})
if(newCenter.zoom) {
this.instance.setZoom(newCenter.zoom)
}
}
public setMapType(layer: string): void {
this.instance.setMapTypeId(layer)
}
public addLayer(layer: any): any {
layer.instance.setMap(this.instance)
}
public removeLayer(layer: any): void {
layer.instance.setMap(null)
}
public addMarker(options: MarkerOptions): GoogleMarker {
return new GoogleMarker(this, options)
}
public addMarkerOverlay(position: MarkerOptions, offset?: MapPoint): any {
return new GoogleMarkerOverlay(this, position, offset)
}
public addInfoWindow(options: MarkerOptions, parent?: any): GoogleInfoWindow {
return new GoogleInfoWindow(this, options, parent)
}
public addPolyline(options: MapPolylineOptions): any {
return new GooglePolyline(this, options)
}
public addTraffic(): any {
return new GoogleTraffic(this)
}
public panTo(position: MapPosition): void {
this.instance.panTo({lat: position.lat, lng: position.lon})
}
public setZoom(zoom: number): void {
this.instance.setZoom(zoom)
}
public getZoom(): number {
return this.instance.getZoom()
}
public fitBounds(bounds: MapPosition[]): void {
if(bounds && bounds.length) {
let gBounds = new google.maps.LatLngBounds()
bounds.map(pos => gBounds.extend({lat: pos.lat, lng: pos.lon}))
this.instance.fitBounds(gBounds)
}
}
public boundsContain(pos: MapPosition): boolean {
let bounds = this.instance.getBounds()
if(bounds) {
return bounds.contains({lat: pos.lat, lng: pos.lon})
}
return false
}
public geocode(pos: MapPosition): Promise<String> {
let geocoder = new google.maps.Geocoder
return new Promise((resolve, reject) => {
geocoder.geocode({'location': {lat: pos.lat, lng: pos.lon}}, results => {
if (results[0]) {
resolve(results[0].formatted_address)
} else {
reject()
}
})
})
}
public onDrag(callback: Function): void {
this.callbacks.drag = callback
}
private checkIfGoogleLibraryIsLoaded (): boolean {
return !!this.getGoogleObject()
}
private getGoogleObject (): any {
// @ts-ignore
return window.google
}
}