-
Notifications
You must be signed in to change notification settings - Fork 3
/
map-google-marker.js
121 lines (99 loc) · 2.26 KB
/
map-google-marker.js
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
import { define, Component } from '@xinix/xin';
export class MapGoogleMarker extends Component {
get props () {
return Object.assign({}, super.props, {
latitude: {
type: Number,
value: -6.254844,
observer: '_positionChanged(latitude, longitude)',
},
longitude: {
type: Number,
value: 106.826583,
observer: '_positionChanged(latitude, longitude)',
},
map: {
type: Object,
observer: '_mapChanged',
},
marker: {
type: Object,
},
info: {
type: Object,
},
title: {
type: String,
},
label: {
type: String,
},
icon: {
type: String,
},
mouseEvents: {
type: Boolean,
observer: '_mouseEventsChanged',
},
});
}
created () {
super.created();
this._listeners = {};
}
_mapChanged (map) {
this.set('marker', new window.google.maps.Marker({
position: { lat: Number(this.latitude), lng: Number(this.longitude) },
map: map,
title: this.title,
label: this.label,
icon: this.icon,
}));
this._mouseEventsChanged();
}
_clearListener (name) {
if (this._listeners[name]) {
this.marker.removeListener(this._listeners[name]);
this._listeners[name] = null;
}
}
_forwardEvent (name) {
if (this._listeners[name]) return;
this._listeners[name] = this.marker.addListener(name, (evt) => {
this.fire(name, evt);
});
}
_mouseEventsChanged () {
if (this.marker) {
if (this.mouseEvents) {
this._forwardEvent('click');
} else {
this._clearListener('click');
}
}
}
_positionChanged (latitude, longitude) {
if (!this.marker) {
return;
}
this.debounce('_positionChanged', () => {
this.marker.setPosition({ lat: latitude, lng: longitude });
});
}
openInfo (content) {
if (!this.info) {
this.set('info', new window.google.maps.InfoWindow({
content: content,
}));
} else {
this.info.setContent(content);
}
this.info.open(this.map, this.marker);
}
closeInfo () {
if (this.info) {
this.info.close();
}
}
}
define('map-google-marker', MapGoogleMarker);