I'm trying to return to separate string in this if statement and not as a single string. one as a latitude and the other as longitude
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
}
}
If you would like to return with all lat-long pairs (without creating a new data structure) when
status
isok
and throw an exception whenstatus
was notok
then you can do that like this:If you can use
ValueTuple
then you could rewrite the code like this:Please also note that
WebClient
is deprecated so please preferHttpClient
instead.UPDATE #1