简体   繁体   English

C#从Google GeoCoding API获得长期支持

[英]C# Getting lat and long from Google GeoCoding API

In my C# project, I'm trying to get the lat and long location of a building. 在我的C#项目中,我试图获取建筑物的经纬度。 I used this thread as a base Distance between addresses and tried to adapt it to get the lat and long of a location. 我将此线程用作地址之间的基本距离,并尝试对其进行调整以获取位置的经度和纬度。 At the moment, the getting the JSON response from the API is fine, it's getting the value I want from the JSON response. 目前,可以从API中获取JSON响应,也可以从JSON响应中获取所需的值。

Here is my code at the moment 这是我现在的代码

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;
    }

Looks like the problem is that you're trying to access geometry and location as if they were lists. 看起来问题在于您试图像访问列表一样访问几何和位置。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定纬度/经度,从C#中的纬度/经度列表中找到最接近的纬度/经度对 - Given a lat/long, find the nearest lat/long pair from a list of lat/long in c# 谷歌地图api没有给出确切地址的纬度/经度c# - Google Map api doesn't gives lat/long on exact address c# 如何在 C# 中按距离(以英里为单位)对给定纬度/经度的纬度/经度列表进行排序? - How to sort a list of lat/long by distance (in miles) from a given lat/long in C#? 如何计算C#中lat长坐标值的距离 - How to calculate distance from lat long coordinate values in C# 如何从 C# 代码调用 Google 地理编码服务 - How to call Google Geocoding service from C# code 为什么Google Geocoding使用DownloadStringAsync(Uri,Object)C#需要这么长时间 - Why is Google Geocoding taking so long using DownloadStringAsync(Uri, Object) C# 尝试使用Google地理编码API时出现异常 - getting exception trying to use google geocoding api C#中的Google地理编码Json解析问题 - Google Geocoding Json Parsing Issue in C# 如何从谷歌地图获取纬度,经度? - How to get Lat,Long from google map? C# Google 地理编码:即使按照所需的步骤操作,权限也会不断被拒绝 - C# Google Geocoding: Constantly Getting Permission Denied Even When following the Steps Required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM