首页 > 代码库 > .Net调用百度地图API和LBS API的办法
.Net调用百度地图API和LBS API的办法
用webclient来实现POST跟GET获取百度地图API的数据,然后用Newtonsoft.Json来对json处理,得到想要的值。
返回的json结构参考百度地图API官方文档。
代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Linq; 6 using System.Net; 7 using System.Text; 8 using System.Threading.Tasks; 9 using Newtonsoft.Json; 10 using Newtonsoft.Json.Linq; 11 12 namespace CY.CommonService 13 { 14 public class MapHelper 15 { 16 private static string ak = "********";//百度应用的密钥 17 public static string GetAddress(double latitude, double longitude) 18 { 19 string baiduaddress = ConvertBaidu(latitude, longitude); 20 string url = string.Format("http://api.map.baidu.com/geosearch/v3/nearby?ak={0}&geotable_id=85509&location={1}&radius=1000&sortby=distance:1", 21 ak, baiduaddress); 22 using (WebClient client = new WebClient()) 23 { 24 try 25 { 26 client.Encoding = Encoding.UTF8; 27 var map = client.DownloadString(url); 28 JObject jobject = JsonConvert.DeserializeObject(map) as JObject; 29 string address = jobject["contents"][0]["address"].ToString(); 30 int distance = Convert.ToInt32(jobject["contents"][0]["distance"].ToString()); 31 string json = JsonHelper.JsonSerialize(new {address = address, distance = distance}); 32 return json; 33 } 34 catch//(Exception) 35 { 36 string address = GetBaiduMapAddress(latitude, longitude); 37 string json = JsonHelper.JsonSerialize(new {address = address, distance = 0}); 38 return json; 39 } 40 } 41 42 } 43 44 public static string GetBaiduMapAddress(double latitude, double longitude) 45 { 46 //string baiduaddress = ConvertBaidu(latitude, longitude); 47 string url = 48 string.Format( 49 "http://api.map.baidu.com/geocoder/v2/?ak={0}&location={1},{2}&output=json&pois=1&coordtype=wgs84ll", 50 ak, latitude, longitude); 51 using (WebClient client = new WebClient()) 52 { 53 try 54 { 55 client.Encoding = Encoding.UTF8; 56 var map = client.DownloadString(url); 57 JObject jobject = JsonConvert.DeserializeObject(map) as JObject; 58 string address = jobject["result"]["formatted_address"].ToString(); 59 return address; 60 } 61 catch//(Exception) 62 { 63 return "出错"; 64 } 65 } 66 } 67 68 public static string ConvertBaidu(double latitude, double longitude) 69 { 70 string url = string.Format("http://api.map.baidu.com/geoconv/v1/?coords={0},{1}&ak={2}&output=json", 71 longitude, latitude, ak); 72 using (WebClient client = new WebClient()) 73 { 74 try 75 { 76 client.Encoding = Encoding.UTF8; 77 var map = client.DownloadString(url); 78 JObject jobject = JsonConvert.DeserializeObject(map) as JObject; 79 string x = jobject["result"][0]["x"].ToString(); 80 string y = jobject["result"][0]["y"].ToString(); 81 string baiduaddress = x + "," + y; 82 return baiduaddress; 83 } 84 catch//(Exception) 85 { 86 return "出错"; 87 } 88 } 89 } 90 91 public static string SaveToBaidu(string latitude, string longitude,string address) 92 { 93 using (WebClient client = new WebClient()) 94 { 95 try 96 { 97 string data = http://www.mamicode.com/string.Format("title={0}&address={1}&latitude={2}&longitude={3}&coord_type=1&ak={4}&geotable_id=85509", 98 address, address, latitude, longitude, ak); 99 client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");100 byte[] sendData =http://www.mamicode.com/ Encoding.UTF8.GetBytes(data);101 byte[] response = client.UploadData("http://api.map.baidu.com/geodata/v3/poi/create", "POST", sendData);102 string responseString = Encoding.UTF8.GetString(response);103 JObject jobject = JsonConvert.DeserializeObject(responseString) as JObject;104 string status = jobject["status"].ToString();105 string message = jobject["message"].ToString();106 //string message = str.ToString();107 return message;108 }109 catch//(Exception)110 {111 return "出错";112 }113 }114 }145 }146 }
.Net调用百度地图API和LBS API的办法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。