C # Baidu Map Development (iii) location, shopping area and surrounding information based on coordinates

Source: Internet
Author: User

In the "C # Baidu Map Development (ii) the conversion of JSON data to the corresponding class" in the article, we got the Baidu coordinates, now based on this coordinate to obtain the corresponding information. Here is the corresponding code

public class baidumap{//<summary>///The URL template for locating information based on coordinates.        Parameter 1: Key of Baidu Map API.                Parameter 2: Coordinates (longitude, latitude). </summary> Public Const string geocoding_coordinate_url_template = "http://api.map.baidu.com/g        Eocoder/v2/?ak={0}&location={1}&output=json&pois=1 "; <summary>////////</summary>//<param name= "coordinates" > coordinates (longitude, latitude) , separated by semicolons </param>//<param name= "mapcoordinatetype" > Coordinate type </param>///&LT;RETURNS&GT;&L                                                     t;/returns> public static Coordlocationresult fetchlocation (String coordinates, Mapcoordinatetype mapcoordinatetype) {Coordtransresult Transformresult = Transtobai            Ducoord (coordinates, MAPCOORDINATETYPE);            String info = ""; if (!transformresult.status.equals (Coordtransstatus.ok)) {Info = "Coordinate transformation exception: state is---" + transformResult.status.ToString ();            return null;  } if (Transformresult.result = = NULL | | transformResult.result.Length <= 0) {Info                = "Coordinate conversion exception: The result is empty or the array length is 0";            return null;            } String Locationurl = ""; foreach (coordinate coordtemp in transformresult.result) {Locationurl = String.Format (GEOCODING                                                    _coordinate_url_template, Map_key_bai_du,            coordtemp.x + "," + coordtemp.y);            } String Locationresponsetext = Requesthelper.requesturl (Locationurl, NULL);            Coordlocationresult locationresult = null; try {locationresult = newtonsoft.json.jsonconvert.deserializeobject<coordlocationresult> (            Locationresponsetext);     } catch (Exception e) {           info = "Locating exception:" + E.message;            return null;        } return Locationresult; }}
Note:

(1). Use the const constant to define a URL template for the Baidu Map API to facilitate subsequent calls.

(2). Transtobaiducoord function is "C # Baidu Map Development (ii) conversion JSON data for the corresponding class" in the non-Baidu coordinates into the Baidu coordinate method package.

(3). The Requesturl method is the encapsulation of the initiating HTTP request as described in "C # 's Baidu Map Development (i) initiating an HTTP request."

(4). For the specific implementation of the Coordlocationresult class, see the following code.

namespace mapapi.baidu{[Serializable] public class Coordlocationresult {//<summary>//        State///</summary> public String status {get; set;} <summary>//Results///</summary> public coordlocationresult_result result {get; set;    }} #region Coordlocationresult_result//<summary>//Positioning results//</summary> [Serializable]         public class Coordlocationresult_result {//<summary>//Latitude/Longitude///</summary>        Public coordlocationresult_result_location location {get; set;}        <summary>///structured address information///</summary> public String formatted_address {get; set;} <summary>///business area information, such as "Renmin University, Zhongguancun, Suzhou Street"///</summary> public String Business {g Et Set }///<summary>//location of the Administrative Region///</summary> public CoordlocatIonresult_result_addresscomponent addresscomponent {get; set;}  <summary>///Peripheral location///</summary> public coordlocationresult_result_poi[] POIs {get; Set }///<summary> surrounding area///</summary> public Coordlocationresult_result_poiregio        N[] Poiregions {get; set;}    <summary>///City Code///</summary> public String Citycode {get; set;} }///<summary>///positioning of the results///</summary> [Serializable] public class Coordlocationresult_ result_location {//<summary>//Longitude//</summary> public String LNG {get; Set    }//<summary>//latitude///</summary> public String lat {get; set;} }///<summary>//Administrative Region of location results///</summary> [Serializable] public class Coordlocationresult_re sult_addresscomponent {//<summary>///city Name///</summary> public String City {get; set;}        <summary>//county name///</summary> public String District {get; set;}        <summary>////</summary> public String province {get; set;}        <summary>///Street name///</summary> public String Street {get; set;}    <summary>///Street Number///</summary> public String street_number {get; set;}    } #endregion #region Coordlocationresult_result_poi///<summary>////around location information//</summary> [Serializable] public class Coordlocationresult_result_poi {//"addr": "Huli District Jiahe Road, Xiamen, Fujian Province 388",//"CP        ":" NavInfo ",//" direction ":" West ",//" Distance ":" $ ",//" name ":" Wing Cheong Building ", "Poitype": "Business Building",//"point": {//"X": 118.13374113945,//"Y": 24.501871673827//},//"tel": "",//"UI D ":" 19C4B3F2642893BEAFB22A1E ",//" Zip ":" "///<summary>//Address information///</summa        Ry> public String addr {get; set;}        <summary>///Data source///</summary> public String CP {get; set;}        <summary>//direction///</summary> public String direction {get; set;}        <summary>///from coordinate point///</summary> public String distance {get; set;}        <summary>///POI name///</summary> public String name {get; set;}        <summary>///POI type, such as ' office building, Business Building '///</summary> public String poitype {get; set;}        <summary>///POI coordinates {x, y}///</summary> Public coordinate point {get; set;}<summary>///Telephone///</summary> public String Tel {get; set;}        <summary>///POI uniquely identifies///</summary> public String uid {get; set;}    <summary>/////</summary> public String zip {get; set;}     } #endregion #region Coordlocationresult_result_poiregion//<summary>///</summary> [Serializable] public class Coordlocationresult_result_poiregion {//<summary>//target direction.        For example: Inside///</summary> public String Direction_desc {get; set;} <summary>///region name.    For example: Music, home life square///</summary> public String name {get; set;} } #endregion}
Note: The construction method of a class is based on the previous construction, or it can be generated directly using a tool (link).

Here is the test code

protected void btnTest_Click (object sender, EventArgs e)        {                      coordinate coordinate = new coordinate ("39.92", " 116.46 ");            Coordlocationresult coordlocationresult=baidumap.fetchlocation (coordinate);            Alert.show (CoordLocationResult.status.ToString ());        }

The test results are as follows

Can see, formatted_address is the location information, business is the business district information, POIs is around the information, other information can be self-reference Baidu map Webapi Official document description.

In this way, we get the location information for the specified coordinate point, and if that information is obtained, what if it is displayed on the previous map? Please see the following article "C # Baidu Map Development (four) front-end display and positioning."

Reprint please indicate the source http://blog.csdn.net/xxdddail/article/details/42705549.

C # Baidu Map Development (iii) location, shopping area and surrounding information based on coordinates

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.