Reputation: 181
How do I convert a Google maps link to GPS coordinates? (I think
10°11'12.0"N 13°14'15.0"E
is an example of a common GPS coordinate format.)
The reason I ask is that Google Maps for Android (I use Samsung Galaxy Note 3) does not seem to give coordinates. It will only give Google links. (Any instructions with right click can not be followed in a phone, only in a computer.)
For example. How do I convert the following link to find the coordinates of the Eiffel Tower:
I think there have been earlier standards by Google where the hyperlink arguments contained the coordinates. But the current standard is more cryptic.
Right now I want to do it manually in my Android (Samsung Galaxy Note 3) phone. But maybe the question is of interest for programmatic conversion too.
Edit:
This question is NOT about the conversion between decimal and DMS (degrees, minutes, seconds) formats for coordinates. Many web pages and code-snippets are available for that.
Upvotes: 3
Views: 17032
Reputation: 1912
I wrote this test class to get coordinates from a Google Maps share link in C# (.NET 8):
public class Test : IDisposable
{
private readonly HttpClient _httpClient = new();
public void Dispose()
{
_httpClient?.Dispose();
GC.SuppressFinalize(this);
}
public async Task<string?> TryGetCoordinatesFromGoogleMapsShareLinkAsync(string shareLink, CancellationToken cancellationToken = default)
{
using var response = await _httpClient.GetAsync(shareLink, cancellationToken);
var expandedUrl = response.RequestMessage?.RequestUri?.ToString();
if (!string.IsNullOrEmpty(expandedUrl))
{
return ExtractCoordinatesFromExpandedGoogleMapsUrl(expandedUrl);
}
return null;
}
private static string? ExtractCoordinatesFromExpandedGoogleMapsUrl(string url)
{
var atIndex = url.IndexOf('@');
string? coordinates;
if (atIndex != -1)
{
var coordinatesPart = url[(atIndex + 1)..];
var parts = coordinatesPart.Split(',');
if (parts.Length >= 2)
{
coordinates = $"{parts[0]},{parts[1]}";
return coordinates;
}
}
else
{
var parts = url.Split('/')[5].Split(',');
if (parts.Length >= 2)
{
coordinates = $"{parts[0]},{parts[1]}";
return coordinates;
}
}
return null;
}
}
Upvotes: 1
Reputation: 1381
It's an old question and I figured the accepted solution doesn't work with the Google map I just got. Here's a version that does work at this time:
import re
def extract_lat_long_from_url(url):
"""
Extract latitude and longitude from a Google Maps URL.
:param url: Google Maps URL as a string.
:return: A tuple containing latitude and longitude as strings, or (None, None) if not found.
"""
regex = r"@([0-9.-]+),([0-9.-]+)"
match = re.search(regex, url)
if match:
latitude, longitude = match.groups()
return latitude, longitude
else:
return None, None
Example:
long_url = 'https://www.google.com/maps/place/Mount+Sunapee+Resort/@43.314131,-72.087448,14.25z/data=!4m6!3m5!1s0x89e1f3c35c83af93:0x74e50d9525036279!8m2!3d43.3313962!4d-72.0804797!16s%2Fm%2F0g563tg?entry=tts'
lat, long = extract_lat_long_from_url(long_url)
# ('43.314131', '-72.087448')
Upvotes: 0
Reputation: 59
You need to unshorten the url link, and the result will be and url with coordinates embedded in it. In your example:
See this topic on how to unshorten using Python: How can I unshorten a URL?
Then you need to parse it for the coordinates, for example by searching for the @
character. Assume your long url is called longurl
. In Python, you can do
import re
temp = re.search('@([0-9]?[0-9]\.[0-9]*),([0-9]?[0-9]\.[0-9]*)', longurl, re.DOTALL)
latitude = temp.groups()[0]
longitude = temp.groups()[1]
(Then you can further convert it from DD to minutes, seconds, if you need that.)
Upvotes: 1
Reputation: 2372
This link shows you how to convert coordinates from a Google Maps link (which is in DD, aka decimal degrees) into GPS coordinates (DMS - degrees, minutes, seconds). The general idea is to take the DD value and split it up into the degrees, minutes, and seconds values for latitude and longitude using the following process:
1) Take the integer value. This becomes the degrees.
2) Take the remaining decimal value and multiply it by 60. The integer portion of this value is the minutes.
3) Lastly, take the remaining decimal value and multiply it by 60 again. This is the seconds value (and is generally rounded off to 2 decimal points).
Example: 48.8583701, 2.2944813
Latitude: degrees
= 48
0.8583701 * 60 = 51.502206 => minutes
= 51
0.502206 * 60 = 30.13236 => seconds
= 30.13
Latitude (DMS): 48º 51' 30.13" N
The link also has some code if you want to do it programmatically.
Upvotes: -1