/// <summary> /// 返回给定坐标点距离内的坐标; /// </summary> /// <returns></returns> public static GpsRectangle GetRectange(Gps gps, decimal distance) { return(new GpsRectangle { Top = gps.Latitude - distance / jl_wd, Bottom = gps.Latitude + distance / jl_wd, Left = gps.Longtitude - distance / jl_wd, Right = gps.Longtitude + distance / jl_wd }); }
/// <summary> /// 根据地址获得GPS的地理信息 /// 根据地址获得 坐标;注意,无法获取到建筑物, /// </summary> /// <param name="address"></param> /// <returns></returns> public static Gps GetGpsByAddress(string address) { string url = "{0}?address={2}&key={1}"; string urlreq = string.Format(url, new string[] { AMAP_GEO_URL, AMAP_APP_KEY, address }); string result = HttpUtil.ReadWeb(urlreq); /*System.Web.Script.Serialization.JavaScriptSerializer ser * = new System.Web.Script.Serialization.JavaScriptSerializer(); * {"status":"1","info":"OK","infocode":"10000","count":"1", * "geocodes":[{"formatted_address":"北京市朝阳区方恒国际中心A座","province":"北京市","citycode":"010","city":"北京市", * "district":"朝阳区","township":[],"neighborhood":{"name":[],"type":[]}, * "building":{"name":[],"type":[]},"adcode":"110105","street":[],"number":[],"location":"116.480690,39.989777","level":"门牌号"}]} */ /* * 注意上面的建筑物有问题,所以需要根据坐标去进行重新解析; */ //ser. var urlResult = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(result); if (urlResult["status"].ToString() == "1") { var gps = new Gps(); var dic = (JArray)urlResult["geocodes"]; if (dic.Count > 0) { var jobject = (JObject)dic.First(); gps.address = (string)jobject["formatted_address"]; gps.province = (string)jobject["province"]; gps.city = (string)jobject["city"]; JToken district = jobject.GetValue("district"); if (district != null) { if (district.ToArray().Length > 0 || district.ToString() != "[]") { //"广东省东莞市南城区东莞大道11号台商大厦57楼" 居然无法获得地区; gps.district = (string)jobject["district"]; } } if (gps.district == null) { gps.district = gps.city; } var location = ((string)jobject["location"]).Split(new char[] { ',' }); gps.Longtitude = Convert.ToDecimal(location[0]); gps.Latitude = Convert.ToDecimal(location[1]); } return(gps); } return(null); }
/// <summary> /// 根据坐标获得GPS的地理信息 /// </summary> /// <param name="longitude">坐标经度</param> /// <param name="latitude">坐标纬度</param> /// <returns></returns> public static Gps GetGpsByCoodinate(decimal longitude, decimal latitude) { try { var coopirate = longitude.ToString() + "," + latitude.ToString(); //http://restapi.amap.com/v3/geocode/regeo?key=8726e94e521325daaee5b4738d83bb56&location=116.481488%2C39.990464 string url = "{0}?location={2}&key={1}"; if (!cooperateDictionary.ContainsKey(coopirate)) { string urlreq = string.Format(url, new string[] { AMAP_REGEO_URL, AMAP_APP_KEY, coopirate }); string result = HttpUtil.ReadWeb(urlreq); //System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer(); var urlResult = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(result); if (urlResult["status"].ToString() == "1") { /* {"status":"1","info":"OK","infocode":"10000", * "regeocode":{"formatted_address":"北京市朝阳区望京街道阜通东大街6号", * "addressComponent":{"province":"北京市","city":[],"citycode":"010","district":"朝阳区","adcode":"110105", * "township":"望京街道","towncode":"110105026000", * "neighborhood":{"name":"方恒国际中心","type":"商务住宅;楼宇;商住两用楼宇"}, * "building":{"name":"方恒国际中心B座","type":"商务住宅;楼宇;商务写字楼"}, * "streetNumber":{"street":"阜通东大街","number":"6号","location":"116.481077,39.9904942","direction":"西","distance":"35.1865"}, * "businessAreas":[{"location":"116.47089234140496,39.9976009239991","name":"望京","id":"110105"}]}}} */ var gps = new Gps(); var dic = (JObject)(urlResult["regeocode"]); gps.address = (string)dic["formatted_address"]; var addressComponent = (JObject)(dic["addressComponent"]); gps.province = (string)addressComponent["province"]; var city = GetValue(addressComponent["city"]); gps.city = city ?? gps.province; if (addressComponent["district"] is JArray) { var district = (JArray)addressComponent["district"]; if (district.Count() == 0) { gps.district = gps.city; } else { gps.district = (string)district[0]; } } else { gps.district = (string)addressComponent["district"]; } // gps.township = (string) addressComponent["township"]; gps.Longtitude = longitude; gps.Latitude = latitude; var building = (JObject)(addressComponent["building"]); /*{ "status":"1","info":"OK","infocode":"10000","regeocode":{ "formatted_address":"安徽省安庆市岳西县巍岭乡三道河","addressComponent":{ "province":"安徽省","city":"安庆市","citycode":"0556","district":"岳西县","adcode":"340828","township":"巍岭乡","towncode":"340828210000","neighborhood":{ "name":[],"type":[] * },"building":{"name":[],"type":[] * },"streetNumber":{"street":[],"number":[],"direction":[],"distance":[]},"businessAreas":[[]]}}}*/ gps.building = GetValue(building["name"]); gps.buildingType = GetValue(building["type"]); cooperateDictionary[coopirate] = gps; return(gps); } else { return(null); } } else { return(cooperateDictionary[coopirate]); } } catch { return(null); } }
/// <summary> /// 计算两个地方的距离; /// </summary> public static int CalculateDistance(Gps gpsa, Gps gpsb) { return((int)Math.Sqrt((Math.Pow((double)(jl_wd * Math.Abs(gpsa.Longtitude - gpsb.Longtitude)), 2) + Math.Pow((double)(jl_jd * Math.Abs(gpsa.Latitude - gpsb.Latitude)), 2)))); }