Tuesday, 23 September 2014

Show path between two Address on google map in Odoo ( formerly openerp )

To show path, distance and duration between source and destination address on google map , just use this simple function in Odoo. An new window will pop up in browser, showing path between source address and destination address .

def show_path_on_google_map(self, cr, uid, ids, context=None):
    ''’ Function to show path between Source and Destination Address on Google map '''
    url="http://maps.google.com/maps?saddr="
    # Demo Addresses
    source_address = ' Mumbai, Maharashtra '
    destination_address = 'Red Fort Delhi, India ,110006'
       
    url += source_address + '&daddr=' + destination_address
    return {
        'type': 'ir.actions.act_url',
        'url':url,
        'nodestroy': True,
        'target': 'new'
    }




Thanks

Sonu Chaudhary

Monday, 8 September 2014

How to get Latitude and Longitude of address in Odoo ( Openerp ) using Google Api in Python


You may require to get Latitude and Longitude between of an address in Odoo ( Openerp ) for many reasons . Here are steps to follow - 

1.     First install pygeocoder Python Package
          Simply you can install using –
          apt-get install python-pip
          pip install pygeocoder
          or from python packages site.
         
 2.     Now import Geocoder class from pygeocoder in your py file
       from pygeocoder import Geocoder
       import urllib
            import json
           
      Now you use get_latitude_longitude (Global )function given below , pass address as argument and you will get a list of latitude and longitude returned from Google Api – ex [19.1605798,72.8380889]

      def get_latitude_longitude(addr):
           url ='https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='
           url += urllib.quote(addr.encode('utf8'))
           res = []
           try:
                gcoder = Geocoder()
                results = gcoder.geocode(addr)
                res.append(results[0].latitude)
                res.append(results[0].longitude)
           except Exception, e:
                pass

            return res



Thanks
Sonu Chaudhary