Call Baidu Map API for geocoding and reverse geocoding (1)

Some time ago, a senior brother wanted to help you do something similar to geocoding, that is, after entering the latitude and longitude in the form, you can realize the batch output of the geographical location through the program, but I didn't write it for a long time.

Under the guidance of the project manager (Teacher Yu) who was intern at the time, I understood what it meant, and the code was also written by him. I hereby declare.


interface design:

   

Instructions for use: Enter the latitude and latitude in the two Text text boxes above, and the third Text text box will output the address;

Enter the address                   in the two Text text boxes above , and the third Text text box will output the latitude and latitude ;


Trigger the event when "Geocode" is clicked:

        

   private void button2_Click(object sender, EventArgs e)
        {
            string url = "http://api.map.baidu.com/geocoder/v2/?ak=Y4NcP7YcxEkiwSuYedq5vW09&output=json&address=" + this.textBox2.Text + "&city=" + this.textBox1.Text;

            WebClient wc = new WebClient();
            Stream stream = wc.OpenRead(url);
            StreamReader sr = new StreamReader(stream);
            string strLine = "";
            while ((strLine = sr.ReadLine()) != null)
            {
                JsonObject js = JsonObject.Parse(strLine) as JsonObject;
                if (js["status"].ToString()=="0")
                {
                    string result = "Longitude:";
                    result += js["result"]["location"]["lat"];
                    result += ",纬度:" + js["result"]["location"]["lng"];
                    this.textBox4.Text = result;

                }
              
            }
            sr.Close();
        }

           Description: Use Json format to transfer data, call  Geocoding API Web service API requires Baidu LBS development platform to apply for a secret key, the link is as follows: http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding


          Trigger the event after clicking "Reverse Geocoding":  

           

   private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://api.map.baidu.com/geocoder/v2/?ak=Y4NcP7YcxEkiwSuYedq5vW09&output=json&location=" + this.textBox1.Text + "," + this.textBox2.Text + "&pois=0";

            WebClient wc = new WebClient();
            Stream stream = wc.OpenRead(url);
            StreamReader sr = new StreamReader(stream);
            string strLine = "";
            while ((strLine = sr.ReadLine()) != null)
            {
                JsonObject js = JsonObject.Parse(strLine) as JsonObject;
                if (js["status"].ToString() == "0")
                {
                    string result = "地址:" + js["result"]["formatted_address"];
                    this.textBox4.Text = result;
                }
            }
            sr.Close();
        }
    }




 

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325357397&siteId=291194637