Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

How do I add a Google Maps link?

By
Shaun Dawson
on 10/21/13, 2:38 AM 2,619 views

Using OpenERP 7.0, I have a custom module in which I have a number of address fields.

In the form view, I'd like have a button or a link to a google map for that address.

This seems quite straightforward, but I can't for the life of my figure out how to do it. I've taken a look at the google_maps module, but it doesn't seem to compile and/or import properly in to OpenERP 7.0, and the techniques that module uses don't seem to work anymore.

Any ideas?

2

Brett Lehrer

--Brett Lehrer--
1252
| 5 4 8
Milwaukee, United States
--Brett Lehrer--
Director of IT and open source enthusiast.
Brett Lehrer
On 10/21/13, 3:10 PM

This code works for me, and this is pretty much taken directly from the 6.1 code from what I recall:

from openerp.osv import osv,fields

class launch_map(osv.osv):
    _inherit = "res.partner"

    def open_map(self, cr, uid, ids, context=None):
        if context is None:
            context = {}

        partner = self.browse(cr, uid, ids[0], context=context)
        url = "http://maps.google.com/maps?oi=map&q="
        if partner.street:
            url += partner.street.replace(' ','+')
        if partner.city:
            url += '+'+partner.city.replace(' ','+')
        if partner.state_id:
            url += '+'+partner.state_id.name.replace(' ','+')
        if partner.country_id:
            url += '+'+partner.country_id.name.replace(' ','+')
        if partner.zip:
            url += '+'+partner.zip.replace(' ','+')

        return {
            'type': 'ir.actions.act_url',
            'url': url,
            'target': 'new'
        }

In your view, just add a button on the partner that calls the open_map function. No more complicated than this:

<openerp>
<data>

<record model="ir.ui.view" id="view_partner_form_googlemaps">
    <field name="name">res.partner.form.googlemaps</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"/>
    <field name="arch" type="xml">
        <xpath expr="//sheet/div[@name='buttons']" position="inside">
            <button name="open_map" string="Google Maps" type="object"/>
        </xpath>
    </field>
</record>

</data>
</openerp>

You can just add in your additional fields wherever they're necessary to fill out the address format in a single line, Google will take care of the rest.

Works perfectly. Thanks so much for sharing. It's so simple, but I didn't know how object buttons worked. But I do now!

Shaun Dawson
on 10/21/13, 4:25 PM

object = python function defined in the object's class. A bit odd to think of it that way, though I understand why. Calling it a "function" there would've probably been more intuitive.

Brett Lehrer
on 10/21/13, 5:19 PM

The other piece of the puzzle that I needed, and didn't find documented anywhere was what the python function in question is supposed to return. In this case, a dictionary describing an ir_actions record, and that there was such a thing as an ir.actions.act_url that could open up a new window pointing to a particular URL. That's immensely helpful.

Shaun Dawson
on 10/21/13, 5:24 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 10/21/13, 2:38 AM
Seen: 2619 times
Last updated: 3/16/15, 8:10 AM