How do I show Map in my form XML, i should be able to use Openstreetmap right?
So I make static js and html inside my module to show camera and this is my xml, but somehow it’s FORBIDDEN.
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<form>
<label name="company"/>
<field name="company"/>
<newline/>
<label name="address"/>
<field name="address"/>
<newline/>
<label name ="longitude"/>
<field name="longitude"/>
<label name ="latitude"/>
<field name="latitude"/>
<newline/>
<label name="html_map"/>
<field name="html_map" widget="html" readonly="1"/>
</form>
And this is the class from html_map
in geolocation.py
class CompanyGeo(ModelSingleton, ModelSQL, ModelView):
'Company Geo'
__name__ = 'company.geo'
company = fields.Many2One('company.company', "Company", required=True,
help="The company the geolocation is associated with.")
address = fields.Char('Address')
longitude = fields.Float('Longitude', readonly =True )
latitude = fields.Float('Latitude', readonly =True)
max_distance = fields.Float('Max Distance (meters)', required=True,
help="Maximum allowed distance in meters for face recognition.")
html_map = fields.Function(fields.Char('Map'), 'get_html_map')
@fields.depends('address')
def on_change_with_coordinates(self):
if not self.address:
self.longitude = None
self.latitude = None
return
geolocator = Nominatim(user_agent="attendanceapp")
try:
location = geolocator.geocode(self.address)
if location:
self.longitude = location.longitude
self.latitude = location.latitude
else:
self.longitude = None
self.latitude = None
except GeocoderTimedOut:
print("Geocoding service timed out. Try again later.")
self.longitude = None
self.latitude = None
except Exception as e:
print(f"An error occurred: {e}")
self.longitude = None
self.latitude = None
def get_html_map(self, name):
return '''
<div id="map" style="height: 400px;"></div>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<script>
var map = L.map('map').setView([%(latitude)s, %(longitude)s], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
var marker = L.marker([%(latitude)s, %(longitude)s]).addTo(map)
.bindPopup('%(address)s')
.openPopup();
</script>
''' % {'latitude': self.latitude or 0, 'longitude': self.longitude or 0, 'address': self.address or ''}