diff options
| author | Indoteknik . <it@fixcomart.co.id> | 2025-06-17 08:47:53 +0700 |
|---|---|---|
| committer | Indoteknik . <it@fixcomart.co.id> | 2025-06-17 08:47:53 +0700 |
| commit | a80565682063e718fc55c90e4243b9d5b2432285 (patch) | |
| tree | bd3b65cc54b62a8994bafb5d38c25631888d6414 | |
| parent | a8460239603b7a73a185fec394b0f95ab0247207 (diff) | |
(andri) fix load gmaps pada view & edit
| -rw-r--r-- | ab_openstreetmap/static/src/js/googlemap_widget.js | 31 | ||||
| -rw-r--r-- | indoteknik_custom/models/res_partner.py | 14 | ||||
| -rw-r--r-- | indoteknik_custom/views/res_partner.xml | 4 |
3 files changed, 25 insertions, 24 deletions
diff --git a/ab_openstreetmap/static/src/js/googlemap_widget.js b/ab_openstreetmap/static/src/js/googlemap_widget.js index 1728fa54..6471be0b 100644 --- a/ab_openstreetmap/static/src/js/googlemap_widget.js +++ b/ab_openstreetmap/static/src/js/googlemap_widget.js @@ -13,30 +13,23 @@ odoo.define("ab_openstreetmap.googlemap_widget", function (require) { this._waitForMapReady(); }, + on_attach_callback: function () { + this._waitForMapReady(); // Trigger ulang saat widget dimunculkan + }, + _waitForMapReady: function () { const mapEl = document.getElementById("mapid"); if (mapEl && mapEl.offsetWidth > 0 && mapEl.offsetHeight > 0) { this._loadGoogle(); } else { - setTimeout(() => this._waitForMapReady(), 100); + setTimeout(() => this._waitForMapReady(), 300); // Tambah jeda untuk mode edit/tab } }, async _loadGoogle() { - // const apiKey = await rpc.query({ - // model: "ir.config_parameter", - // method: "get_param", - // args: ["google.maps.api_key"], - // }); - // const mapId = await rpc.query({ - // model: "ir.config_parameter", - // method: "get_param", - // args: ["google.maps.map_id"], - // }); - - const apiKey = "AIzaSyB7bG9aSNAJnSrj0Z7f1abFsqKVoiJfsPE"; // Ganti dengan API Key Anda - const mapId = "1af072c8d80a2adec8057f34"; - // Ganti dengan Map ID Anda + const apiKey = "AIzaSyB7bG9aSNAJnSrj0Z7f1abFsqKVoiJfsPE"; // Ganti sesuai kebutuhan + const mapId = "1af072c8d80a2adec8057f34"; // Ganti sesuai kebutuhan + if (!window.google || !window.google.maps) { const script = document.createElement("script"); script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&v=weekly&libraries=places,marker`; @@ -57,6 +50,7 @@ odoo.define("ab_openstreetmap.googlemap_widget", function (require) { if (!mapEl) return; mapEl.style.position = "relative"; + mapEl.style.minHeight = "400px"; // Pastikan map tidak collapse const { Map } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); @@ -67,6 +61,12 @@ odoo.define("ab_openstreetmap.googlemap_widget", function (require) { mapId: mapId || undefined, }); + // Trigger resize untuk menangani kasus map awalnya hidden + setTimeout(() => { + google.maps.event.trigger(map, "resize"); + map.setCenter({ lat, lng }); // Reset ulang posisi setelah resize + }, 300); + const marker = new AdvancedMarkerElement({ map, position: { lat, lng }, @@ -100,7 +100,6 @@ odoo.define("ab_openstreetmap.googlemap_widget", function (require) { `; mapEl.appendChild(input); - // Gunakan Autocomplete klasik (deprecated tapi stabil) const autocomplete = new google.maps.places.Autocomplete(input); autocomplete.addListener("place_changed", () => { const place = autocomplete.getPlace(); diff --git a/indoteknik_custom/models/res_partner.py b/indoteknik_custom/models/res_partner.py index a8ce95d1..9986b9c0 100644 --- a/indoteknik_custom/models/res_partner.py +++ b/indoteknik_custom/models/res_partner.py @@ -579,9 +579,10 @@ class ResPartner(models.Model): ]) # Ambil API Key - api_key = self.env['ir.config_parameter'].sudo().get_param('google.maps.api_key') - if not api_key: - raise UserError("API Key Google Maps belum dikonfigurasi. Silakan isi melalui Settings.") + api_key = "AIzaSyB7bG9aSNAJnSrj0Z7f1abFsqKVoiJfsPE" + # api_key = self.env['ir.config_parameter'].sudo().get_param('google.maps.api_key') + # if not api_key: + # raise UserError("API Key Google Maps belum dikonfigurasi. Silakan isi melalui Settings.") # Request ke Google Maps url = f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={api_key}' @@ -638,9 +639,10 @@ class ResPartner(models.Model): def _reverse_geocode(self, lat, lng): - api_key = self.env['ir.config_parameter'].sudo().get_param('google.maps.api_key') - if not api_key: - raise UserError("API Key Google Maps belum dikonfigurasi.") + api_key = "AIzaSyB7bG9aSNAJnSrj0Z7f1abFsqKVoiJfsPE" + # api_key = self.env['ir.config_parameter'].sudo().get_param('google.maps.api_key') + # if not api_key: + # raise UserError("API Key Google Maps belum dikonfigurasi.") url = f'https://maps.googleapis.com/maps/api/geocode/json?latlng={lat},{lng}&key={api_key}' response = requests.get(url) diff --git a/indoteknik_custom/views/res_partner.xml b/indoteknik_custom/views/res_partner.xml index 2a4b03a7..30b5ca36 100644 --- a/indoteknik_custom/views/res_partner.xml +++ b/indoteknik_custom/views/res_partner.xml @@ -67,9 +67,9 @@ </group> <xpath expr="//notebook/page[@name='contact_addresses']" position="before"> <page string="Pin Point Location" name="map_location"> - <!-- <group> + <group> <button name="geocode_address" type="object" string="Get Pin Point Location" class="btn btn-primary"/> - </group> --> + </group> <div style="margin: 16px 0;"> <field name="map_view" widget="googlemap" nolabel="1"/> </div> |
