C # access Baidu map via network address in the background to retrieve the longitude and latitude of the current map, and deserialize the retrieved string in complicated Json format (Newtonsoft. Json ),

Source: Internet
Author: User

C # access Baidu map via network address in the background to retrieve the longitude and latitude of the current map, and deserialize the retrieved string in complicated Json format (Newtonsoft. Json ),

Directly go to the Code: explanations are all in the code

The accesskey must be applied for on Baidu map.

When you apply for an accesskey, you only need to enter * For the render attribute.

Namespace HampWebControl is my space name!

Namespace HampWebControl is my space name!

Namespace HampWebControl is my space name!

Using Newtonsoft. json; using System. IO; using System. net; using System. security. policy; using System. text; namespace HampWebControl {public partial class WebForm2: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {var a = ReturnXY (); # The region Window is displayed to view the xy value, in actual business, the string X =. x. toString (); string Y =. y. toString (); string alert = "x value:" + X + ", y value:" + Y; Response. write ("<script> alert ('" + alert + "'); </script>"); # endregion} public static Point ReturnXY () {string strBuff = ""; url url = new Url (" http://api.map.baidu.com/location/ip?ak= "+" Self-applied Baidu AK "+" & coor = bd09ll "); // The HttpWebRequest class inherits from WebRequest and does not have its own constructor, you must use the Creat method of WebRequest to establish and forcibly convert the type to HttpWebRequest httpReq = (HttpWebRequest) WebRequest. create (url. value); // use the GetResponse () method of HttpWebRequest to establish HttpWebResponse and forcibly convert the type to HttpWebResponse httpResp = (HttpWebResponse) httpReq. getResponse (); // GetResponseStream () method to get the HTTP response data stream and try to get the content of the webpage specified in the URL. // if the content of the webpage is obtained successfully, System is used. IO. stream format If the API fails, the ProtoclViolationException error is returned. In this case, the following code should be put into a try block for processing. Stream respStream = httpResp. getResponseStream (); // The returned content is in the Stream format. Therefore, you can use the StreamReader class to obtain the content of GetResponseStream, read the content of each row of the source code of the webpage in sequence by // StreamReader, until the end of the line (Read encoding format: UTF8) StreamReader respStreamReader = new StreamReader (respStream, encoding. UTF8); strBuff = respStreamReader. readToEnd (); Models models = JsonConvert. deserializeObject <Models> (strBuff); var xy = models. content. point; return xy ;}} public class Models {public string address {get; set;} public Content content Content {get; set ;}public int status {get; set ;}} public class Content {public string address {get; set;} public AddressDetail address_detail {get; set;} public Point point {get; set ;}} public class AddressDetail {public string city {get; set;} public int city_code {get; set;} public string district {get; set;} public string province {get; set ;} public string street {get; set;} public string street_number {get; set ;}} public class Point {public Decimal x {get; set ;} public Decimal y {get; set ;}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.