C # Baidu map development (3) obtains location, business area, and surrounding information based on coordinates,

Source: Internet
Author: User

C # Baidu map development (3) obtains location, business area, and surrounding information based on coordinates,

In the article "C # Baidu map development (ii) Converting JSON data into corresponding classes", we get Baidu coordinates, which are used to obtain the corresponding information. Below is the corresponding code

Public class BaiduMap {// <summary> // URL template for obtaining positioning information based on coordinates. /// Parameter 1: The KEY of Baidu map API. /// Parameter 2: coordinate (longitude, latitude ). /// </Summary> public const string GEOCODING_COORDINATE_URL_TEMPLATE = "http://api.map.baidu.com/geocoder/v2? Ak = {0} & location = {1} & output = json & pois = 1 "; /// <summary> /// obtain positioning information based on coordinates /// </summary> /// <param name = "coordinates"> coordinates (longitude, latitude ), separate multiple coordinates with semicolons </param> // <param name = "mapCoordinateType"> coordinate type </param> /// <returns> </returns> public static coordLocationResult FetchLocation (String coordinates, mapCoordinateType mapCoordinateType) {CoordTransResult transformResult = TransToBaiduCoord (coordinates, mapCoordina TeType); String info = ""; if (! TransformResult. status. equals (CoordTransStatus. OK) {info = "coordinate conversion exception: the status is ---" + transformResult. status. toString (); return null;} if (transformResult. result = null | transformResult. result. length <= 0) {info = "Coordinate conversion exception: the result is null 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_1__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 of Baidu map API, which facilitates subsequent calls.

(2). The TransToBaiduCoord function is the encapsulation of non-Baidu coordinate conversion into a hundred-degree Coordinate Method in "C # Baidu map development (2) Converting JSON data into a corresponding class.

(3). The RequestUrl method is the encapsulation of the HTTP request initiated in Baidu map development (1) initiate an HTTP request in C.

(4) for the specific implementation of the CoordLocationResult class, see the code below.

Namespace MapApi. baidu {[Serializable] public class CoordLocationResult {// <summary> // status /// </summary> public String status {get; set ;} /// <summary >/// result /// </summary> public CoordLocationResult_Result result {get; set ;}} # region CoordLocationResult_Result // <summary> // locate the result /// </summary> [Serializable] public class CoordLocationResult_Result {// <summary> // locate the longitude and latitude // </ Summary> public CoordLocationResult_Result_Location {get; set ;}/// <summary> // structured address information /// </summary> public String formatted_address {get; set ;} /// <summary> // information of the business area, such as "Renmin University, Zhongguancun, Suzhou Street" // </summary> public String business {get; set ;} /// <summary> /// locate the administrative region /// </summary> public CoordLocationResult_Result_AddressComponent addressComponent {get; set ;}/// <summary> // The surrounding location Set /// </summary> public CoordLocationResult_Result_Poi [] pois {get; set ;} /// <summary> /// surrounding area /// </summary> public CoordLocationResult_Result_PoiRegion [] poiRegions {get; set ;} /// <summary> // city code // </summary> public String cityCode {get; set ;}} /// <summary> /// longitude and latitude of the positioning result /// </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 the positioning result /// </summary> [Serializable] public class CoordLocationResult_Result_AddressComponent {// <summary> // city name // /</summary> public String city {get; set ;}//< summary> /// district/county name /// </summary> public String district {get; set ;} /// <summary> /// province name /// </s Ummary> 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> // surrounding location information // </summary> [Serializable] public class CoordLocationResult_Result_Poi {// "addr ": "388, Jiahe Road, Huli district, Xiamen City, Fujian Province", // "cp": "NavInfo ", // "Direction": "West", // "distance": "49", // "name": "yongtongchang building", // "poiType ": "Business Tower", // "point": {// "x": 118.13374113945, // "y": 24.501871673827 //}, // "tel ":"", // "uid": "19c4b3f00002893beafb22a1e", // "zip ": "" /// <summary> /// address information // </summary> public String addr {get; set ;} /// <summary> // data source // </summary> public String cp {get; set ;} /// <summary> /// direction /// </summary> public String direction {get; set ;}/// <summary> /// distance from the coordinate point /// </summary> public String distance {get; set ;} /// <summary> /// poi name /// </summary> public String name {get; set ;}/// <summary> // poi type, for example, 'Office building, Business Mansion '// </summary> public String poiType {get; set ;}/// <summary> // poi coordinate {x, y} // </summary> public Coordinate point {get; set ;}/// <summary> // call // </summary> public String Tel {get; set ;}/// <summary> // unique poi identifier /// </summary> public String uid {get; set ;} /// <summary> // zip code // </summary> public String zip {get; set ;}} # endregion # region // <summary> // surrounding area // </summary> [Serializable] public class CoordLocationResult_Result_PoiRegion {// <summary> // target direction. For example: inner // </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 class constructor is based on the aforementioned constructor. You can also directly generate (Link) using the tool ).

The following 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:

As you can see, formatted_address is the location information, business is the business area information, and pois is the surrounding information. For other information, refer to the official documentation of Baidu map WebApi.

In this way, we get the location information of the specified coordinate point. What if the information is displayed on the previous map? Refer to "Baidu map development (4) Front-end display and positioning in C #".

For more information, see http://blog.csdn.net/xxdddail/article/details/42705549.

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.