Saturday, 14 June 2014

Show your Customers on google map in openerp ( Odoo Google Map Integration)

In this blog , I will show you ,how to show markers on google map for your Customers with their Details
I wanted to show filtered customers only . Example all customers for a particular City


1.Install Python Packages-
  Install Geocoder
   pip install pygeocoder

  Install Pygmaps ( Please install my customised Package (ask me at - sonuch9551241@gmail.com))
  Install apache-
      pip install apache2
  Restart apache-
      sudo /etc/init.d/apache2 restart
  Create custom dir-
      I created "/home/openerp/html"
  Now change in config file for apache (or create your own custom config file and activate that)
    sudo nano /etc/apache2/sites-available/default
        change DocumentRoot "/var/www" to  "/home/openerp/html"
 
  import urllib
  import pygmaps
  from pygeocoder import Geocoder

2. Now suppose you want to show all cutomers for city "San Diago"
   search customer for city 'San Diago'
       customer_ids = self.pool.get('res.partner').search(cr ,uid , [('city','ilike','San Diago')])


3. Create instance for Google Map
   results = Geocoder.geocode('San Diago')
   cord = results1[0].coordinates
   if len(cord) > 1 and cord[0] and cord[1]:
            lat = cord[0]
            lon = cord[1]
   mymap = pygmaps.maps(lat, lon, 7) # 7 is the zoom level
   # mymap is the google map instance

4. Now to create markers for customers , use loop
   for customer_id in customer_ids:
    customer = self.pool.get('res.partner').browse(cr ,uid ,customer_id)
    results = Geocoder.geocode(customer.street + ',' + customer.city + ',' + customer.zip)
        #get coordinates from it
        cord = results[0].coordinates
    if len(cord) > 1 and cord[0] and cord[1]:
        lat = cord[0]   
        lon = cord[1]
   
    mymap.addpoint(lat, lon, "#0000FF" , customer.name , customer.email + ' , ' + customer.phone)
       #to show markers on google map for customer
       #lat - latitude
       #long - longitude
       #color - specify color of marker
       #name - specify Customer Name to be shown in Bold
       #description (details) - Details to be shown other than name

5. Generate html file
   mymap.draw('/home/openerp/html/customer.html')

6. Now show this to Customer -
   return {
            'type': 'ir.actions.act_url',
            'url':'http://ip_domain/customer.html' , #ip_domain is your systems ip
            'nodestroy': True,
            'target': 'new'
            }

  Now Google Map is opened in new tab with your customers. Click on marker to see the Details, or hover your mouse on marker to see name only




Thanks
Sonu Chaudhary


4 comments:

  1. Hi Sonu, did you already create a module that does it? I'm looking for one that allowed me to see a list of customers on the map (i.e. selecting them from the tree view). Thanks in advance!

    ReplyDelete
  2. Hi Sonu Chaudhary, Can you please help me in getting values of longitude and latitude form a Csv file and display the map inside openerp module. thanks

    ReplyDelete
  3. Hi Sonu. Is your code available on Github or some other public repository?

    ReplyDelete
  4. Hi Sonu. Are your code modifications available on Github or some other public repository?

    ReplyDelete