如果位置错误,如何为google api请求设置错误消息

时间:2017-01-05 09:27:44

标签: c# asp.net asp.net-web-api asp.net-web-api-routing asp.net-webpages

{
    string url = "http://maps.google.com/maps/api/geocode/xml?address=" + Location.Text + "&sensor=false";

    WebRequest request = WebRequest.Create(url);
    using (WebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        {
            DataSet dsResult = new DataSet();
            dsResult.ReadXml(reader);

            DataTable dtCoordinates = new DataTable();

            dtCoordinates.Columns.AddRange(new DataColumn[4] 
            { 
                new DataColumn("Id", typeof(int)),
                    new DataColumn("Address", typeof(string)),
                    new DataColumn("Latitude",typeof(string)),
                    new DataColumn("Longitude",typeof(string)) 
            });

            //  if (response["status"] == OK)
            //   {

            foreach (DataRow row in dsResult.Tables["result"].Rows)
            {
                string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString();
                DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0];

                // TextBox1.Text = location["lat"];

                dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"]);
                // TextBox1.Text = dsResult.Tables["lat"].ToString;
                // TextBox2.Text = location["lng"];
            }
            GridView1.DataSource = dtCoordinates;
            GridView1.DataBind();
}

在上面的代码中,如果我向Google API提供错误的位置,则会在未设置实例时发生错误。在这里,我需要一条消息作为标签或其他任何位置错误。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

{
string url = "http://maps.google.com/maps/api/geocode/xml?address=" + Location.Text + "&sensor=false";
try{
WebRequest request = WebRequest.Create(url);
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
    {
        DataSet dsResult = new DataSet();
        dsResult.ReadXml(reader);

        DataTable dtCoordinates = new DataTable();

        dtCoordinates.Columns.AddRange(new DataColumn[4] 
        { 
            new DataColumn("Id", typeof(int)),
                new DataColumn("Address", typeof(string)),
                new DataColumn("Latitude",typeof(string)),
                new DataColumn("Longitude",typeof(string)) 
        });

        //  if (response["status"] == OK)
        //   {

        foreach (DataRow row in dsResult.Tables["result"].Rows)
        {
            string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString();
            DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0];

            // TextBox1.Text = location["lat"];

            dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"]);
            // TextBox1.Text = dsResult.Tables["lat"].ToString;
            // TextBox2.Text = location["lng"];
        }
        GridView1.DataSource = dtCoordinates;
        GridView1.DataBind();
}
catch(Exception err)
{
    if(err.Message.Contains("instance is not set"))//Check if you get error code then match error code
    {
        Console.Write("Custom message"); 
        MessageBox.Show("Custom message");
    }
    else
    { 
        Console.Write("Normal message"); 
        MessageBox.Show("Normal message");
    }
}
}

如果您有任何疑问或问题,请随时问我。