Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Google maps link to coordinates

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:

http://goo.gl/maps/VqEsU

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.

like image 525
cvr Avatar asked Jul 10 '26 08:07

cvr


2 Answers

You need to unshorten the url link, and the result will be and url with coordinates embedded in it. In your example:

https://www.google.com/maps/place/Eiffel+Tower/@48.8583701,2.2922926,17z/data=!3m1!4b1!4m2!3m1!1s0x0:0x8ddca9ee380ef7e0?hl=en

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.)

like image 192
GEK Avatar answered Jul 14 '26 02:07

GEK


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;
    }
}
like image 22
Marco Concas Avatar answered Jul 14 '26 01:07

Marco Concas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!