public string GetAddress(Point gps) { //Google Geocoding Api //https://developers.google.com/maps/documentation/geocoding/intro#start DAL.MgooWebClient mwc = new DAL.MgooWebClient(); mwc.Method = "get"; mwc.Url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + gps.Lng + "," + gps.Lat + "&key=" + key + "&result_type=street_address&language=" + language; string jsonLocation = mwc.Send(); Dictionary <string, object> addressDic = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonLocation); string address = "未获取到地址."; if (addressDic["status"].Equals("OK")) { var results = (JArray)addressDic["results"]; var result = (JObject)results[0]; address = result["formatted_address"].ToString(); } else if (addressDic["status"].Equals("OVER_QUERY_LIMIT")) { //key 配额已超限 var path = System.AppDomain.CurrentDomain.BaseDirectory + "/google.log"; File.AppendAllText(path, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "已超出配额。" + Environment.NewLine); return("OVER_QUERY_LIMIT."); } return(address); }
public string GetAddress(Point gps) { try { //高德地图 Georegeo API //http://lbs.amap.com/api/webservice/guide/api/georegeo string jsonLocation = ""; DAL.MgooWebClient wc = new DAL.MgooWebClient(); wc.Method = "GET"; wc.Url = "http://restapi.amap.com/v3/geocode/regeo?output=json&location=" + gps.Lat + "," + gps.Lng + "&key=" + key + "&radius=1000&extensions=all"; jsonLocation = wc.Send(); //"{\"status\":\"1\",\"info\":\"OK\",\"infocode\":\"10000\",\"regeocode\":{\"formatted_address\":\"广东省中山市南朗镇华峰路\",\"addressComponent\":{\"country\":\"中国\",\"province\":\"广东省\",\"city\":\"中山市\",\"citycode\":\"0760\",\"district\":[],\"adcode\":\"442000\",\"township\":\"南朗镇\",\"towncode\":\"442000113000\",\"neighborhood\":{\"name\":[],\"type\":[]},\"building\":{\"name\":[],\"type\":[]},\"streetNumber\":{\"street\":\"华峰路\",\"number\":\"15号\",\"location\":\"113.509635,22.5109078\",\"direction\":\"东北\",\"distance\":\"31.9644\"},\"businessAreas\":[[]]}}}" Dictionary <string, object> addressDic = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonLocation); if (addressDic["status"].Equals("1")) { JObject regocode = (JObject)addressDic["regeocode"]; //var regocode = addressDic["regeocode"] as Dictionary<string, object> ; var formatted_address = regocode["formatted_address"].ToString(); //地址 var pois = (JArray)regocode["pois"]; // as List<Dictionary<string, string>>; //地址附近的POI数组,当请求的extensions 等于all 时才有 if (pois.Count > 0) { var pois_first = pois[0]; var pois_first_name = pois_first["name"].ToString(); var pois_first_direction = pois_first["direction"].ToString(); //方向 var pois_first_distance = pois_first["distance"].ToString(); //该POI到请求坐标的距离(米) if (formatted_address.EndsWith(pois_first_name) && pois.Count > 1) { pois_first = pois[1]; pois_first_name = pois_first["name"].ToString(); pois_first_direction = pois_first["direction"].ToString(); //方向 pois_first_distance = pois_first["distance"].ToString(); //该POI到请求坐标的距离(米) } formatted_address += ",离" + pois_first_name + "(" + pois_first_direction + ")约" + Convert.ToDouble(pois_first_distance).ToString("0") + "米"; } return(formatted_address); } else if (addressDic["status"].Equals("10003")) { //访问已超出日访问量 //开发者的日访问量超限,被系统自动封停,第二天0: 00会自动解封。 var path = System.AppDomain.CurrentDomain.BaseDirectory + "/amap.log"; File.AppendAllText(path, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " 访问已超出日访问量" + Environment.NewLine); return("DAILY_QUERY_OVER_LIMIT"); } else { return("未获取到地址信息"); } } catch (Exception ex) { throw ex; } }
public string GetAddress(Point gps) { try { //百度地图 Geocoding API //http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding DAL.MgooWebClient wc = new DAL.MgooWebClient(); wc.Url = "http://api.map.baidu.com/geocoder/v2/?ak=" + key + "&location=" + gps.Lng + "," + gps.Lat + "&output=json&pois=1&extensions_poi=0&radius=1000"; //http://api.map.baidu.com/geocoder/v2/?location=27.148609,111.227029&output=json&pois=1&ak=PFEwxiwsyv4GjEQcZrNZS0NsIkEvU8TL&extensions_poi=0&radius=1000 wc.Method = "GET"; string jsonLocation = wc.Send(); var addressDic = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonLocation); var formatted_address = ""; if (addressDic["status"].ToString().Equals("0")) { JObject result = (JObject)addressDic["result"]; // var result = addressDic["result"] as Dictionary<string, object>; formatted_address = result["formatted_address"].ToString(); var pois = (JArray)result["pois"]; if (pois.Count > 0) { var pois_first = pois[0]; var pois_first_name = pois_first["name"].ToString(); var pois_first_direction = pois_first["direction"].ToString(); //方向 var pois_first_distance = pois_first["distance"].ToString(); //该POI到请求坐标的距离(米) formatted_address += ",离" + pois_first_name + "(" + pois_first_direction + ")约" + Convert.ToInt32(pois_first_distance) + "米"; } return(formatted_address); } else if (addressDic["status"].ToString().Equals("1")) { var path = System.AppDomain.CurrentDomain.BaseDirectory + "/baidu.log"; File.AppendAllText(path, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "配额校验失败" + Environment.NewLine); return("配额校验失败."); } else { return("未获取到地址信息."); } } catch (Exception) { return("未知."); } }