Php obtains the country, province, city, and surrounding data types based on geographic coordinates.

Source: Internet
Author: User
: This article describes how to use php to obtain data from countries, provinces, cities, and surrounding areas based on geographical coordinates. For more information about PHP tutorials, see. Function: when the App obtains the geographical coordinates of a user, it can know the data of the user in that country, province, city, and surrounding area based on the coordinates.

Principle: based on the Baidu Geocoding API, you need to register a Baidu developer first, and then apply for a Baidu AK (key)
Geocoding. class. php


  $ Ak, 'location' => implode (',', array ($ latitude, $ longpolling), '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 request address * @ param Array $ param request parameter * @ return JSON */privatestaticfunctiontoCurl ($ url, $ param = array ()) {$ Ch = curl_init (); if (substr ($ url, 0,5) = 'https') {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false ); // skip the certificate check curl_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


  '; Print_r ($ result); echo'
';?>


Return example:

Array ([status] => 0 [result] => Array ([location] => Array ([lng] => 113.32778195925 [lat] => 23.137201991056) [formatted_address] => No. 35, Xiheng Street, sports, Tianhe district, Guangzhou, Guangdong, China [business] => Tianhe city, sports center, shahe [addressComponent] => Array ([city] => Guangzhou [country] => China [direction] => nearby [distance] => 14 [district] => Tianhe district [province] => Guangdong [street] => sports West Heng street [street_number] => 35 [country_code] => 0) [poiRegions] => Array () [sematic_description] => 58 meters north of Hongyi building [cityCode] => 257 ))


Source code: Click to view

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes how to use php to obtain data from countries, provinces, cities, and surrounding areas based on geographical coordinates, including related content. I hope to help my friends who are interested in PHP tutorials.

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.