diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/website_google_map/static/src/js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website_google_map/static/src/js')
| -rw-r--r-- | addons/website_google_map/static/src/js/website_google_map.js | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/addons/website_google_map/static/src/js/website_google_map.js b/addons/website_google_map/static/src/js/website_google_map.js new file mode 100644 index 00000000..ae935a5e --- /dev/null +++ b/addons/website_google_map/static/src/js/website_google_map.js @@ -0,0 +1,86 @@ +function initialize_map() { + 'use strict'; + + // MAP CONFIG AND LOADING + var map = new google.maps.Map(document.getElementById('odoo-google-map'), { + zoom: 1, + center: {lat: 0.0, lng: 0.0}, + mapTypeId: google.maps.MapTypeId.ROADMAP + }); + + // ENABLE ADDRESS GEOCODING + var Geocoder = new google.maps.Geocoder(); + + // INFO BUBBLES + var infoWindow = new google.maps.InfoWindow(); + var partners = new google.maps.MarkerImage('/website_google_map/static/src/img/partners.png', new google.maps.Size(25, 25)); + var partner_url = document.body.getAttribute('data-partner-url') || ''; + var markers = []; + var options = { + imagePath: '/website_google_map/static/src/lib/images/m' + }; + + google.maps.event.addListener(map, 'click', function() { + infoWindow.close(); + }); + + // Display the bubble once clicked + var onMarkerClick = function() { + var marker = this; + var p = marker.partner; + infoWindow.setContent( + '<div class="marker">'+ + (partner_url.length ? '<a target="_top" href="'+partner_url+p.id+'"><b>'+p.name +'</b></a>' : '<b>'+p.name+'</b>' )+ + (p.type ? ' <b>' + p.type + '</b>' : '')+ + ' <pre>' + p.address + '</pre>'+ + '</div>' + ); + infoWindow.open(map, marker); + }; + + // Create a bubble for a partner + var set_marker = function(partner) { + // If no lat & long, geocode address + // TODO: a server cronjob that will store these coordinates in database instead of resolving them on-the-fly + if (!partner.latitude && !partner.longitude) { + Geocoder.geocode({'address': partner.address}, function(results, status) { + if (status === google.maps.GeocoderStatus.OK) { + var location = results[0].geometry.location; + partner.latitude = location.ob; + partner.longitude = location.pb; + var marker = new google.maps.Marker({ + partner: partner, + map: map, + icon: partners, + position: location + }); + google.maps.event.addListener(marker, 'click', onMarkerClick); + markers.push(marker); + } else { + console.debug('Geocode was not successful for the following reason: ' + status); + } + }); + } else { + var latLng = new google.maps.LatLng(partner.latitude, partner.longitude); + var marker = new google.maps.Marker({ + partner: partner, + icon: partners, + map: map, + position: latLng + }); + google.maps.event.addListener(marker, 'click', onMarkerClick); + markers.push(marker); + } + }; + + // Create the markers and cluster them on the map + if (odoo_partner_data){ /* odoo_partner_data special variable should have been defined in google_map.xml */ + for (var i = 0; i < odoo_partner_data.counter; i++) { + set_marker(odoo_partner_data.partners[i]); + } + var markerCluster = new MarkerClusterer(map, markers, options); + } +} + +// Initialize map once the DOM has been loaded +google.maps.event.addDomListener(window, 'load', initialize_map); |
