Google Api is not giving location

泪湿孤枕 提交于 2019-12-11 08:36:35

问题


I am using google API to get location in my project. It gives location sometimes but most of the time it does not give any location. and from today it is giving server return an error 403 forbidden. When i am hitting the URL in my browser it is giving location. I am not using any key for my API can it cause this issue.

Just to add- I get the lat and long from my api and then i get the location from google api and stores it in my database.

What i am doing is:-

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

回答1:


Add your API key

&key=yourkey

With the latest policy updates to the maps api, i believe this must be the case that you're hitting...

Also use https ... It's permitted only over https



来源:https://stackoverflow.com/questions/51987226/google-api-is-not-giving-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!