1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
odoo.define("ab_openstreetmap.googlemap_widget", function (require) {
"use strict";
const AbstractField = require("web.AbstractField");
const fieldRegistry = require("web.field_registry");
const rpc = require("web.rpc");
const GoogleMapWidget = AbstractField.extend({
template: "googlemap_template",
start: async function () {
await this._super(...arguments);
this._waitForMapReady();
},
_waitForMapReady: function () {
const mapEl = document.getElementById("mapid");
if (mapEl && mapEl.offsetWidth > 0 && mapEl.offsetHeight > 0) {
this._loadGoogle();
} else {
setTimeout(() => this._waitForMapReady(), 100);
}
},
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 = "AIzaSyD1b2e4g3f8a9c5d6e7f8g9h0i1j2k3l4m"; // Ganti dengan API Key Anda
const mapId = "1af072c8d80a2adec8057f34";
// Ganti dengan Map ID Anda
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`;
script.async = true;
script.defer = true;
script.onload = () => this._initMap(mapId);
document.head.appendChild(script);
} else {
this._initMap(mapId);
}
},
async _initMap(mapId) {
const lat = parseFloat(this.recordData.latitude) || -6.2;
const lng = parseFloat(this.recordData.longtitude) || 106.816666;
const edit = this.mode === "edit";
const mapEl = document.getElementById("mapid");
if (!mapEl) return;
mapEl.style.position = "relative";
const { Map } = await google.maps.importLibrary("maps");
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker");
const map = new Map(mapEl, {
center: { lat, lng },
zoom: 15,
mapId: mapId || undefined,
});
const marker = new AdvancedMarkerElement({
map,
position: { lat, lng },
gmpDraggable: edit,
title: "Lokasi",
});
if (edit) {
marker.addListener("dragend", () => {
const pos = marker.position;
this._updateCoordinates(pos.lat, pos.lng);
});
// Tambahkan input search
const input = document.createElement("input");
input.type = "text";
input.placeholder = "Cari alamat...";
input.id = "search-input";
input.style.cssText = `
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
z-index: 5;
width: 300px;
padding: 6px;
font-size: 14px;
border-radius: 4px;
border: 1px solid #ccc;
background: white;
`;
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();
if (place && place.geometry && place.geometry.location) {
const pos = place.geometry.location;
map.setCenter(pos);
marker.position = pos;
this._updateCoordinates(pos.lat(), pos.lng());
}
});
}
},
_updateCoordinates(lat, lng) {
this.trigger_up("field_changed", {
dataPointID: this.dataPointID,
changes: {
latitude: lat.toString(),
longtitude: lng.toString(),
},
viewType: this.viewType,
});
},
isSet() {
return true;
},
});
fieldRegistry.add("googlemap", GoogleMapWidget);
});
|