Skip to Content
Menu
This question has been flagged
1 Reply
1325 Views

For Example This Url: https://goo.gl/maps/5URZH1fAZ6S6Ew2k9
How Can We Extract Latitude And Longitude ?


Avatar
Discard
Best Answer

You can use OpenStreetMap to retrieve the latitude/longitude for an address or city. For example:

def get_coordinates(self, address):
# Define the API endpoint and parameters for the API request
endpoint = 'https://nominatim.openstreetmap.org/search'
params = {
'q': address,
'format': 'json',
'addressdetails': 1,
'limit': 1,
​ # (Optional) country codes separated by a comma (e.g., 'de,gb')
'countrycodes': 'gb'
}
# Send the API request and get the response
response = requests.get(endpoint, params=params)
data = response.json()
if data:
return (data[0]['lat'], data[0]['lon'])
else:
return False

If you want to parse the url you can use something like below which returns the full url:

import requests

def get_full_url(self, short_url):
try:
# Make a request to the short URL with allow_redirects=False
# to prevent the requests library from following the redirect
response = requests.head(short_url, allow_redirects=False)
if response.status_code in [301, 302]:
return response.headers['Location']
else:
return None
except Exception as e:
return None

I hope this helps!

Avatar
Discard
Author

Thank you, But the question was how to retrieve latitude/longitude for a shared URL (https://goo.gl/maps/5URZH1fAZ6S6Ew2k9) not for an address

Author

let's say i have a field "location_url", in this field I paste the url of a place that I have received from someone, and onchange this field i update the longitude and latitude fields.

In that case I misunderstood. You can use the code in the updated answer. You only have to parse the longitude and latitude from the url, e.g., re.search(r'@(-?\d+\.\d+),(-?\d+\.\d+)', url)

Related Posts Replies Views Activity
0
May 25
281
2
Apr 25
2461
1
Feb 25
809
2
Feb 25
1312
2
Jan 25
1439