C # Baidu map development (5) IP location

Source: Internet
Author: User

C # Baidu map development (5) IP location

Previously, we have implemented coordinate positioning and front-end display, while coordinate acquisition is generally only on mobile devices, which is hard to be obtained on PCs. But on the PC side, we can get the corresponding IP address. After obtaining the IP address, we can determine it. The Code is as follows:

 

Public class BaiduMap {////// URL template for obtaining Location Information Based on IP address. /// Parameter 1: The KEY of Baidu map API. /// Parameter 2: IP address. Public const string IP_LOCATION_URL_TEMPLATE = http://api.map.baidu.com/location/ip? Ak = {0} & ip = {1} & coor = bd09ll ;////// Obtain the location information based on the IP address //////Coordinates ///
  Public static IpLocationResult FetchLocation (String ip) {if (String. isNullOrWhiteSpace (ip) {return null;} String ipLocationUrl = String. format (IP_LOCATION_URL_TEMPLATE, MAP_KEY_BAI_DU, ip); String responseText = RequestHelper. requestUrl (ipLocationUrl, null); if (String. isNullOrWhiteSpace (responseText) {return null;} IpLocationResult locationResult = null; try {locationResult = Newtonsoft. json. jsonConvert. deserializeObject
  
   
(ResponseText);} catch (Exception) {return null;} return locationResult ;}# endregion}
  
Note;

 

(1). Use const to define the Api URL template.

(2) enter the Baidu KEY after applying for it.

(3). IpLOcationResult receives the deserialization result of json data. The implementation of this class is as follows:

 

Namespace MapApi. Baidu {[Serializable] public class IpLocationResult {////// Status ///Public String status {get; set ;}////// Address ///Public String address {get; set ;}////// Content ///Public IpLocationResult_Content content {get; set ;}# region IpLocationResult_Content ////// Locate the result text ///[Serializable] public class IpLocationResult_Content {////// Address ///Public String address {get; set ;}////// Address details ///Public IpLocationResult_Content_AddressDetail address_detail {get; set ;}////// Longitude and latitude ///Public Coordinate point {get; set ;}}////// Address details of the positioning result text ///[Serializable] public class IpLocationResult_Content_AddressDetail {////// City ///Public String city {get; set ;}////// City code ///Public String city_code {get; set ;}////// Region ///Public String district {get; set ;}////// Province ///Public String province {get; set ;}////// Sub-district ///Public String street {get; set ;}////// House number ///Public String street_number {get; set ;}# endregion}

The test code is as follows:

 

 

   protected void btnTest_Click(object sender, EventArgs e)        {            String ip = 47.153.128.1;            IpLocationResult ipLocationResult = BaiduMap.FetchLocation(ip);            Alert.Show(ipLocationResult.status.ToString());        }

Test result;

 

Note:

(1). You can see that the positioning result is obtained based on the IP address. However, the result of IP location can only be accurate to the city, so it is a precise positioning result. If you need to precisely locate the result, you still need to use the longitude and latitude coordinates.

(2). You can see the longitude and latitude corresponding to the IP address from the result, so you can display the positioning on the map at the front of the longitude and latitude (For details, refer to the previous article ).

So how can we get the value of these IP addresses for websites? See the following article "Baidu map development (6) IP address records for user access to web pages in C #".

 

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.