Skip to content

Commit abccd54

Browse files
authored
Merge pull request #207 from jlamanskygitt/master
Updated Reverse Geocoding Logic
2 parents 2a98ed4 + 156c957 commit abccd54

4 files changed

Lines changed: 40 additions & 31 deletions

File tree

src/components/form/dt-location-map/dt-location-map.stories.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export default {
8080
error: { control: 'text' },
8181
slot: { control: 'text' },
8282
onChange: { action: 'on-change' },
83+
onGeocode: { action: 'dt:geocode' },
8384
...argTypes,
8485
},
8586
args: {
@@ -106,6 +107,7 @@ export default {
106107
error: '',
107108
slot: '',
108109
onChange: action('on-change'),
110+
onGeocode: action('dt:geocode'),
109111
},
110112
render: args => {
111113
const {
@@ -125,6 +127,7 @@ export default {
125127
loading = false,
126128
saved = false,
127129
onChange,
130+
onGeocode,
128131
open,
129132
slot,
130133
i18n,
@@ -155,6 +158,7 @@ export default {
155158
limit="${ifDefined(args.limit)}"
156159
error="${ifDefined(args.error)}"
157160
@change=${args.onChange}
161+
@dt:geocode=${args.onGeocode}
158162
>
159163
${args.slot}
160164
</dt-location-map>

src/components/form/dt-location-map/dt-map-modal.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class DtMapModal extends DtBase {
4747
connectedCallback() {
4848
super.connectedCallback();
4949

50-
this.canEdit = !this.metadata;
50+
this.canEdit = !(this.metadata?.lat);
5151

5252
if (!window.mapboxgl) {
5353
const script = document.createElement('script');
@@ -85,24 +85,27 @@ export class DtMapModal extends DtBase {
8585
// Add pin if there is one
8686
this.addPinFromMetadata();
8787

88-
// If map is editable add/move marker on click
89-
this.map.on('click', (e) => {
88+
// Keep pin in the center of the map
89+
this.map.on('move', () => {
9090
if (!this.canEdit) {
9191
return;
9292
}
93+
94+
const currentCenter = this.map.getCenter();
95+
9396
if (this.marker) {
94-
this.marker.setLngLat(e.lngLat)
97+
this.marker.setLngLat(currentCenter);
9598
} else {
9699
this.marker = new mapboxgl.Marker()
97-
.setLngLat(e.lngLat)
100+
.setLngLat(currentCenter)
98101
.addTo(this.map);
99102
}
100103
});
101104
}
102105
}
103106

104107
addPinFromMetadata() {
105-
if (this.metadata) {
108+
if (this.metadata?.lat) {
106109
const { lng, lat, level } = this.metadata;
107110
let zoom = 15
108111
if (level === 'admin0') {
@@ -137,7 +140,7 @@ export class DtMapModal extends DtBase {
137140
}
138141

139142
onClose(e) {
140-
if (e?.detail?.action === 'button' && this.marker) {
143+
if (e?.detail?.action === 'button' && this.marker && this.canEdit) {
141144
this.dispatchEvent(new CustomEvent('submit', {
142145
detail: {
143146
location: this.marker.getLngLat(),
@@ -152,6 +155,7 @@ export class DtMapModal extends DtBase {
152155
.title=${this.metadata?.label}
153156
?isopen=${this.isOpen}
154157
hideButton
158+
closeButton
155159
@close=${this.onClose}
156160
tabindex="-1"
157161
>

src/services/componentService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ export default class ComponentService {
335335
if (results && results.length) {
336336
const place = results[0];
337337
onSuccess({
338-
lng: place.geometry.location.lng,
339-
lat: place.geometry.location.lat,
338+
lng: place.geometry.location.lng(),
339+
lat: place.geometry.location.lat(),
340340
level: place.types && place.types.length ? place.types[0] : null,
341341
label: place.formatted_address,
342342
source: 'user',

src/services/googleGeocodeService.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ export default class GoogleGeocodeService {
144144
*/
145145
async getPlaceDetails(metadata, language = 'en') {
146146
let response = null;
147+
148+
if (!metadata || !metadata.place_id) {
149+
return response;
150+
}
151+
147152
if (this.window.google) {
148153
const geocoder = new window.google.maps.Geocoder();
149154
try {
@@ -172,36 +177,32 @@ export default class GoogleGeocodeService {
172177
return response;
173178
}
174179

175-
/**
180+
/**
176181
* Reverse geocode a lng/lat pair to get place details
177182
* @param longitude
178183
* @param latitude
179184
* @param language
180185
* @returns {Promise<Array>}
181186
*/
182187
async reverseGeocode(longitude, latitude, language = 'en') {
183-
const params = new URLSearchParams({
184-
key: this.token,
185-
latlng: `${latitude},${longitude}`,
186-
language,
187-
result_type: [
188-
'point_of_interest',
189-
'establishment',
190-
'premise',
191-
'street_address',
192-
'neighborhood',
193-
'sublocality',
194-
'locality',
195-
'colloquial_area',
196-
'political',
197-
'country',
198-
].join('|')
188+
return new Promise((resolve) => {
189+
const geocoder = new this.window.google.maps.Geocoder();
190+
const latlng = { lat: parseFloat(latitude), lng: parseFloat(longitude) };
191+
192+
geocoder.geocode(
193+
{ location: latlng, language },
194+
(results, status) => {
195+
if (status === 'OK' && results) {
196+
resolve(results);
197+
} else if (status === 'ZERO_RESULTS') {
198+
resolve([]);
199+
} else {
200+
console.error('Reverse geocoding failed:', status);
201+
resolve([]);
202+
}
203+
}
204+
);
199205
});
200-
const apiUrl = `https://maps.googleapis.com/maps/api/geocode/json?${params}`;
201-
const response = await fetch(apiUrl, { method: 'GET' });
202-
203-
const result = await response.json();
204-
return result?.results;
205206
}
206207

207208
convert_level(level) {

0 commit comments

Comments
 (0)