【问题标题】:Is the GeoCoding Maps API only valid for websites?GeoCoding Maps API 是否仅对网站有效?
【发布时间】:2016-12-21 15:02:37
【问题描述】:

我有一个当前运行 .Net Framework 4.6.1 并在 Visual Studio 2015 中维护的 Windows Forms (WinForm) 计费产品。

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

我给自己弄了一个测试密钥,并将它应用到我的代码中。

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>

可以在 Windows 窗体应用程序中使用 Google GeoCoding API 吗?

【问题讨论】:

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


    【解决方案1】:

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

    确实,我刚刚用我自己的 API 密钥测试了你的代码,它工作正常,这让我觉得你的 API 密钥有问题。

    所以尝试按照以下步骤创建一个新的:

    1. 登录developer console
    2. 选择您的项目或创建一个新项目 (1)
    3. 转到Library (2) 并搜索Geocoding API (3)
    4. 进入后点击Enable (4) 启用它
    5. 转到凭据并点击 Create credentials 按钮 (5)
    6. 从凭据类型中选择API key (6)

    【讨论】:

    • 很好的回应!我无法在回复中发布图片。这里显示的是我目前拥有的。它缺少什么吗? dropbox.com/s/bzvdcpn6muqlvmd/screenshot.jpg?dl=0
    • 只是一个提醒。如果您从域调用 google API,而不是从浏览器直接调用,则必须在您获得的 google 帐户中授权域。 @jp2code
    猜你喜欢
    • 2016-06-12
    • 2011-11-20
    • 2018-02-27
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2016-09-18
    相关资源
    最近更新 更多