PHP gets national, provincial, city, and peripheral data based on geographic coordinates

Source: Internet
Author: User
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.

  • 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.