我试图在这个if语句中返回单独的字符串,而不是作为一个单独的字符串。一个作为纬度,另一个作为经度
static string GeoCoding(string address)
{
var json = new WebClient().DownloadString(baseUrlGC + address.Replace(" ", "+")
+ plusUrl);//concatenate URL with the input address and downloads the requested resource
GoogleGeoCodeResponse jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); //deserializing the result to GoogleGeoCodeResponse
string status = jsonResult.status; // get status
string geoLocation = String.Empty;
//check if status is OK
if (status == "OK")
{
for (int i = 0; i < jsonResult.results.Length;i++) //loop throught the result for lat/lng
{
geoLocation = jsonResult.results[i].geometry.location.lat + jsonResult.results[i].geometry.location.lng + Environment.NewLine; //append the result addresses to every new line
}
return geoLocation; //return result
}
else
{
return status; //return status / error if not OK
}
}
假设您的预期结果是2个字符串:纬度经度以及出现错误时的代码。我建议你创建一个新的
class GeoResponse{
List<(string, string)> geocodeList;
string status;
}
并将您的方法的返回类型更改为
static GeoResponse GeoCoding(string address)
{
var json = new WebClient().DownloadString(baseUrlGC + address.Replace(" ", "+")
+ plusUrl);//concatenate URL with the input address and downloads the requested resource
GoogleGeoCodeResponse jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); //deserializing the result to GoogleGeoCodeResponse
GeoResponse result = new GeoResponse();
result.status = jsonResult.status; // get status
//check if status is OK
if (status == "OK")
{
for (int i = 0; i < jsonResult.results.Length; i++) //loop throught the result for lat/lng
{
result.geocodeList.Add(jsonResult.results[i].geometry.location.lat, jsonResult.results[i].geometry.location.lng);
}
}
return result;
}
如果您想在status
是ok
时返回所有lat-long对(而不创建新的数据结构(,并在status
不是ok
时抛出异常,那么您可以这样做:
static List<Tuple<string, string>> GeoCoding(string address)
{
var json = new WebClient().DownloadString($"...");
var jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json);
if (jsonResult.status != "OK")
throw new Exception($"Request failed with {jsonResult.status}");
return jsonResult.results
.Select(result => result.geometry.location)
.Select(loc => new Tuple<string, string>(loc.lat, loc.lng))
.ToList();
}
如果你可以使用ValueTuple
,那么你可以像这样重写代码:
static List<(string Lat, string Long)> GeoCoding(string address)
{
...
return jsonResult.results
.Select(result => result.geometry.location)
.Select(loc => (loc.lat, loc.lng))
.ToList();
}
还请注意,WebClient
已弃用,因此请选择HttpClient
。
更新#1
我想输出";N/A";ZERO_RESULTS
static List<(string Lat, string Long)> GeoCoding(string address)
{
var json = new WebClient().DownloadString($"...");
var jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json);
if (jsonResult.status == "ZERO_RESULTS")
return new List<(string, string)> { ("N/A", "N/A") };
if (jsonResult.status != "OK")
throw new Exception($"Request failed with {jsonResult.status}");
return jsonResult.results
.Select(result => result.geometry.location)
.Select(loc => (loc.lat, loc.lng))
.ToList();
}