odoo原生内置google maps,导致中国大部分区域不仅无法正常使用,用户浏览器一直加载到超时还拖慢了网站整体
相关源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class res_partner(osv.osv): _inherit = "res.partner" def google_map_img(self, cr, uid, ids, zoom=8, width=298, height=298, context=None): partner = self.browse(cr, uid, ids[0], context=context) params = { 'center': '%s, %s %s, %s' % (partner.street or '', partner.city or '', partner.zip or '', partner.country_id and partner.country_id.name_get()[0][1] or ''), 'size': "%sx%s" % (height, width), 'zoom': zoom, 'sensor': 'false', } return urlplus('//maps.googleapis.com/maps/api/staticmap' , params) def google_map_link(self, cr, uid, ids, zoom=10, context=None): partner = self.browse(cr, uid, ids[0], context=context) params = { 'q': '%s, %s %s, %s' % (partner.street or '', partner.city or '', partner.zip or '', partner.country_id and partner.country_id.name_get()[0][1] or ''), 'z': zoom, } return urlplus('https://maps.google.com/maps' , params) |
可见针对联系人扩展了静态地图和外链地图两个接口,其参数都是通过模块自身的国家、邮编、城市、街道等地址相关字段构成
了解上诉原理之后,参照百度地图开放平台API,梳理出相近接口的参数异同点后,完全可以改造成具有中国特色的odoo maps
核心代码:
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 |
class ResPartner(models.Model): _inherit = "res.partner" def baidu_map_img(self, zoom=15, width=298, height=298): country_name = self.country_id and self.country_id.name or '' state_name = self.state_id and self.state_id.name or '' city_name = self.city or '' street_name = self.street or '' street2_name = self.street2 or '' params = { 'markers': '%s' % street2_name, 'center': '%s%s%s%s' % (country_name, state_name, city_name, street_name), 'height': "%s" % height, 'width': "%s" % width, 'zoom': zoom, 'copyright': 1, } #http://lbsyun.baidu.com/index.php?title=static return urlplus('//api.map.baidu.com/staticimage' , params) def baidu_map_link(self): partner_name = self.name city_name = self.city or '' street2_name = self.street2 or '' params = { 'address': '%s,%s,%s' % (city_name, street2_name, partner_name), 'output': 'html', 'src': 'odoo', } #http://lbsyun.baidu.com/index.php?title=uri return urlplus('//api.map.baidu.com/geocoder' , params) |
最后顺便将其整合到odoo开源模块Website China Features里,有需要可直接从官方应用市场下载使用
https://apps.odoo.com/apps/modules/9.0/website_china_features/