PageSourceSearch

https://neuroptimal.com/wp-content/plugins/google-maps-sort-by-dis…/assets/js/sort-by-distance.js?v=489324

js neuroptimal.com collected 2026-10-01 14:36:27 UTC 32,903 bytes, 490 lines download raw bytes

1(function($, window, document, undefined) {
2    "use strict";
3
4    jQuery( window ).on('load', function() { // Removed $ parameter here as it's defined globally by jQuery
5                    
6        jQuery.fn.locationSortByDistance = function ( options ) { 
7            
8            let map_obj = jQuery(options.wpgmp_map_selector).data("wpgmp_maps"); 
9
10            if (typeof map_obj === 'undefined' || !map_obj) {
11                console.error("WP Maps Pro Sort By Distance Addon: Failed to get map object using selector '" + options.wpgmp_map_selector + "' and data key 'wpgmp_maps' even after window.onload. The main plugin might have updated its structure or initialization timing further.");
12                return this; 
13            }
14
15            let is_place_changed = false;
16            let is_location_allowed = false;
17            let sorted_locations_by_search = [];
18            let search_box_autocomplete = '';
19            let current_address = '';
20            let current_user_location = '';
21
22            let btn_html = '<div><button type="button" id="btn_locate_me" class="fc_btn_locate_me">Locate Me</button></div>';
23
24            if (map_obj.settings && map_obj.settings.locateme_control == true ) {
25                 if ($('body').find('.categories_filter').length) {
26                     $('body').find('.categories_filter').append(btn_html);
27                 } else if (map_obj.container && $(map_obj.container).find('.categories_filter').length) {
28                     $(map_obj.container).find('.categories_filter').append(btn_html);
29                 } else {
30                    console.warn("WP Maps Pro Sort By Distance Addon: '.categories_filter' not found to append 'Locate Me' button.");
31                 }
32            }
33
34            if (map_obj.container) {
35                $(map_obj.container).on("click", "#btn_locate_me", function() {
36                    map_obj.is_place_changed = true;
37                    if ($(map_obj.container).find('.wpgmp_locateme_control').length) {
38                        $(map_obj.container).find('.wpgmp_locateme_control').trigger('click');
39                    } else {
40                         console.warn("WP Maps Pro Sort By Distance Addon: '.wpgmp_locateme_control' not found for button click.");
41                    }
42                });
43
44                $(map_obj.container).on("click", ".wpgmp_locateme_control", function() {
45                    if (typeof map_obj.get_current_location === 'function') {
46                        map_obj.get_current_location(function(user_location) {
47                            
48                            current_user_location = user_location;
49                            let position_lat = user_location.lat();
50                            let position_lng = user_location.lng();
51                            let google_map_position = new google.maps.LatLng( position_lat, position_lng );
52                            let google_maps_geocoder = new google.maps.Geocoder();
53                            google_maps_geocoder.geocode(
54                                { 'latLng': google_map_position },
55                                function( results, status ) {
56                                    if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
57                                        
58                                        map_obj.search_area = results[0].geometry.location;
59                                        map_obj.search_place = results[0];
60                                        current_address = results[0].formatted_address;
61                                        if ($(map_obj.container).find('[data-input="wpgmp-search-text"]').length) {
62                                            $(map_obj.container).find('[data-input="wpgmp-search-text"]').val(current_address);
63                                        }
64                                    }
65                                }
66                            );
67
68                            if (map_obj.map && typeof map_obj.map.setCenter === 'function') {
69                                map_obj.map.setCenter(user_location);
70                            }
71
72                            if (map_obj.map_center_marker && typeof map_obj.map_center_marker.setPosition === 'function') {
73                                map_obj.map_center_marker.setPosition(user_location);
74                            }
75
76                            if (map_obj.set_center_circle && typeof map_obj.set_center_circle.setCenter === 'function') {
77                                map_obj.set_center_circle.setCenter(user_location);
78                            }
79                            
80                            if (typeof map_obj.shuffle_by_distance_locateme === 'function' && typeof map_obj.update_places_listing === 'function') {
81                                map_obj.sorted_locations_by_search = map_obj.shuffle_by_distance_locateme(map_obj.show_places || []); 
82                                map_obj.update_places_listing();
83                            }
84
85                             if ($('.wpgmp_listing_list div.distance').length) {
86                                $('.wpgmp_listing_list div.distance').css('display','block').show();
87                             }
88
89                        });
90                    } else {
91                         console.error("WP Maps Pro Sort By Distance Addon: map_obj.get_current_location is not a function.");
92                    }
93                });
94
95                if ($(map_obj.container).find('[data-name="radius"]').length) {
96                    $(map_obj.container).find('[data-name="radius"]').on('change', function() {                
97                        var radius = $(this).val();
98                        
99                        if( radius != '' && typeof map_obj.search_area != 'undefined' && map_obj.search_area != '' && Array.isArray(map_obj.places)){                 
100                            map_obj.show_places = [];                  
101                            $.each(map_obj.places, function (place_key, place) {
102                                if (place && place.marker && typeof place.marker.setVisible === 'function' && typeof map_obj.wpgmp_within_radius === 'function') {
103                                    if (typeof map_obj.search_area != 'undefined' && map_obj.search_area != '' && map_obj.wpgmp_within_radius(place, map_obj.search_area) === true) {
104                                        place.marker.visible = true;
105                                        place.marker.setVisible(true);
106                                        map_obj.places[place_key] = place;
107                                        map_obj.show_places.push(place);
108                                    } else {
109                                        place.marker.visible = false;
110                                        place.marker.setVisible(false);
111                                        map_obj.places[place_key] = place;
112                                    }
113                                }
114                            });
115                            if (typeof map_obj.update_places_listing === 'function') {
116                                map_obj.update_places_listing();
117                            }
118                        } else if (typeof map_obj.update_filters === 'function') {
119                            map_obj.update_filters();
120                        }
121                        if (typeof map_obj.sort_fit_bounds === 'function') {
122                             map_obj.sort_fit_bounds();
123                        }
124                        if (typeof map_obj.set_marker_cluster === 'function') {
125                            setTimeout(function () {
126                                map_obj.set_marker_cluster();
127                            }, 600);
128                        }
129                    }); 
130                } 
131
132                if ($(map_obj.container).find('[data-filter="dropdown"]').length) {
133                     $(map_obj.container).on('change', '[data-filter="dropdown"]', function () {
134                         if (map_obj.map_data && map_obj.map_data.listing && map_obj.map_data.listing.apply_default_radius == true && typeof map_obj.search_area != 'undefined' && map_obj.search_area != '' && Array.isArray(map_obj.places)){
135                             map_obj.show_places = [];
136                             $.each(map_obj.places, function (place_key, place) {
137                                 if (place && place.marker && typeof place.marker.setVisible === 'function' && typeof map_obj.wpgmp_within_radius === 'function') {
138                                     if (typeof map_obj.search_area != 'undefined' && map_obj.search_area != '' && map_obj.wpgmp_within_radius(place, map_obj.search_area) === true) {
139                                         place.marker.visible = true;
140                                         place.marker.setVisible(true);
141                                         map_obj.places[place_key] = place;
142                                         map_obj.show_places.push(place);
143                                     } else {
144                                         place.marker.visible = false;
145                                         place.marker.setVisible(false);
146                                         map_obj.places[place_key] = place;
147                                     }
148                                 }
149                             });
150                             if (typeof map_obj.update_places_listing === 'function') {
151                                map_obj.update_places_listing();
152                             }
153                         } else if (typeof map_obj.update_filters === 'function') {
154                             map_obj.update_filters();
155                         }
156                         if (typeof map_obj.sort_fit_bounds === 'function') {
157                            map_obj.sort_fit_bounds();
158                         }
159                         let selected_radius = $(map_obj.container).find('[data-name="radius"]').val();
160                         if(selected_radius != '') {
161                             if ($(map_obj.container).find('[data-name="radius"]').length) {
162                                $(map_obj.container).find('[data-name="radius"]').val(selected_radius).trigger('change');
163                             }
164                         }
165                     }); 
166                } 
167
168                 if ($(map_obj.container).find('.categories_filter_reset_btn').length) {
169                     $(map_obj.container).on('click', '.categories_filter_reset_btn', function () {
170                         if (map_obj.places && Array.isArray(map_obj.places)) {
171                            $.each(map_obj.places, function (place_key, place) {
172                                if (place && place.location && place.location.extra_fields) {
173                                    delete place.location.extra_fields.distance;
174                                }
175                            });
176                         }
177                         delete map_obj.search_place; 
178                         if (typeof map_obj.update_places_listing === 'function') {
179                            map_obj.update_places_listing();
180                         }
181                     }); 
182                 } 
183            }
184
185
186            // --- Functions defined on map_obj ---
187            map_obj.shuffle_by_distance_locateme = function(array){
188                if (!map_obj.map || typeof map_obj.map.getCenter !== 'function' || !Array.isArray(array)) { return array; }
189                let current_center = map_obj.map.getCenter();
190                if (!current_center || typeof current_center.lat !== 'function' || typeof current_center.lng !== 'function') { return array; }
191                let latitude1 = current_center.lat();
192                let longitude1 = current_center.lng();
193                for(let i=0; i<array.length; i++ ){
194                    if (array[i] && array[i].location && typeof array[i].location.lat !== 'undefined' && typeof array[i].location.lng !== 'undefined') {
195                        let latitude2 = array[i].location.lat;
196                        let longitude2 = array[i].location.lng;
197                        try {
198                            let distance = google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(latitude1, longitude1), new google.maps.LatLng(latitude2, longitude2));
199                            array[i].distance = distance;   
200                        } catch(e) { array[i].distance = Infinity; }
201                    } else { array[i].distance = Infinity; }
202                }
203                array.sort(function(obj1, obj2) {
204                    const dist1 = (typeof obj1.distance === 'number' && !isNaN(obj1.distance)) ? obj1.distance : Infinity;
205                    const dist2 = (typeof obj2.distance === 'number' && !isNaN(obj2.distance)) ? obj2.distance : Infinity;
206                    return dist1 - dist2;
207                });
208                return array;
209            }
210
211            map_obj.sort_fit_bounds = function() {
212                if (!map_obj.map || typeof map_obj.map.fitBounds !== 'function' || !Array.isArray(map_obj.show_places)) { return; }
213                let bounds = new google.maps.LatLngBounds();
214                let boundsExtended = false;
215                map_obj.show_places.forEach(function(place) {
216                    if (place && place.location && place.location.lat && place.location.lng && place.marker && place.marker.visible == true) {
217                        try {
218                            bounds.extend(new google.maps.LatLng(parseFloat(place.location.lat), parseFloat(place.location.lng)));
219                            boundsExtended = true;
220                        } catch(e) { console.error("Error extending bounds:", e, place); }
221                    }
222                });
223                if (boundsExtended) { map_obj.map.fitBounds(bounds); }
224            }
225
226            map_obj.center_map_for_filtered_places = function() {
227                if (!map_obj.map_data || !map_obj.map_data.map_options || !map_obj.map || typeof map_obj.map.fitBounds !== 'function' || !Array.isArray(map_obj.show_places)) { return; }
228                if(typeof map_obj.map_data.map_options.bound_map_after_filter !== typeof undefined && map_obj.map_data.map_options.bound_map_after_filter == 'true') {
229                    let after_filter_bounds = new google.maps.LatLngBounds();
230                    let boundsExtended = false;
231                    for(const place of map_obj.show_places){
232                        if (place && place.location && place.location.lat && place.location.lng) {
233                            try {
234                                let markerInResult = new google.maps.LatLng(place.location.lat, place.location.lng);
235                                after_filter_bounds.extend(markerInResult);
236                                boundsExtended = true;
237                             } catch (e) { console.error("Error extending bounds in center_map_for_filtered_places:", e, place); }
238                        }
239                    }
240                    if (boundsExtended) { map_obj.map.fitBounds(after_filter_bounds); }
241                }
242            }
243
244            map_obj.center_map_load_nearest = function(){
245                is_place_changed = true;
246                if (typeof map_obj.get_current_location !== 'function') { return; }
247                map_obj.get_current_location(function(user_location) {
248                    if (!map_obj.map || typeof map_obj.map.setCenter !== 'function') { return; }
249                    map_obj.map.setCenter(user_location);
250                    current_user_location = user_location;
251                    let position_lat = user_location.lat();
252                    let position_lng = user_location.lng();
253                    let google_map_position = new google.maps.LatLng( position_lat, position_lng );
254                    let google_maps_geocoder = new google.maps.Geocoder();
255                    google_maps_geocoder.geocode( { 'latLng': google_map_position }, function( results, status ) {
256                        if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
257                            current_address = results[0].formatted_address;
258                            if (map_obj.container && $(map_obj.container).find('[data-input="wpgmp-search-text"]').length) {
259                                $(map_obj.container).find('[data-input="wpgmp-search-text"]').val(current_address);
260                            }
261                        }
262                    });
263                    if( map_obj.map_center_marker && typeof map_obj.map_center_marker.setPosition === 'function' ) { map_obj.map_center_marker.setPosition(user_location); }
264                    if( map_obj.set_center_circle && typeof map_obj.set_center_circle.setCenter === 'function' ) { map_obj.set_center_circle.setCenter(user_location); }
265                    if (typeof map_obj.shuffle_by_distance_single === 'function' && typeof map_obj.update_places_listing === 'function') {
266                        map_obj.show_places = map_obj.shuffle_by_distance_single(map_obj.show_places || []);
267                        sorted_locations_by_search = map_obj.show_places;
268                        map_obj.update_places_listing();
269                    }
270                    if (typeof map_obj.center_map_for_filtered_places === 'function') { map_obj.center_map_for_filtered_places(); }
271                    if ($('.wpgmp_listing_list div.distance').length) { $('.wpgmp_listing_list div.distance').css('display','block').show(); }
272                });
273            }
274
275            map_obj.shuffle_by_distance_single = function(array){
276                 if (!map_obj.map || typeof map_obj.map.getCenter !== 'function' || !Array.isArray(array)) { return array; }
277                let current_center = map_obj.map.getCenter();
278                if (!current_center || typeof current_center.lat !== 'function' || typeof current_center.lng !== 'function') { return array; }
279                let latitude1 = current_center.lat();
280                let longitude1 = current_center.lng();
281                for(let i=0; i<array.length; i++ ){
282                     if (array[i] && array[i].location && typeof array[i].location.lat !== 'undefined' && typeof array[i].location.lng !== 'undefined') {
283                        let latitude2 = array[i].location.lat;
284                        let longitude2 = array[i].location.lng;
285                        try {
286                            let distance = google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(latitude1, longitude1), new google.maps.LatLng(latitude2, longitude2));
287                            array[i].distance = distance;
288                         } catch (e) { array[i].distance = Infinity; }
289                    } else { array[i].distance = Infinity; }
290                }
291                array.sort(function(obj1, obj2) {
292                    const dist1 = (typeof obj1.distance === 'number' && !isNaN(obj1.distance)) ? obj1.distance : Infinity;
293                    const dist2 = (typeof obj2.distance === 'number' && !isNaN(obj2.distance)) ? obj2.distance : Infinity;
294                    return dist1 - dist2;
295                });
296                return array;
297            }
298
299            map_obj.sort_by_distance = function(array){
300                 if (!Array.isArray(array)) { return array; }
301                array.sort(function(obj1, obj2) {
302                    if(obj1 && obj1.hasOwnProperty("distance") && typeof obj1.distance === 'number' && !isNaN(obj1.distance) && obj2 && obj2.hasOwnProperty("distance") && typeof obj2.distance === 'number' && !isNaN(obj2.distance)){
303                        return obj1.distance - obj2.distance;
304                    }
305                    return 0; 
306                });
307                return array;
308            }
309
310            map_obj.shuffle_by_distance = function(array,location) {
311                if (!Array.isArray(array)) { return array; }
312                if(location != undefined && location.geometry && location.geometry.location && typeof location.geometry.location.lat === 'function' && typeof location.geometry.location.lng === 'function') {
313                    let latitude1 = location.geometry.location.lat();
314                    let longitude1 = location.geometry.location.lng();
315                    for(let i=0; i<array.length; i++ ){
316                         if (array[i] && array[i].location && typeof array[i].location.lat !== 'undefined' && typeof array[i].location.lng !== 'undefined') {
317                            let latitude2 = array[i].location.lat;
318                            let longitude2 = array[i].location.lng;
319                             try {
320                                let distance = google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(latitude1, longitude1), new google.maps.LatLng(latitude2, longitude2));
321                                array[i].distance = distance;
322                            } catch (e) { array[i].distance = Infinity; }
323                        } else { array[i].distance = Infinity; }
324                    }
325                    if (typeof map_obj.sort_by_distance === 'function') { array = map_obj.sort_by_distance(array); }
326                    return array;
327                } else { return array; }
328            }
329            
330            map_obj.shuffle_by_distance_latlng = function(array,lat,lng){
331                 if (!Array.isArray(array)) { return array; }
332                 if (typeof lat !== 'number' || isNaN(lat) || typeof lng !== 'number' || isNaN(lng)) { return array; }
333                let latitude1 = lat;
334                let longitude1 = lng;
335                for( let i=0; i<array.length; i++ ){
336                    if (array[i] && array[i].location && typeof array[i].location.lat !== 'undefined' && typeof array[i].location.lng !== 'undefined') {
337                        let latitude2 = array[i].location.lat;
338                        let longitude2 = array[i].location.lng;
339                        try {
340                            let distance = google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(latitude1, longitude1), new google.maps.LatLng(latitude2,longitude2 ) );
341                            array[i].distance = distance; 
342                            array[i].distancecopy = distance; 
343                         } catch (e) { array[i].distance = Infinity; array[i].distancecopy = Infinity; }
344                    } else { array[i].distance = Infinity; array[i].distancecopy = Infinity; }
345                }
346                 array.sort(function(obj1, obj2) {
347                    const dist1 = (obj1 && typeof obj1.distancecopy === 'number' && !isNaN(obj1.distancecopy)) ? obj1.distancecopy : Infinity;
348                    const dist2 = (obj2 && typeof obj2.distancecopy === 'number' && !isNaN(obj2.distancecopy)) ? obj2.distancecopy : Infinity;
349                    return dist1 - dist2;
350                });
351                return array;
352            }
353
354
355            map_obj.addon_google_auto_suggest = function(obj) {
356                obj.each(function() {
357                    let current_input = this;
358                    let autocomplete; 
359                    try { autocomplete = new google.maps.places.Autocomplete(this); } 
360                    catch(e) { console.error("Error initializing Google Places Autocomplete:", e); return; }
361                    
362                    search_box_autocomplete  = autocomplete;
363
364                    if (typeof wpgmp_local !== 'undefined' && wpgmp_local.wpgmp_country_specific && wpgmp_local.wpgmp_country_specific == true && Array.isArray(wpgmp_local.wpgmp_countries)) {
365                        try { autocomplete.setComponentRestrictions({ 'country': wpgmp_local.wpgmp_countries }); } 
366                        catch(e) { console.error("Error setting Autocomplete restrictions:", e); }
367                    }
368
369                    try { autocomplete.bindTo('bounds', map_obj.map); } 
370                    catch (e) { console.error("Error binding Autocomplete to map bounds:", e); }
371                   
372                    if ($(this).attr("name") == 'location_address') {
373                        // Admin-side or specific location input logic
374                        let marker;
375                         try { marker = new google.maps.Marker({ map: map_obj.map, draggable: true, anchorPoint: new google.maps.Point(0, -29) }); } 
376                         catch (e) { console.error("Error creating Marker for location_address:", e); return; }
377                        if (typeof map_obj.marker_bind === 'function') { map_obj.marker_bind(marker); }
378
379                        google.maps.event.addListener(autocomplete, 'place_changed', function() {
380                            let place = autocomplete.getPlace();
381                            if (!place || !place.geometry) { console.warn("Autocomplete returned place with no geometry."); return; }
382                            if (place.geometry.viewport) { map_obj.map.fitBounds(place.geometry.viewport); } 
383                            else { map_obj.map.setCenter(place.geometry.location); map_obj.map.setZoom(17); }
384                            try {
385                                $(".google_latitude").val(place.geometry.location.lat());
386                                $(".google_longitude").val(place.geometry.location.lng());
387                                if (typeof map_obj.wpgmp_finddata === 'function') {
388                                    $(".google_city").val(map_obj.wpgmp_finddata(place, 'administrative_area_level_3') || map_obj.wpgmp_finddata(place, 'locality') || '');
389                                    $(".google_state").val(map_obj.wpgmp_finddata(place, "administrative_area_level_1") || '');
390                                    $(".google_country").val(map_obj.wpgmp_finddata(place, "country") || '');
391                                }
392                                if (place.address_components) {
393                                    for (let i = 0; i < place.address_components.length; i++) {
394                                        for (let j = 0; j < place.address_components[i].types.length; j++) {
395                                            if (place.address_components[i].types[j] == "postal_code") {
396                                                $(".google_postal_code").val(place.address_components[i].long_name);
397                                            }
398                                        }
399                                    }
400                                }
401                            } catch (e) { console.error("Error updating address fields:", e); }
402                            marker.setPosition(place.geometry.location);
403                            marker.setVisible(true);
404                        });
405                    } else {
406                        // Frontend search input logic
407                        google.maps.event.addListener(autocomplete, 'place_changed', function() {
408                            map_obj.enable_search_term = true; 
409                            map_obj.search_term = '';
410                            map_obj.search_place = autocomplete.getPlace();
411                            if (map_obj.search_place && map_obj.search_place.geometry) {
412                                map_obj.search_area = map_obj.search_place.geometry.location;
413                                $(current_input).data('latitude', map_obj.search_area.lat());
414                                $(current_input).data('longitude', map_obj.search_area.lng());
415                                map_obj.is_place_changed = true; 
416                                if (typeof map_obj.shuffle_by_distance === 'function' && map_obj.places) {
417                                    map_obj.sorted_locations_by_search = map_obj.shuffle_by_distance(map_obj.places, map_obj.search_place);
418                                } else { console.error("shuffle_by_distance function or map_obj.places not available."); }
419                                if (typeof map_obj.update_filters === 'function') { map_obj.update_filters(); }
420                            } else {
421                                 console.warn("Autocomplete place has no geometry.");
422                                 map_obj.search_area = ''; 
423                                 map_obj.is_place_changed = false; 
424                                 if (typeof map_obj.update_filters === 'function') { map_obj.update_filters(); }
425                            }
426                            if (typeof map_obj.update_places_listing === 'function') { map_obj.update_places_listing(); }
427                            if (typeof map_obj.center_map_for_filtered_places === 'function') { map_obj.center_map_for_filtered_places(); }
428                            if (typeof map_obj.sort_fit_bounds === 'function') {
429                                map_obj.sort_fit_bounds();
430                                if (map_obj.map && map_obj.map.getZoom() > 1) { map_obj.map.setZoom(map_obj.map.getZoom() - 1); }
431                            }
432                            if($(map_obj.container).find('[data-name="radius"]').length > 0) {
433                               let selected_radius = $(map_obj.container).find('[data-name="radius"]').val();
434                                if(selected_radius != '') { $(map_obj.container).find('[data-name="radius"]').val(selected_radius).trigger('change'); }
435                            }
436                            if ($(map_obj.container).find('.distance').length) { $(map_obj.container).find('.distance').show(); }
437                        }); 
438
439                         if ($(map_obj.container).find('[data-input="wpgmp-search-text"]').length) {
440                            $(map_obj.container).find('[data-input="wpgmp-search-text"]').on('keyup', function () {
441                                map_obj.search_term = $(this).val().trim(); 
442                                if (map_obj.search_term.length > 0) {
443                                    map_obj.search_term = map_obj.search_term.toLowerCase();
444                                    map_obj.enable_search_term = true;
445                                } else { map_obj.enable_search_term = false; map_obj.search_term = ''; }
446                                if (typeof map_obj.update_filters === 'function') { map_obj.update_filters(); }
447                            });
448                         } 
449                    } 
450                }); 
451            }
451 // End addon_google_auto_suggest function definition
452            
453            let wpgmp_search_form = $(".wpgmp_auto_suggest");
454            if (wpgmp_search_form.length > 0 && typeof map_obj.addon_google_auto_suggest === 'function') {
455                map_obj.addon_google_auto_suggest(wpgmp_search_form);
456            }
457            
458            return this; 
459        }; 
460
461        // Initialization loop at the bottom
462        jQuery("div.wpgmp_map_container").each(function (index, element) { 
463             let mapDiv = $(element).find('div[id^="map"]'); 
464             let mapIdAttr = mapDiv.attr('id');
465
466             if (mapIdAttr) {
467                 let wpgmp_map_selector = "#" + mapIdAttr; 
468                 let wpgmp_layout_args = {'wpgmp_map_selector' : wpgmp_map_selector};
469                 
470                 if (jQuery(wpgmp_map_selector).length > 0) {
471                     // Add a small delay (e.g., 500ms) before initializing the addon
472                     setTimeout(function() {
473                         if (typeof jQuery.fn.locationSortByDistance === 'function') {
474                             jQuery(wpgmp_map_selector).locationSortByDistance(wpgmp_layout_args);
475                         } else {
476                             console.error("WP Maps Pro Sort By Distance Addon: locationSortByDistance function not defined.");
477                         }
478                     }, 500); // Delay in milliseconds
479                 } else {
480                     console.warn("WP Maps Pro Sort By Distance Addon: Map element " + wpgmp_map_selector + " not found.");
481                 }
482             } else {
483                 console.warn("WP Maps Pro Sort By Distance Addon: Could not find map ID within container: ", element);
484             }
485            
486        }); 
487
488    }); // End of window.on('load') wrapper
489
490}(jQuery, window, document)); // End of IIFE

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.