{"id":26612,"date":"2022-04-02T20:15:41","date_gmt":"2022-04-02T14:45:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=26612"},"modified":"2022-04-02T20:15:41","modified_gmt":"2022-04-02T14:45:41","slug":"how-to-get-geolocation-in-python-using-geopy","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-get-geolocation-in-python-using-geopy\/","title":{"rendered":"How to Get Geolocation in Python using GeoPy?"},"content":{"rendered":"
What is Geolocation?<\/strong><\/p>\n Geolocation refers to a physical location. It identifies and tracks the location of linked electronic devices using various location technologies such as GPS and IP addresses.<\/p>\n Python’s GeoPy module is used for this purpose. When you enter a location name, it returns all info such as the postal code, city, state, and nation, as well as latitudes and longitudes. When we enter the coordinates, it returns the name of the location.<\/p>\n GeoPy is not a built-in Python library. It must be explicitly installed.<\/p>\n Installation:<\/strong><\/p>\n Installation in Anaconda:<\/strong><\/p>\n Every geolocation service, such as Google Maps or Nominatim, has its own class in geopy.geocoders that are used to abstract the API of the service. We’ll be using Nominatim, a free API service tool, in this case.<\/p>\n Geocodes of some places in India:<\/strong><\/p>\n Place\u00a0<\/strong> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Latitude\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Longitude<\/strong><\/p>\n Telangana, India\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a017.123184\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a079.208824<\/p>\n Kerala, India\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a010.850516\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a076.271080<\/p>\n Tamil Nadu, India\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a011.127123\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a078.656891<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":" What is Geolocation? Geolocation refers to a physical location. It identifies and tracks the location of linked electronic devices using various location technologies such as GPS and IP addresses. Python’s GeoPy module is used for this purpose. When you enter a location name, it returns all info such as the postal code, city, state, and …<\/p>\npip\u00a0install\u00a0geopy<\/pre>\n
conda install geopy<\/pre>\n
Getting Geolocation in Python using GeoPy<\/h2>\n
\n
1)Getting <\/strong>information about the specified location using Location name<\/strong><\/h3>\n<\/div>\n
\n
# Import Nominatim from geocoders of geopy module using the import keyword \r\nfrom geopy.geocoders import Nominatim\r\n# Create an object for Nominatim() class by passing some random user_agent as an argument to it.\r\n# Here user_agent is the app name to which it is provides services.\r\nlocation = Nominatim(user_agent= \"GetLocationdetails\")\r\n\r\n# Pass some random location to the geocode() function and apply it on the above location(Nominatim)\r\n# Here it gives geopy.location.Location object which contains the address and the coordinate\r\nLocationinfo = location.geocode(\"Mumbai Maharastra\")\r\n# Get the address of the above given location using the address attribute\r\nprint(\"Address = \", Locationinfo.address)\r\n# Get the latitude of the above given location using the latitude attribute\r\nprint(\"Latitude = \", Locationinfo.latitude)\r\n# Get the longitude of the above given location using the longitude attribute\r\nprint(\"Longitude = \", Locationinfo.longitude)\r\n# Get the altitude of the above given location using the altitude attribute\r\nprint(\"Altitude = \", Locationinfo.altitude)<\/pre>\n
Address = Mumbai, Mumbai Metropolitan Region, Mumbai Suburban, Maharashtra, India\r\nLatitude = 19.0759899\r\nLongitude = 72.8773928\r\nAltitude = 0.0<\/pre>\n
2)Getting <\/strong>information about the specified location using Latitudes, Longitudes<\/strong><\/h3>\n
\n
# Import Nominatim from geocoders of geopy module using the import keyword \r\nfrom geopy.geocoders import Nominatim\r\n# Create an object for Nominatim() class by passing some random user_agent as an argument to it.\r\n# Here user_agent is the app name to which it is provides services.\r\nlocation = Nominatim(user_agent= \"GetLocationdetails\")\r\n\r\n# Pass latitudes and longitudes coordinates as arguments to the reverse() function to get the\r\n# location name and store it in a variable.\r\nloc_name = location.reverse(\"19.0759899, 72.8773928\")\r\n# Get the address of the above given location using the address attribute\r\nprint(\"The Address of the from the given latitudes and longitudes: \")\r\nprint(loc_name.address)<\/pre>\n
The Address of the from the given latitudes and longitudes: \r\nSanta Cruz \u2013 Chembur Link Road, Hallow Pul, L Ward, Zone 5, Mumbai, Mumbai Metropolitan Region, Mumbai Suburban, Maharashtra, 400070, India<\/pre>\n