我目前正在编写一种方法来对我的邮政编码进行地理编码并将它们存储在我的 sql 数据库中以下是我的代码示例:
问题:“json”字符串中有两个数组。但是,当我尝试进行地理编码时,它只对第一个数组中的邮政编码进行地理编码,而不是第二个。我应该如何对第二个数组中的邮政编码进行地理编码。
protected void getGeoCode()
{
string json = DAO.getLatLongNull();
JObject jobject = JObject.Parse(json);
JArray array = (JArray)jobject["e"];
string postalCode = string.Empty;
string glat = string.Empty;
string glong = string.Empty;
string gPostal = string.Empty;
string sqlQuery = string.Empty;
string connectionString = DAO.GetConnectionString();
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
foreach (var item in array)
{
postalCode = item["postalCode"].ToString();
string url = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Singapore%20";
dynamic googleResults = new Uri(url + postalCode).GetDynamicJsonObject();
foreach (var result in googleResults.results)
{
glat += result.geometry.location.lat;
glong += result.geometry.location.lng;
gPostal += item["postalCode"].ToString();
sqlQuery = "update testlatlong set lat =@lat,long =@long where postalCode =@postal";
SqlCommand command = new SqlCommand(sqlQuery, sqlConn);
command.Parameters.Add("@lat", SqlDbType.NVarChar).Value = glat;
command.Parameters.Add("@long", SqlDbType.NVarChar).Value = glong;
command.Parameters.Add("@postal", SqlDbType.NVarChar).Value = gPostal;
command.ExecuteNonQuery();
}
}
}