跳至内容
菜单
此问题已终结
1 回复
1827 查看

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)

相关帖文 回复 查看 活动
2
8月 25
2736
1
7月 25
1093
1
8月 25
1151
0
5月 25
1520
2
4月 25
3710