C# 地理编码地图API是否仅对网站有效?

C# 地理编码地图API是否仅对网站有效?,c#,google-maps,google-geocoding-api,C#,Google Maps,Google Geocoding Api,我有一个Windows窗体(WinForm)计费产品,目前正在运行.Net Framework 4.6.1,并在Visual Studio 2015中维护 我们被要求修改映射工具,该工具采用物理地址并返回纬度/经度网格坐标 我给自己准备了一个测试密钥,并将其应用到我的代码中 private void GoogleGeoCodeSearch(LocationRecord currentRecord, String key) { var street = String.Format("{0}

我有一个Windows窗体(WinForm)计费产品,目前正在运行.Net Framework 4.6.1,并在Visual Studio 2015中维护

我们被要求修改映射工具,该工具采用物理地址并返回纬度/经度网格坐标

我给自己准备了一个测试密钥,并将其应用到我的代码中

private void GoogleGeoCodeSearch(LocationRecord currentRecord, String key)
{
    var street = String.Format("{0}", currentRecord.street_name).Trim();
    var city = String.Format("{0}", currentRecord.city).Trim();
    var state = String.Format("{0}", currentRecord.state).Trim();
    var postal = String.Format("{0}", currentRecord.postal_code).Trim();
    var country = (state.Length == 2 && STATES.Contains(state)) ? "USA" : String.Format("{0}", currentRecord.country).Trim();
    var address = String.Format("{0}, {1}, {2}, {3} {4}", street, city, state, postal, country).Replace("-", String.Empty).Replace("'", String.Empty);
    var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false&region={1}&key={2}", Uri.EscapeDataString(address), country, key);

    var request = System.Net.WebRequest.Create(requestUri);
    using (var response = request.GetResponse())
    {
        var ok = false;
        var title = String.Format("{0}::Google API WebRequest", _title);
        var source = "response stream";
        var element = "XDocument";
        var xdoc = System.Xml.Linq.XDocument.Load(response.GetResponseStream());
        if (xdoc != null)
        {
            source = element;
            element = "GeocodeResponse";
            var geoResponse = xdoc.Element(element);
            if (geoResponse != null)
            {
                source = element;
                element = "result";
                var result = geoResponse.Element(element);
                if (result != null)
                {
                    source = element;
                    element = "geometry";
                    var geometry = result.Element(element);
                    if (geometry != null)
                    {
                        source = element;
                        element = "location";
                        var locationElement = geometry.Element(element);
                        if (locationElement != null)
                        {
                            source = element;
                            element = "lat/lng";
                            var lat = locationElement.Element("lat");
                            var lng = locationElement.Element("lng");
                            if ((lat != null) && (lng != null))
                            {
                                Double latitude, longitude;
                                Double.TryParse(lat.Value, out latitude);
                                Double.TryParse(lng.Value, out longitude);
                                SaveCoordinates(currentRecord, latitude, longitude);
                                ok = true;
                            }
                        }
                    }
                } else
                {
                    result = geoResponse.Element("status");
                    if (result != null)
                    {
                        var msg = result.Value;
                        result = geoResponse.Element("error_message");
                        if (result != null)
                        {
                            msg = String.Format("{0}{1}{1}{2}", msg, Environment.NewLine, result.Value);
                        }
                        MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
        if (!ok)
        {
            MessageBox.Show(String.Format("No [{0}] information found in [{1}] element.", element, source), title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
每次我到达这一行,我都会得到一个
NULL
响应:

var result = geoResponse.Element(element);
这就是解释:

<GeocodeResponse>
  <status>REQUEST_DENIED</status>
  <error_message>The provided API key is invalid.</error_message>
</GeocodeResponse>

请求被拒绝
提供的API密钥无效。

谷歌地理编码API可以在Windows窗体应用程序中使用吗?当然,您可以从WinForms应用程序中使用地理编码API。对于API,发送请求的内容无关紧要。重要的是请求参数正确,API密钥有效

事实上,我刚刚用我自己的API密钥测试了您的代码,它工作得很好,这让我觉得您的API密钥有问题

因此,请尝试按照以下步骤创建一个新的:

  • 登录
  • 选择项目或创建新项目(1)
  • 转到
    (2)并搜索
    地理编码API
    (3)
  • 进入内部后,单击“启用”(4)以启用它
  • 转到凭证并点击创建凭证按钮(5)
  • 从凭证类型(6)中选择API密钥

  • 当然,您可以从WinForms应用程序中使用地理编码API。对于API,发送请求的内容无关紧要。重要的是请求参数正确,API密钥有效

    事实上,我刚刚用我自己的API密钥测试了您的代码,它工作得很好,这让我觉得您的API密钥有问题

    因此,请尝试按照以下步骤创建一个新的:

  • 登录
  • 选择项目或创建新项目(1)
  • 转到
    (2)并搜索
    地理编码API
    (3)
  • 进入内部后,单击“启用”(4)以启用它
  • 转到凭证并点击创建凭证按钮(5)
  • 从凭证类型(6)中选择API密钥

  • 反响很好!我不能在回复上贴照片。这里显示的是我目前拥有的。有什么遗漏吗?只是提醒一下。如果您是从域调用google API,而不是从浏览器直接调用,则必须在您获得的google帐户中授权该域@JP2Code反应很好!我不能在回复上贴照片。这里显示的是我目前拥有的。有什么遗漏吗?只是提醒一下。如果您是从域调用google API,而不是从浏览器直接调用,则必须在您获得的google帐户中授权该域@jp2code