/** * 转换手机坐标 * @param {*} gg_lon * @param {*} gg_lat */ public static BaiDuCoordinate Baidu2GCJ(double bd_lon, double bd_lat) { double x = bd_lon - 0.0065, y = bd_lat - 0.006; double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi); double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi); double gg_lon = z * Math.Cos(theta); double gg_lat = z * Math.Sin(theta); BaiDuCoordinate obj = new BaiDuCoordinate { Latitude = bd_lat, Longitude = bd_lon }; return(obj); }
/** * 转换百度坐标 * @param {*} gg_lon * @param {*} gg_lat */ public static BaiDuCoordinate GCJ2Baidu(double gg_lon, double gg_lat) { double bd_lon, bd_lat; double x = gg_lon, y = gg_lat; double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi); double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi); bd_lon = z * Math.Cos(theta) + 0.0065; bd_lat = z * Math.Sin(theta) + 0.006; BaiDuCoordinate obj = new BaiDuCoordinate { Latitude = bd_lat, Longitude = bd_lon }; return(obj); }
public static int GetTopShopModel() { string exceptionUri = ""; int exceptionShopID = 0; List <ShopModel> list = ShopDal.GetTopShopModel(); if (list.Count == 0) { return(1); } //Thread.Sleep(5000); try { foreach (ShopModel model in list) { BaiDuCoordinate bd_Coor = BaiduCoordinateTrans.GCJ2Baidu(model.Longitude, model.Latitude); /*个别经纬度格式不对*/ StringBuilder sb = new StringBuilder(); sb.Append("http://api.map.baidu.com/geocoder/v2/?"); sb.Append("latest_admin=1&"); sb.Append("extensions_town=true&"); sb.Append("ak=" + Const.BaiduAK + "&"); sb.Append("callback=renderReverse&"); //sb.Append("location=22.939708,113.951647&"); sb.Append("location=" + bd_Coor.Latitude + "," + bd_Coor.Longitude + "&"); sb.Append("output=xml&"); sb.Append("pois=0"); exceptionUri = sb.ToString(); exceptionShopID = model.ShopID; model.bd_Latitude = bd_Coor.Latitude; model.bd_Longitude = bd_Coor.Longitude; model.Uri = sb.ToString(); string result = WebHandler.GetHtmlStr(sb.ToString(), "UTF8"); if (result == "") { ShopDal.UpdateLongLat(model, 2);/*异常*/ continue; } XmlDocument doc = new XmlDocument(); doc.LoadXml(result); string statusPath = "/GeocoderSearchResponse/status"; string rootPath = "/GeocoderSearchResponse/result/addressComponent"; XmlElement root = doc.DocumentElement; if (root.SelectNodes(statusPath)[0].InnerText == "0") // 成功 { XmlNodeList listNodes = root.SelectNodes(rootPath); string country = root.SelectNodes(rootPath + "/country")[0].InnerText; string province = root.SelectNodes(rootPath + "/province")[0].InnerText; string city = root.SelectNodes(rootPath + "/city")[0].InnerText; string district = root.SelectNodes(rootPath + "/district")[0].InnerText; string town = root.SelectNodes(rootPath + "/town")[0].InnerText; string street = root.SelectNodes(rootPath + "/street")[0].InnerText; model.Province = province; model.CityID = city; model.County = district; model.Town = town; model.Address = street; ShopDal.Update(model, 1); } else { ShopDal.UpdateLongLat(model, 2);/*异常*/ } } } catch (Exception ex) { ShopDal.UpdateState(exceptionShopID, exceptionUri, 3);/*程序异常*/ LogUtil.WriteInfo(ex.Message); LogUtil.WriteInfo("exceptionHtml=" + exceptionUri); } return(0); }