前段时间有师兄希望帮忙做一个类似于地理编码之类的东西,就是在表格里输入经纬度以后可以通过程序实现地理位置的批量输出,自己写了半天也没写出来,
在当时实习的项目经理(于老师)的指导下我才明白什么意思,代码也都是他写的,特此声明。
界面设计:
使用说明:在上面两个Text文本框分别输入纬度和纬度,第三个Text文本框会输出地址;
在上面两个Text文本框分别输入地址,第三个Text文本框会输出纬度和纬度;
点击"地理编码"后触发事件:
private void button2_Click(object sender, EventArgs e){string url = "http://api./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 = "经度:";result += js["result"]["location"]["lat"];result += ",纬度:" + js["result"]["location"]["lng"];this.textBox4.Text = result;}}sr.Close();}
说明:使用Json格式传输数据,调用 Geocoding APIWeb服务API需要百度LBS开发平台申请秘钥,链接如下:/map/index.php?title=webapi/guide/webservice-geocoding
点击"逆地理编码"后触发事件:
private void button1_Click(object sender, EventArgs e){string url = "http://api./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();}}