<summary>
Get city names based on latitude and longitude
</summary>
<param name= "lat" > Latitude </param>
<param name= "LNG" > Longitude </param>
<returns> City Name </returns>
public static string Getcity (String lat, string lng)
{
String url = string. Format (
"{0}?output={1}&ak={2}&location={3},{4}",
configurationmanager.appsettings["Baidugeocodingapi"],
"JSON",
configurationmanager.appsettings["Baiduak"],
Lat
LNG);
String citystring = Remotequery.getresponse (URL);
if (string. IsNullOrEmpty (citystring)) return string. Empty;
Jobject city = (jobject) jsonconvert.deserializeobject (citystring);
if (city["status"). ToString ()! = "0")
{
Log. Error ("Get City error with Status:" + city["status"]);
return string. Empty;
}
Return (string) city["Result" ["addresscomponent"] ["City"];
}
public static string GetResponse (String accessurl)
{
string response = string. Empty;
Try
{
#region access the service URL and get the JSON data returned
HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (Accessurl);
Request. Method = "GET";
Request. ContentType = "application/x-www-form-urlencoded";
Request. Credentials = CredentialCache.DefaultCredentials;
Request. Timeout = 10000;
HttpWebResponse HttpWebResponse = null;
HttpWebResponse = Request. GetResponse () as HttpWebResponse;
StreamReader sr = new StreamReader (Httpwebresponse.getresponsestream (), Encoding.UTF8);
Response = Sr. ReadToEnd ();
Sr. Close ();
#endregion
}
catch (Exception ex)
{
Log. Info ("Error when accessing:" + Accessurl);
Log. Error (ex. Message);
}
return response;
}
}
<!--configuration file Weather longitude=116.31985&latitude=39.959836, Beijing--
<add key= "Baiduweatherapi" value= "Http://api.map.baidu.com/telematics/v3/weather"/>
<add key= "Baiduak" value= "3lx7gmqlza5kzqwde4zizmul"/>
C # Analog HTTP sends a post or GET request