Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
1336 Переглядів

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


Аватар
Відмінити
Найкраща відповідь

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!

Аватар
Відмінити
Автор

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

Автор

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 Відповіді Переглядів Дія
0
трав. 25
335
2
квіт. 25
2506
1
лют. 25
833
2
лют. 25
1341
2
січ. 25
1480