Get the latitude or longitude according to the address of the province, or get the address information according to latitude and longitude

Source: Internet
Author: User

When Web development involves map operations or mobile client development, there are sometimes the following requirements:

Get the latitude or longitude according to the address of the province, or get the address information according to latitude and longitude

The following main record is a solution to the API provided by Baidu:

In the Baidu development platform provides such a call Geocoding API, this API is used to provide from the address to latitude and longitude coordinates or from the latitude and longitude coordinates to the address of the transformation service, using the HTTP protocol, return JSON or XML data.

Geocoding: That is, address resolution, from the detailed to the street of the structured address to get Baidu latitude and longitude information, such as: "Haidian District, Beijing Zhongguancun South Street, No. 27th" The result of address resolution is "lng:116.31985,lat:39.959836". At the same time, geocoding also support the historical sites, landmark building name directly analytic return Baidu latitude and longitude, for example: "Baidu Mansion" address resolution results are "lng:116.30815,lat:40.056885", general poi search requirements, the proposed use place API.

Inverse geocoding: That is, inverse address resolution, by Baidu latitude and longitude information to get structured address information, for example: "lat:31.325152,lng:120.558957" inverse address analysis results are "Jiangsu province Suzhou Huqiu District Tower Garden Road No. 318."

Usage Restrictions:

Baidu Map Geocoding API is a set of free open API, default quota 1 million times/day.

How to use:

The first step: Apply for AK (that is, to obtain a key), if no Baidu account first need to register Baidu account.

In the second step, spell the URL of the HTTP request, and note that you need to use the first step to request the AK.

The third step is to receive the data returned by the HTTP request (JSON and XML formats are supported).

Hello, world Example:

Send an address is the "Baidu Tower" request, return the address corresponding to the geographical coordinates. The sample URLs are as follows:

http://api.map.baidu.com/geocoder/v2/?address= 10th, 10 Street, Haidian District, Beijing, China &output=json&ak= E4805d16520de693a3fe707cdc962045&callback=showlocation

Case:

1. Packaging Tool Class

classTools { Public Static $LNGLATURL= ' http://api.map.baidu.com/geocoder/v2/';//Request Address Public Static $ak= ' 4UGEXHTYX6AHQMNSFPP7SNHPE ';//The key that the user applies for registration/** * Baidu Map API, through the provinces/cities/counties to obtain the latitude and longitude of the corresponding address * @param string $city Province/city/county * @param string $address Province/city/county or more detailed address
* @param string $output The format of the returned data*/ Public Static functionLnglat ($address,$city= ', $output = ' json ') { $res= Json_decode (file_get_contents(Self::$LNGLATURL."? Ak= ". Self::$ak." &output= ". $output." &address= ".UrlEncode($address)." &city= ".UrlEncode($city))); if($res->status = = 0) { return $res->result->Location ; } return false; }}

2. Calling API

 Public function Api () {        $address = ' Nanjing ';         $city = ' Jiangsu province ';         $res = Tools::lnglat ($address,$city);         Var_dump ($res);    }

3, the result of printing

Object (StdClass) [104]    Public float 118.77807440803 public  float 32.057235501806

A more detailed description: http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding

Get the latitude or longitude according to the address of the province, or get the address information according to latitude and longitude

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.