Surya_from_1627

WSP4

Sep 9th, 2026
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. import requests
  2.  
  3. def get_coordinates(address):
  4.     url = "https://nominatim.openstreetmap.org/search"
  5.     params = {
  6.         'q': address,
  7.         'format': 'json',
  8.         'limit': 1
  9.     }
  10.     headers = {
  11.         'User-Agent': 'GeocoderPythonApp'  # Required by OpenStreetMap API
  12.     }
  13.  
  14.     try:
  15.         response = requests.get(url, params=params, headers=headers)
  16.         response.raise_for_status()
  17.         data = response.json()
  18.  
  19.         if data:
  20.             lat = data[0]['lat']
  21.             lon = data[0]['lon']
  22.             print(f"📍 Address: {address}")
  23.             print(f"🧭 Latitude: {lat}, Longitude: {lon}")
  24.         else:
  25.             print("❌ No coordinates found for the given address.")
  26.     except requests.exceptions.RequestException as e:
  27.         print(f"❌ Error fetching coordinates: {e}")
  28.  
  29. # Example usage
  30. address = input("Enter an address: ")
  31. get_coordinates(address)
  32.  
Advertisement
Add Comment
Please, Sign In to add comment