C # Baidu map development (I) Initiate HTTP requests

Source: Internet
Author: User
Tags aliyun

C # Baidu map development (I) Initiate HTTP requests

The Baidu map development documentation provides many examples, but there are some differences when you use specific languages for development. I developed it using C. When obtaining the corresponding data, you need to pass the value through the URL and then obtain the corresponding return value (this is the case for many platforms: such as the enterprise number ). So what we need to solve first is how to initiate a URL request.

If it is at the front end, we can easily initiate a URL request, that is, enter the URL in the address bar of the browser. How can we initiate a request in the C # background? Let's look at the following code:

////// Initiate a request to the specified URL (which can be used to send data remotely )/////////Sent data//////
 Public static String RequestUrl (String url, byte [] data, String method = "POST") {try {WebRequest request = WebRequest. create (url); request. method = method; request. contentType = "application/x-www-form-urlencoded"; if (data! = Null & data. length> 0) {request. contentLength = data. length; Stream newStream = request. getRequestStream (); newStream. write (data, 0, data. length); newStream. close ();} else {request. contentLength = 0;} WebResponse response = request. getResponse (); Stream stream = response. getResponseStream (); MemoryStream MS = new MemoryStream (); long ChunkSize = 1024; byte [] buffer = new byte [ChunkSize]; long dataLengthToRead = response. contentLength; // The total size of the response data. while (dataLengthToRead> 0) {int lengthRead = stream. read (buffer, 0, Convert. toInt32 (ChunkSize); // read size (ms. write (buffer, 0, lengthRead); dataLengthToRead = dataLengthToRead-lengthRead;} stream. close (); response. close (); string responseText = Encoding. UTF8.GetString (ms. toArray (); return responseText;} catch (Exception ex) {return ex. message ;}}
Note:

(1) The WebRequest initiates a request and uses WebResponse to obtain the response data.

(2) The parameter URL can contain parameters, which are the same as the parameters added by the Common GET method.

(3) If the POST method is used, data can also be sent in the past. If data is null, it is similar to the GET method. With POST, cross-Origin data transmission can be implemented (you only need to point the URL to the URL of a general handler), such as file transmission.

The following is the test code.

////// Baidu map api key ///Public const string MAP_KEY_BAI_DU = "XXXXXXXXXXXXX ";////// Convert the URL Template into a hundred-degree coordinate. /// parameter 1: The KEY of Baidu map API. /// Parameter 2: coordinate data. The longitude and latitude of coordinates (longitude, latitude) are separated by commas, And the coordinates are separated by semicolons. /// Parameter 3: Type of the source coordinate. /// Parameter 4: Result coordinate type. ///Public const string TRANSFORM_COORDINATE_URL_TEMPLEATE = "http://api.map.baidu.com/geoconv/v1? Ak = {0} & coords = {1} & from = {2} & to = {3 }&";


protected void btnTest_Click(object sender, EventArgs e)        {            String coordinates = "39.92,116.46;40.13,117.10";            String transformUrl = String.Format(BaiduMap.TRANSFORM_COORDINATE_URL_TEMPLEATE,                                                 BaiduMap.MAP_KEY_BAI_DU,                                                 coordinates,                                                 (int)MapCoordinateType.GOOGLE_SOSO_ALIYUN_MAPABC_AMAP,                                                 (int)MapCoordinateType.BAIDU);            String transformResponsText = RequestHelper.RequestUrl(transformUrl, null);            Alert.Show(transformResponsText);        }
Public enum MapCoordinateType {////// Unknown type ///UNKNOWN = 0 ,////// The angle coordinate obtained by the GPS device ;///GPS_ANGLE = 1 ,////// The rice coordinate obtained by GPS and the coordinate used by the sogou map ;///GPS_METER_OR_SOGOU = 2 ,////// Coordinates Used for google Maps, soso maps, aliyun maps, mapabc maps, and amap maps ///GOOGLE_SOSO_ALIYUN_MAPABC_AMAP = 3 ,////// Metric coordinates corresponding to the coordinates of google Maps, soso maps, aliyun maps, mapabc maps, and amap maps ///GOOGLE_SOSO_ALIYUN_MAPABC_AMAP_METER = 4 ,////// Longitude and latitude coordinates used by Baidu map ///BAIDU = 5 ,////// Metric coordinates used by Baidu map ///BAIDU_METER = 6 ,////// Mapbar map coordinates ///MAPBAR = 7 ,////// 51 map coordinates ///_ 51 = 8}


Note:

(1). Baidu map KEY needs to be applied

(2). MapCoordinateType is an enumeration type encapsulated according to official documents.

(3) The main function of this test method is to convert non-Baidu coordinates into hundred-degree coordinates. If it is not Baidu's coordinates (for example, the obtained coordinate data), and then use Baidu's map to locate the location, there will be a huge difference, and some may be within 108,000, so it must be converted before use.

The following test results


We can see that the returned JSON data is returned. How can we convert this JSON data into a C # class? Please refer to the following article "Baidu map development of C # (2) Converting JSON data into corresponding classes".

Indicate the source for reprinting. Http://blog.csdn.net/xxdddail/article/details/42705027

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.