Get country, province, city, and peripheral data by using PHP 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

<?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 peripheral data by address * Private Tocurl using Curl call Baidu Geocoding API */class Geocoding {//Baidu Geocoding API Co    NST API = ' http://api.map.baidu.com/geocoder/v2/';    Do not display peripheral data const No_pois = 0;     Display peripheral Data Const POIS = 1;  /** * Base site for country, province, city and surrounding data * @param String $ak Baidu AK (Key) * @param Decimal $longitude Longitude * @param Decimal $latitude latitude * @param Int $pois Show Peripheral Data * @return Array */public static function GetAd                Dresscomponent ($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 invoke the Baidu geocoding API * @param String $url The requested address * @param Array $param The requested parameter * @return        JSON */private static function Tocurl ($url, $param =array ()) {$ch = Curl_init (); if (substr ($url, 0,5) = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifypeer, false);//Skip certificate Check Curl_setop  T ($ch, Curlopt_ssl_verifyhost, true);        Check the existence of the SSL encryption algorithm 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)) {return false;        } curl_close ($ch);    return $response; }}?>


demo.php

<?phprequire "Geocoding.class.php"; $ak = ' Write your Baidu AK here '; $longitude = 113.327782; $latitude = 23.137202; $result = Geocoding::getaddresscomponent ($ak, $longitude, $latitude, Geocoding::no_pois), Echo ' <pre> ';p rint_r ($result) ; Echo ' </pre> ';? >


Example returns:

array ([status] = 0 [result] = = Array ([location] =& Gt                Array ([LNG] = 113.32778195925 [lat] = 23.137201991056 ) [formatted_address] = 35th West Heng Road, Guangzhou Tianhe District, Guangdong Province [Business] = Tianhe City, sports Center, Shahe [add                    Resscomponent] = Array ([City] = Guangzhou [country] = China                    [Direction] = near [distance] [district] = Tianhe District                    [Province] = Guangdong Province [Street] + sports West Heng Jie [street_number] = 35th                [Country_code] = 0) [Poiregions] = = Array ( ) [sematic_description] = Red Shield building North 58 m [Citycode] = 257)) 

This article explains the use of PHP according to geographical coordinates to obtain the country, province, city, and surrounding data categories, more relevant content please pay attention to the PHP Chinese web.

Related recommendations:

How to use the Glob method to traverse all files under a folder

A brief explanation of the comparison method of PHP floating-point numbers

Explanation of how to export query results to CSV method via MySQL

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.