/// <summary> /// 执行地图查询. /// </summary> private void DoMapQuery() { BaiduMapService service = new BaiduMapService(); BaiduGeocoderResult result = service.Geocoding(this.txtAddress.Text); if (result.result != null && result.result.location != null) { this.lon = result.result.location.lng; this.lat = result.result.location.lat; } }
public BaiduGeocoderResult Geocoding(string addressName) { string url = BASE_URL + System.Web.HttpUtility.UrlEncode(addressName, Encoding.UTF8); //访问该链接 WebRequest wrt = WebRequest.Create(url); //获得返回值 WebResponse wrs = wrt.GetResponse(); // 从 Internet 资源返回数据流。 Stream s = wrs.GetResponseStream(); // 构造 序列化类. DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(BaiduGeocoderResult)); // 读取结果. BaiduGeocoderResult result = (BaiduGeocoderResult)dcjs.ReadObject(s); // 返回. return(result); }