-
Notifications
You must be signed in to change notification settings - Fork 3
/
map-google-view.js
145 lines (117 loc) · 3.23 KB
/
map-google-view.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { define } from '@xinix/xin';
import { View } from '@xinix/xin/components/view';
import html from './templates/map-google-view.html';
import './map-google';
import './map-google-marker';
import './css/map-google-view.css';
export class MapGoogleView extends View {
get template () {
return html;
}
get props () {
return Object.assign({}, super.props, {
title: {
type: String,
value: 'Choose Map',
},
latitude: {
type: Number,
observer: '_locationChanged(latitude, longitude)',
},
longitude: {
type: Number,
observer: '_locationChanged(latitude, longitude)',
},
apiKey: {
type: String,
},
value: {
type: Object,
value: () => ({}),
},
search: {
type: String,
value: '',
},
callback: {
type: Function,
value: () => {
return result => {
console.info('Choose callback do nothing');
};
},
},
});
}
ready () {
super.ready();
this.classList.add('map-google-view');
}
focusing (parameters) {
super.focusing(parameters);
if (parameters.latitude && parameters.longitude) {
this.set('latitude', Number(parameters.latitude));
this.set('longitude', Number(parameters.longitude));
} else {
navigator.geolocation.getCurrentPosition(position => {
// if (this.latitude || this.longitude) {
// return;
// }
this.set('latitude', position.coords.latitude);
this.set('longitude', position.coords.longitude);
});
}
}
focused () {
super.focused();
this.$.map.resize();
}
_chooseClicked (evt) {
evt.stopImmediatePropagation();
if (this.isSet) {
window.history.back();
this.callback(this.value);
}
}
_mapReady (evt) {
let searchBox = new window.google.maps.places.SearchBox(this.$.search);
searchBox.addListener('places_changed', () => {
this.$.search.blur();
let places = searchBox.getPlaces();
if (places && places.length) {
this.$.map.map.setCenter(places[0].geometry.location);
}
});
}
_locationChanged (latitude, longitude) {
if (!window.google || !latitude || !longitude) {
return;
}
this.isSet = false;
this.debounce('_locationChanged', () => {
let geocoder = new window.google.maps.Geocoder();
geocoder.geocode({ location: { lat: this.latitude, lng: this.longitude } }, (results, status) => {
if (status !== window.google.maps.GeocoderStatus.OK) {
return;
}
let val = {
latitude: this.latitude,
longitude: this.longitude,
googleFormattedAddress: results[0].formatted_address,
address: results[0].formatted_address,
components: {},
};
results[0].address_components.forEach(component => {
if (component.types[0] === 'political') {
return;
}
val.components[component.types[0]] = component.long_name;
});
this.set('value', val);
this.set('search', val.address);
this.isSet = true;
});
});
}
}
define('map-google-view', MapGoogleView);