diff --git a/srcs/plugins/map_prof/map_prof_hooks.php b/srcs/plugins/map_prof/map_prof_hooks.php index 18b6dfd..db3d649 100644 --- a/srcs/plugins/map_prof/map_prof_hooks.php +++ b/srcs/plugins/map_prof/map_prof_hooks.php @@ -95,6 +95,7 @@ function mp_add_div() { wp_enqueue_style( 'mp_style', plugins_url('styles/mp_style.css', __FILE__), '', '', false); wp_enqueue_script('mp_info_window', plugins_url('scripts/mp_info_window.js', __FILE__), '', '', false); wp_enqueue_script('mp_create_markers', plugins_url('scripts/mp_create_markers.js', __FILE__), '', '', false); + wp_enqueue_script('mp_draw_clusters', plugins_url('scripts/mp_draw_clusters.js', __FILE__), '', '', false); wp_enqueue_script('mp_marker_clusterer', $marker_clusterer, '', '', true); wp_enqueue_script('mp_init_map', plugins_url('scripts/mp_init_map.js', __FILE__), ['mp_marker_clusterer'],'', true); wp_enqueue_script('mp_google_api', mp_url_api(), ['mp_init_map'], '', true); diff --git a/srcs/plugins/map_prof/scripts/mp_draw_clusters.js b/srcs/plugins/map_prof/scripts/mp_draw_clusters.js new file mode 100644 index 0000000..5635b64 --- /dev/null +++ b/srcs/plugins/map_prof/scripts/mp_draw_clusters.js @@ -0,0 +1,49 @@ +function draw_clusters(map, markers) { + + /* + * following variable are created by mp_add_to_script.php + * - let icon_color = "" + * - let icon_color_back = "" + * - let icon_size = [x, y] + * - let cluster_size_factor = Number + */ + + let renderer = { + render({ count, position }, stats) { + + /* CLUSTERS SETTINGS */ + + let cluster_icon_size = [ + icon_size[0] + ( cluster_size_factor * (count - 2) ), + icon_size[1] + ( cluster_size_factor * (count - 2) ) + ]; + let cluster_svg = window.btoa(` + + + + `); + let cluster_icon = { + url: `data:image/svg+xml;base64,${cluster_svg}`, + scaledSize: new google.maps.Size(cluster_icon_size[0], cluster_icon_size[1]), + }; + let cluster_label = { + text: String(count), + color: "#ba197a", + fontSize: "12px", + fontWeight: "bold", + }; + let cluster_title = `Cluster of ${count} markers`; + let cluster_zIndex = Number(google.maps.Marker.MAX_ZINDEX) + count; + + return new google.maps.Marker({ + position, + icon: cluster_icon, + label: cluster_label, + title: cluster_title, + zIndex: cluster_zIndex, + }); + } + } + + new markerClusterer.MarkerClusterer({ map, markers, renderer }); +} diff --git a/srcs/plugins/map_prof/scripts/mp_info_window.js b/srcs/plugins/map_prof/scripts/mp_info_window.js index e6972fb..7c4d8ee 100644 --- a/srcs/plugins/map_prof/scripts/mp_info_window.js +++ b/srcs/plugins/map_prof/scripts/mp_info_window.js @@ -6,8 +6,15 @@ function attach_info_window(map, marker, events, infowindow) { */ marker.addListener('click', () => { + infowindow.setOptions({ + disableAutoPan: true, + //position: center_position, + //pixelOffset: { width: 50, height: 50 }, + //shouldFocus: false, + }); + infowindow.setPosition(map.getCenter()); infowindow.setContent("test"); - infowindow.open(map, marker); + infowindow.open(map); }); } diff --git a/srcs/plugins/map_prof/scripts/mp_init_map.js b/srcs/plugins/map_prof/scripts/mp_init_map.js index 92d1e3e..af7b42e 100644 --- a/srcs/plugins/map_prof/scripts/mp_init_map.js +++ b/srcs/plugins/map_prof/scripts/mp_init_map.js @@ -16,10 +16,6 @@ function mp_init_map() { * - let cluster_size_factor = Number */ - /* - * SETTINGS - */ - console.log("locations:"); console.log(locations); @@ -42,8 +38,6 @@ function mp_init_map() { let map_div = document.getElementById("ljdp_map"); let infowindow = new google.maps.InfoWindow(); - - /* * DRAW MAP */ @@ -51,46 +45,10 @@ function mp_init_map() { let map = new google.maps.Map(map_div, map_options); let markers = create_markers(map, locations, infowindow); - /* - * DRAW CLUSTERS - */ + // add listener to close infowindow at any click on map + map.addListener('click', function() { + infowindow.close(); + }); - let renderer = { - render({ count, position }, stats) { - - /* CLUSTERS SETTINGS */ - - let cluster_icon_size = [ - icon_size[0] + ( cluster_size_factor * (count - 2) ), - icon_size[1] + ( cluster_size_factor * (count - 2) ) - ]; - let cluster_svg = window.btoa(` - - - - `); - let cluster_icon = { - url: `data:image/svg+xml;base64,${cluster_svg}`, - scaledSize: new google.maps.Size(cluster_icon_size[0], cluster_icon_size[1]), - }; - let cluster_label = { - text: String(count), - color: "#ba197a", - fontSize: "12px", - fontWeight: "bold", - }; - let cluster_title = `Cluster of ${count} markers`; - let cluster_zIndex = Number(google.maps.Marker.MAX_ZINDEX) + count; - - return new google.maps.Marker({ - position, - icon: cluster_icon, - label: cluster_label, - title: cluster_title, - zIndex: cluster_zIndex, - }); - } - } - - new markerClusterer.MarkerClusterer({ map, markers, renderer }); + draw_clusters(map, markers); }