【问题标题】:C# Getting lat and long from Google GeoCoding APIC# 从 Google GeoCoding API 获取经纬度
【发布时间】:2018-12-04 09:59:56
【问题描述】:

在我的 C# 项目中,我试图获取建筑物的经纬度位置。我将此线程用作Distance between addresses 的基础,并尝试对其进行调整以获取位置的经纬度。目前,从 API 获取 JSON 响应很好,它正在从 JSON 响应中获取我想要的值。

这是我目前的代码

string requesturl = url;
string content = fileGetContents(requesturl);
JObject o = JObject.Parse(content);
user.Latitude = (double)o.SelectToken("results[0].geometry[0].location[0].lat");
user.Longitude = (double)o.SelectToken("results[0].geometry[0].location[0].lng");

protected string fileGetContents(string fileName)
    {
        string sContents = string.Empty;
        string me = string.Empty;
        try
        {
            if (fileName.ToLower().IndexOf("https:") > -1)
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                byte[] response = wc.DownloadData(fileName);
                sContents = System.Text.Encoding.ASCII.GetString(response);

            }
            else
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
                sContents = sr.ReadToEnd();
                sr.Close();
            }
        }
        catch { sContents = "unable to connect to server "; }
        return sContents;
    }

【问题讨论】:

  • 请更新您的问题以包含content 的值。

标签: c# json google-maps google-geocoding-api


【解决方案1】:

看起来问题在于您正在尝试访问几何和位置,就好像它们是列表一样。试试这个:

user.Latitude = (double)o.SelectToken("results[0].geometry.location.lat");
user.Longitude = (double)o.SelectToken("results[0].geometry.location.lng");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多