zet-lib 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/moduleLib.js +165 -72
- package/package.json +1 -1
package/lib/moduleLib.js
CHANGED
|
@@ -147,92 +147,185 @@ m.location = function (req, res, key) {
|
|
|
147
147
|
let script = ``;
|
|
148
148
|
let head = ``;
|
|
149
149
|
let end = ``;
|
|
150
|
-
end
|
|
151
|
-
script += `let searchAddressMap;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
initMap(
|
|
160
|
-
$("#search_map_location").val("
|
|
161
|
-
|
|
150
|
+
end =+ `<script src="https://maps.googleapis.com/maps/api/js?key=${process.env.GOOGLE_KEY}&callback=initAutocompleteZmap&libraries=places,marker&language=ID&loading=async" defer></script>`
|
|
151
|
+
script += `let searchAddressMap, autocompleteMap, marker, map;
|
|
152
|
+
|
|
153
|
+
function initAutocompleteZmap() {
|
|
154
|
+
if (mapLocationJSON && mapLocationJSON.lat) {
|
|
155
|
+
$("#search_map_location").val(mapLocationJSON.address1);
|
|
156
|
+
initMap(mapLocationJSON.lat, mapLocationJSON.lon);
|
|
157
|
+
} else {
|
|
158
|
+
navigator.geolocation.getCurrentPosition(function(position) {
|
|
159
|
+
initMap(position.coords.latitude, position.coords.longitude);
|
|
160
|
+
$("#search_map_location").val("");
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
searchAddressMap = document.querySelector("#search_map_location");
|
|
165
|
+
|
|
166
|
+
// Use PlaceAutocompleteElement (new API) instead of deprecated Autocomplete
|
|
167
|
+
// Check if PlaceAutocompleteElement is available
|
|
168
|
+
if (google.maps.places && google.maps.places.PlaceAutocompleteElement) {
|
|
169
|
+
try {
|
|
170
|
+
// Create PlaceAutocompleteElement programmatically
|
|
171
|
+
autocompleteMap = new google.maps.places.PlaceAutocompleteElement({
|
|
172
|
+
inputField: searchAddressMap,
|
|
173
|
+
componentRestrictions: {country: ["ID"]},
|
|
174
|
+
requestedResultFields: ["formattedAddress", "geometry", "addressComponents"]
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Listen for place selection event
|
|
178
|
+
autocompleteMap.addEventListener("gmp-placeselect", function(event) {
|
|
179
|
+
if (event.place) {
|
|
180
|
+
fillInAddressNew(event.place);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
searchAddressMap.focus();
|
|
185
|
+
} catch (e) {
|
|
186
|
+
console.warn("Error initializing PlaceAutocompleteElement, falling back to Autocomplete:", e);
|
|
187
|
+
// Fallback to old Autocomplete API
|
|
188
|
+
autocompleteMap = new google.maps.places.Autocomplete(searchAddressMap, {
|
|
189
|
+
componentRestrictions: {country: ["ID"]},
|
|
190
|
+
fields: ["address_component", "geometry"]
|
|
191
|
+
});
|
|
192
|
+
searchAddressMap.focus();
|
|
193
|
+
autocompleteMap.addListener("place_changed", fillInAddress);
|
|
162
194
|
}
|
|
163
|
-
|
|
195
|
+
} else {
|
|
196
|
+
// Fallback to old Autocomplete API if PlaceAutocompleteElement is not available
|
|
197
|
+
console.warn("PlaceAutocompleteElement not available, using legacy Autocomplete");
|
|
164
198
|
autocompleteMap = new google.maps.places.Autocomplete(searchAddressMap, {
|
|
165
199
|
componentRestrictions: {country: ["ID"]},
|
|
166
|
-
fields: ["address_component", "geometry"]
|
|
200
|
+
fields: ["address_component", "geometry"]
|
|
167
201
|
});
|
|
168
202
|
searchAddressMap.focus();
|
|
169
203
|
autocompleteMap.addListener("place_changed", fillInAddress);
|
|
170
204
|
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function fillInAddress() {
|
|
208
|
+
var place = autocompleteMap.getPlace();
|
|
209
|
+
var lat = place.geometry.location.lat();
|
|
210
|
+
var lng = place.geometry.location.lng();
|
|
211
|
+
initMap(lat, lng);
|
|
212
|
+
mapLocationJSON = {
|
|
213
|
+
lat: lat,
|
|
214
|
+
lon: lng,
|
|
215
|
+
address1: $("#search_map_location").val(),
|
|
216
|
+
address2: place.address_components
|
|
217
|
+
};
|
|
218
|
+
setAddress();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function fillInAddressNew(place) {
|
|
222
|
+
// Handle PlaceAutocompleteElement place selection
|
|
223
|
+
var location = place.geometry.location;
|
|
224
|
+
var lat = typeof location.lat === 'function' ? location.lat() : location.lat;
|
|
225
|
+
var lng = typeof location.lng === 'function' ? location.lng() : location.lng;
|
|
171
226
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
227
|
+
initMap(lat, lng);
|
|
228
|
+
mapLocationJSON = {
|
|
229
|
+
lat: lat,
|
|
230
|
+
lon: lng,
|
|
231
|
+
address1: place.formattedAddress || $("#search_map_location").val(),
|
|
232
|
+
address2: place.addressComponents || []
|
|
233
|
+
};
|
|
234
|
+
setAddress();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function setAddress() {
|
|
238
|
+
$("#${key}").val(JSON.stringify(mapLocationJSON));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function initMap(lat, lng) {
|
|
242
|
+
var position = {lat: lat, lng: lng};
|
|
243
|
+
|
|
244
|
+
// Create map with mapId for AdvancedMarkerElement support
|
|
245
|
+
// If you have a custom map ID from Google Cloud Console, replace "DEMO_MAP_ID" with it
|
|
246
|
+
map = new google.maps.Map(document.getElementById("map_${key}"), {
|
|
247
|
+
zoom: 13,
|
|
248
|
+
center: position,
|
|
249
|
+
draggable: true,
|
|
250
|
+
mapId: "DEMO_MAP_ID" // Required for AdvancedMarkerElement - replace with your actual Map ID
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// Use AdvancedMarkerElement instead of deprecated Marker
|
|
254
|
+
// Note: AdvancedMarkerElement requires mapId on the map
|
|
255
|
+
try {
|
|
256
|
+
marker = new google.maps.marker.AdvancedMarkerElement({
|
|
257
|
+
position: position,
|
|
258
|
+
map: map,
|
|
259
|
+
gmpDraggable: true,
|
|
260
|
+
title: $("#search_map_location").val() || "Location"
|
|
194
261
|
});
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
262
|
+
|
|
263
|
+
// Handle marker drag end
|
|
264
|
+
marker.addListener("dragend", function(event) {
|
|
265
|
+
var newPosition = event.latLng || marker.position;
|
|
266
|
+
mapLocationJSON.lat = typeof newPosition.lat === 'function' ? newPosition.lat() : newPosition.lat;
|
|
267
|
+
mapLocationJSON.lon = typeof newPosition.lng === 'function' ? newPosition.lng() : newPosition.lng;
|
|
268
|
+
|
|
269
|
+
var geocoder = new google.maps.Geocoder();
|
|
270
|
+
geocoder.geocode({location: newPosition}, function(results, status) {
|
|
271
|
+
if (status == google.maps.GeocoderStatus.OK) {
|
|
272
|
+
$("#search_map_location").val(results[0].formatted_address);
|
|
273
|
+
$("#mapErrorMsg").hide(100);
|
|
274
|
+
mapLocationJSON.address2 = results[0].formatted_address;
|
|
275
|
+
mapLocationJSON.address1 = $("#search_map_location").val();
|
|
276
|
+
setAddress();
|
|
277
|
+
} else {
|
|
278
|
+
$("#mapErrorMsg").html("Cannot determine address at this location." + status).show(100);
|
|
279
|
+
}
|
|
204
280
|
});
|
|
205
|
-
map.panTo(position);
|
|
206
|
-
}
|
|
207
|
-
google.maps.event.addListener(marker, 'dragend', function () {
|
|
208
|
-
geocodePosition(marker.getPosition());
|
|
209
281
|
});
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
282
|
+
|
|
283
|
+
// Handle marker click
|
|
284
|
+
marker.addListener("click", function() {
|
|
285
|
+
var infoWindow = new google.maps.InfoWindow({
|
|
286
|
+
content: $("#search_map_location").val()
|
|
287
|
+
});
|
|
288
|
+
infoWindow.open(map, marker);
|
|
289
|
+
});
|
|
290
|
+
} catch (e) {
|
|
291
|
+
// Fallback to old Marker API if AdvancedMarkerElement is not available
|
|
292
|
+
console.warn("AdvancedMarkerElement not available, using legacy Marker:", e);
|
|
293
|
+
marker = new google.maps.Marker({
|
|
294
|
+
position: position,
|
|
295
|
+
map: map,
|
|
296
|
+
draggable: true,
|
|
297
|
+
animation: google.maps.Animation.DROP
|
|
213
298
|
});
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
299
|
+
|
|
300
|
+
google.maps.event.addListener(marker, "dragend", function() {
|
|
301
|
+
var newPosition = marker.getPosition();
|
|
302
|
+
mapLocationJSON.lat = newPosition.lat();
|
|
303
|
+
mapLocationJSON.lon = newPosition.lng();
|
|
304
|
+
|
|
217
305
|
var geocoder = new google.maps.Geocoder();
|
|
218
|
-
geocoder.geocode
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
mapLocationJSON.address1=$("#search_map_location").val();
|
|
228
|
-
setAddress();
|
|
229
|
-
} else {
|
|
230
|
-
$("#mapErrorMsg").html('Cannot determine address at this location.' + status).show(100);
|
|
231
|
-
}
|
|
306
|
+
geocoder.geocode({latLng: newPosition}, function(results, status) {
|
|
307
|
+
if (status == google.maps.GeocoderStatus.OK) {
|
|
308
|
+
$("#search_map_location").val(results[0].formatted_address);
|
|
309
|
+
$("#mapErrorMsg").hide(100);
|
|
310
|
+
mapLocationJSON.address2 = results[0].formatted_address;
|
|
311
|
+
mapLocationJSON.address1 = $("#search_map_location").val();
|
|
312
|
+
setAddress();
|
|
313
|
+
} else {
|
|
314
|
+
$("#mapErrorMsg").html("Cannot determine address at this location." + status).show(100);
|
|
232
315
|
}
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
google.maps.event.addListener(marker, "click", function() {
|
|
320
|
+
var infoWindow = new google.maps.InfoWindow({
|
|
321
|
+
content: $("#search_map_location").val()
|
|
322
|
+
});
|
|
323
|
+
infoWindow.open(map, marker);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
map.panTo(position);
|
|
328
|
+
}`;
|
|
236
329
|
return {
|
|
237
330
|
head: head,
|
|
238
331
|
end: end,
|