@@ -137,12 +137,19 @@ public static function cmb2_render_openstreetmap_field_type( $field, $escaped_va
137137 return ;
138138 }
139139
140+ // Set default min and max lat/lng.
140141 $ min_lat = null ;
141142 $ max_lat = null ;
142143 $ min_long = null ;
143144 $ max_long = null ;
144145
145- $ locations = [];
146+ // Set settings options for the map.
147+ $ center_lat = get_option ( 'openkaarten_base_default_lat ' , 52.0 );
148+ $ center_long = get_option ( 'openkaarten_base_default_lng ' , 4.75 );
149+ $ default_zoom = get_option ( 'openkaarten_base_default_zoom ' , 8 );
150+
151+ $ locations = [];
152+ $ fit_bounds = false ;
146153
147154 try {
148155 // Parse the feature collection.
@@ -151,62 +158,65 @@ public static function cmb2_render_openstreetmap_field_type( $field, $escaped_va
151158 // Get the bounding box of the geometry.
152159 $ bbox = $ geom ->getBBox ();
153160
154- // Set min and max values for the map.
155- $ min_lat = ( null === $ min_lat || $ bbox ['miny ' ] < $ min_lat ) ? $ bbox ['miny ' ] : $ min_lat ;
156- $ max_lat = ( null === $ max_lat || $ bbox ['maxy ' ] > $ max_lat ) ? $ bbox ['maxy ' ] : $ max_lat ;
157- $ min_long = ( null === $ min_long || $ bbox ['minx ' ] < $ min_long ) ? $ bbox ['minx ' ] : $ min_long ;
158- $ max_long = ( null === $ max_long || $ bbox ['maxx ' ] > $ max_long ) ? $ bbox ['maxx ' ] : $ max_long ;
159-
160- // Get average lat and long for the center of the map.
161- $ center_lat = ( $ min_lat + $ max_lat ) / 2 ;
162- $ center_long = ( $ min_long + $ max_long ) / 2 ;
163-
164- // Loop through all the components of the geometry and add them to the locations array with the right properties.
165- foreach ( $ geom ->getComponents () as $ component ) {
166- // Use json output to plot the geometry on the map.
167- $ location_output = $ component ->out ( 'json ' );
168- $ location_output = json_decode ( $ location_output , true );
169-
170- if ( isset ( $ location_output ['geometry ' ] ) ) {
171- $ geometry = $ location_output ['geometry ' ];
172- } else {
173- $ geometry = $ location_output ;
174- }
175-
176- $ location = [
177- 'feature ' => $ geometry ,
178- 'content ' => '' ,
179- 'icon ' => '' ,
180- 'color ' => '' ,
181- ];
182-
183- if ( ! empty ( $ location_output ['properties ' ] ) ) {
184-
185- // Get location properties.
186- $ location_properties = $ location_output ['properties ' ];
187- if ( 'live ' === $ datalayer_url_type ) {
188- $ location_data_for_marker = $ location_output ['properties ' ];
161+ if ( ! empty ( $ bbox ) ) {
162+ // Set min and max values for the map.
163+ $ min_lat = $ bbox ['miny ' ];
164+ $ max_lat = $ bbox ['maxy ' ];
165+ $ min_long = $ bbox ['minx ' ];
166+ $ max_long = $ bbox ['maxx ' ];
167+
168+ // Get average lat and long for the center of the map.
169+ $ center_lat = ( $ min_lat + $ max_lat ) / 2 ;
170+ $ center_long = ( $ min_long + $ max_long ) / 2 ;
171+
172+ // Loop through all the components of the geometry and add them to the locations array with the right properties.
173+ foreach ( $ geom ->getComponents () as $ component ) {
174+ // Use json output to plot the geometry on the map.
175+ $ location_output = $ component ->out ( 'json ' );
176+ $ location_output = json_decode ( $ location_output , true );
177+
178+ if ( isset ( $ location_output ['geometry ' ] ) ) {
179+ $ geometry = $ location_output ['geometry ' ];
189180 } else {
190- $ location_data_for_marker = $ location_output ;
181+ $ geometry = $ location_output ;
191182 }
192183
193- // Get marker information.
194- $ item_marker = Locations:: get_location_marker ( $ object_id , false , $ location_data_for_marker );
195- $ geom_marker = [
196- 'color ' => $ item_marker [ ' color ' ] ,
197- 'icon ' => Locations:: get_location_marker_url ( $ item_marker [ ' icon ' ] ) ,
184+ $ location = [
185+ ' feature ' => $ geometry ,
186+ ' content ' => '' ,
187+ 'icon ' => '' ,
188+ 'color ' => '' ,
198189 ];
199190
200- // Get title information based on title field mapping.
201- $ title_fields = get_post_meta ( $ object_id , 'title_field_mapping ' , true );
202- $ title = Importer::create_title_from_mapping ( $ location_properties , $ title_fields );
191+ if ( ! empty ( $ location_output ['properties ' ] ) ) {
192+
193+ // Get location properties.
194+ $ location_properties = $ location_output ['properties ' ];
195+ if ( 'live ' === $ datalayer_url_type ) {
196+ $ location_data_for_marker = $ location_output ['properties ' ];
197+ } else {
198+ $ location_data_for_marker = $ location_output ;
199+ }
200+
201+ // Get marker information.
202+ $ item_marker = Locations::get_location_marker ( $ object_id , false , $ location_data_for_marker );
203+ $ geom_marker = [
204+ 'color ' => $ item_marker ['color ' ],
205+ 'icon ' => Locations::get_location_marker_url ( $ item_marker ['icon ' ] ),
206+ ];
207+
208+ // Get title information based on title field mapping.
209+ $ title_fields = get_post_meta ( $ object_id , 'title_field_mapping ' , true );
210+ $ title = Importer::create_title_from_mapping ( $ location_properties , $ title_fields );
211+
212+ $ location ['content ' ] = $ title ;
213+ $ location ['icon ' ] = $ geom_marker ['icon ' ] ?? '' ;
214+ $ location ['color ' ] = $ geom_marker ['color ' ] ?? '' ;
215+ }
203216
204- $ location ['content ' ] = $ title ;
205- $ location ['icon ' ] = $ geom_marker ['icon ' ] ?? '' ;
206- $ location ['color ' ] = $ geom_marker ['color ' ] ?? '' ;
217+ $ locations [] = $ location ;
218+ $ fit_bounds = true ;
207219 }
208-
209- $ locations [] = $ location ;
210220 }
211221 } catch ( \Exception $ e ) {
212222 // Add error message via admin notice.
@@ -226,8 +236,8 @@ public static function cmb2_render_openstreetmap_field_type( $field, $escaped_va
226236 'maxLong ' => esc_attr ( $ max_long ),
227237 'centerLat ' => esc_attr ( $ center_lat ),
228238 'centerLong ' => esc_attr ( $ center_long ),
229- 'defaultZoom ' => 10 ,
230- 'fitBounds ' => true ,
239+ 'defaultZoom ' => $ default_zoom ,
240+ 'fitBounds ' => $ fit_bounds ,
231241 ]
232242 );
233243
0 commit comments