/// <summary> /// 获取指定位置城市经纬度 /// </summary> /// <param name="samelocation"></param> /// <param name="errortimes">0</param> /// <returns></returns> public static SameLocation GetCityLatLng(SameLocation samelocation) { if (samelocation.Location != ":") { string[] locationgroup = samelocation.Location.Split(','); string location = samelocation.Location; for (int i = locationgroup.Length - 1; i >= 0; i--) { string city = SelectCity(location, 0); if (city != null) { samelocation.Location = city; samelocation = GetLatLng(samelocation, 0); break; } if (i > 0) { location = location.Remove(location.Length - locationgroup[i].Length - 1); } } } else { samelocation.Lat = null; } return(samelocation); }
/// <summary> /// 获取指定位置经纬度 /// </summary> /// <param name="samelocation"></param> /// <param name="errortimes">0</param> /// <returns></returns> public static SameLocation GetLatLng(SameLocation samelocation, int errortimes) { try { Prolongation.JHWebClient client = new Prolongation.JHWebClient(); System.Threading.Thread.Sleep(100); string locationinfo = client.DownloadString("http://maps.google.com/maps/api/geocode/json?sensor=false&address=" + samelocation.Location); if (locationinfo.IndexOf("\"location\"") != -1) { string LatLng = new Regex("\"location\":{(.*?)},").Matches(locationinfo.Replace(" ", "").Replace("\n", ""))[0].Groups[1].ToString(); string Lat = LatLng.Split(',')[0].Replace("\"lat\":", ""); string Lng = LatLng.Split(',')[1].Replace("\"lng\":", ""); samelocation.Lat = Lat; samelocation.Lng = Lng; } else { if (errortimes < 3) { samelocation = GetLatLng(samelocation, errortimes + 1); } else { samelocation.Lat = "DataError"; } } } catch (Exception) { if (errortimes < 3) { samelocation = GetLatLng(samelocation, errortimes + 1); } else { samelocation.Lat = "NetworkError"; } } return(samelocation); }
/// <summary> /// 获取坐标城市经纬度 /// </summary> /// <param name="samelocation"></param> /// <param name="errortimes">0</param> /// <returns></returns> public static SameLocation GetLatLngCityLatLng(SameLocation samelocation) { if (samelocation.Location != ",") { string location = samelocation.Location; string city = SelectLatLngCity(location, 0); if (city != null) { samelocation.Location = city; samelocation = GetLatLng(samelocation, 0); } else { samelocation.Lat = null; } } else { samelocation.Lat = null; } return(samelocation); }