簡體   English   中英

使用 C# winform 應用程序從谷歌地圖 URL 中提取緯度和經度

[英]Extract latitude and longitude from Google Maps URL using C# winform Application

如何以編程方式使用 C# winform 應用程序從谷歌地圖 URL 中提取緯度和經度?

您可以使用以下代碼。 您可以將 urlGoogleMaps(字符串)更改為您喜歡的任何其他 URL。 這個例子是美國紐約自由女神像的位置。

    public void YourCode()
    {
        string urlGoogleMaps = "https://www.google.com/maps/place/New+York,+NY,+USA/@40.6893423,-74.0446546,18.75z/data=!4m5!3m4!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!8m2!3d40.7127753!4d-74.0059728";
        Coordination LibertyStatue = new Coordination(urlGoogleMaps);

        Console.WriteLine("Statue of Liberty:");
        Console.WriteLine("Latitude: " + LibertyStatue.Latitude);
        Console.WriteLine("Longitude: " + LibertyStatue.Longitude);
        Console.WriteLine("Height: " + LibertyStatue.Height);
    }

    public class Coordination
    {
        public float Latitude;
        public float Longitude;
        public float Height;

        public Coordination(string urlGoogleMaps)
        {
            string rawCoords = urlGoogleMaps.Split('/').Where(c => c.StartsWith("@") && c.EndsWith("z")).FirstOrDefault();
            Latitude = float.Parse(rawCoords.Split(',')[0].TrimStart('@'));
            Longitude = float.Parse(rawCoords.Split(',')[1]);
            Height = float.Parse(rawCoords.Split(',')[2].TrimEnd('z'));
        }
    }

筆記; 當 url 不適合滿足這些方法時,您可能需要添加一些預防措施。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM