How to get Latitude and Longitude of address in Odoo ERP using Google Api in Python

In this blog we will see how we can  latitude and longitude of an address.
You may require to get latitude and longitude between of an address in Odoo 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     or you can download from python packages site Geocoder.

 2.  Now import Geocoder class from pygeocoder in your py file
        from pygeocoder import Geocoder
        import urllib
             import json
           
3.  Now you use get_latitude_longitude function given below. 
    You will have to 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

Comments

Popular posts from this blog

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