Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- def get_coordinates(address):
- url = "https://nominatim.openstreetmap.org/search"
- params = {
- 'q': address,
- 'format': 'json',
- 'limit': 1
- }
- headers = {
- 'User-Agent': 'GeocoderPythonApp' # Required by OpenStreetMap API
- }
- try:
- response = requests.get(url, params=params, headers=headers)
- response.raise_for_status()
- data = response.json()
- if data:
- lat = data[0]['lat']
- lon = data[0]['lon']
- print(f"📍 Address: {address}")
- print(f"🧭 Latitude: {lat}, Longitude: {lon}")
- else:
- print("❌ No coordinates found for the given address.")
- except requests.exceptions.RequestException as e:
- print(f"❌ Error fetching coordinates: {e}")
- # Example usage
- address = input("Enter an address: ")
- get_coordinates(address)
Advertisement
Add Comment
Please, Sign In to add comment