Function: When the app obtains the user's geographic coordinates, it can know what data the user is currently in that country, province, city, and periphery according to the coordinates.
Principle: Based on Baidu geocoding API implementation, need to first register Baidu Developer, and then apply for Baidu AK (key)
Geocoding.class.php
/** * Base coordinates to obtain the country, province, city, and the surrounding data class (using Baidu geocoding API implementation) * Baidu Key acquisition method: Http://lbsyun.baidu.com/apiconsole/key? Application=key (need to register Baidu Developer account first) * date:2015-07-30 * author:fdipzone * ver:1.0 * * Func: * Public getaddresscomponent Get country, province, city and surrounding data by address * Private Tocurl use curl to call Baidu geocoding API
*/ classGeocoding {//Baidu geocoding APIConstAPI =' http://api.map.baidu.com/geocoder/v2/';//Do not display peripheral dataConstNo_pois =0;//Show Peripheral DataConstPOIS =1;/** * Base site for country, province, city and surrounding data * @param String $ak Baidu AK (Key) * @param Decimal $longi Tude Longitude * @param Decimal $latitude latitude * @param Int $pois Show Peripheral data * @return /c6> Array * / PublicStatic functiongetaddresscomponent($ak, $longitude, $latitude , $pois=self::no_pois){$param=Array(' AK '=$ak,' Location '= Implode (',',Array($latitude,$longitude)),' POIs '=$pois,' Output '=' JSON ');//Request Baidu API$response= Self:: Tocurl ( Self:: API,$param);$result=Array();if($response){$result= Json_decode ($response,true); }return$result; }/** * Use curl to call Baidu geocoding API * @param String $url requested address * @param Array $param Request The parameters * @return JSON */PrivateStatic functiontocurl($url, $param=array()){$ch= Curl_init ();if(Substr ($url,0,5)==' https ') {curl_setopt ($ch, Curlopt_ssl_verifypeer,false);//Skip certificate Checkcurl_setopt ($ch, Curlopt_ssl_verifyhost,true);//Check whether the SSL encryption algorithm exists from the certificate} curl_setopt ($ch, Curlopt_returntransfer,true); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_postfields, Http_build_query ($param));$response= Curl_exec ($ch);if($error=curl_error ($ch)){returnfalse; } curl_close ($ch);return$response; }}?>
demo.php
require"Geocoding.class.php";$ak = '这里填写你的百度AK';$longitude = 113.327782;$latitude = 23.137202;$result = Geocoding::getAddressComponent($ak, $longitude, $latitude, Geocoding::NO_POIS);echo'';print_r($result);echo'
';?>
Example returns:
Array([Status] =0[result] = =Array([location] = =Array([LNG] =113.32778195925[Lat] =23.137201991056) [formatted_address] = Guangzhou Tianhe District sports West Heng Jie, Guangdong province *No. [Business] = Tianhe City, sports Center, Shahe [Addresscomponent] =Array([City] = Guangzhou [Country] = China [direction] = near [Distance] = -[district] = Tianhe District [province] = Guangdong Province [Street] + sports West Heng Jie [s Treet_number] = *Number [Country_code] =0) [Poiregions] =Array() [sematic_description] = Red Shield building North -m [Citycode] =257))
SOURCE Download Address: Click to view
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the PHP based on geographical coordinates to obtain the country, province, city, and surrounding data classes, including the content, I hope to be interested in the PHP tutorial friends helpful.