首页 > 解决方案 > Google Api 未提供位置

问题描述

我正在使用 google API 在我的项目中获取位置。它有时会给出位置,但大多数时候它不会给出任何位置。从今天开始,它给服务器返回一个错误 403 禁止。当我在浏览器中点击 URL 时,它会给出位置。我没有为我的 API 使用任何密钥,这是否会导致此问题。

只是补充一下-我从我的 api 获取经纬度,然后从 google api 获取位置并将其存储在我的数据库中。

我正在做的是: -

public static string getGoogleLocationByLogLat(string log, string lat)
{

    try
    {
         if (log == "")
    {
        log = "0.0";
    }
    if (lat == "")
    {
        lat = "0.0";
    }

    XmlTextReader xReader;
    bool element = false;
    string location = "", time, GL, parentEmail, respDateTime;
    string parentElementName = "";
    string childElementName = "";
    string childElementValue = "", prmryCntact = "";

    string url = "http://maps.google.com/maps/api/geocode/xml?latlng=" + lat + "," + log + "&sensor=true";

    xReader = new XmlTextReader(url);

        while (xReader.Read())
        {

            if (xReader.NodeType == XmlNodeType.Element)
            {

                if (element)
                {
                    parentElementName = parentElementName + childElementName + "<br>";
                }

                element = true;

                childElementName = xReader.Name;

            }

            else if (xReader.NodeType == XmlNodeType.Text | xReader.NodeType == XmlNodeType.CDATA)
            {

                element = false;

                childElementValue = xReader.Value;

                if (childElementName == "formatted_address")
                {
                    location = childElementValue;
                    break;
                }
            }

        }

    if (location.Trim() == "")
    {
        location = "Location not found";
    }

    return location;
    }
    catch (Exception ex)
    {
        return "LOCATION NOT FOUND";
    }
}

标签: google-mapsgoogle-apigeolocationgoogle-location-services

解决方案


添加您的 API 密钥

&key=yourkey

随着地图 API 的最新政策更新,我相信这一定是您遇到的情况......

也使用 https ... 仅允许通过 https


推荐阅读