PHP gets the coordinates that iOS or Android pass through the header (header), obtains the specific city and address through the Baidu interface, and deposits it into the session.

Source: Internet
Author: User

First, encapsulate a method for getting header header files in the Function.php method file.

if (!function_exists (' getallheaders ')) {
function Getallheaders () {
$headers = Array ();
foreach ($_server as $name = = $value) {
if (substr ($name, 0, 5) = = ' Http_ ') {
$headers [Str_replace (', '-'), Ucwords (Strtolower (Str_replace (' _ ', ', ' substr ($name, 5))])] = $value;
}
}
return $headers;
}
}

Then, call the method in the corresponding controller to get the data, first check the session there is no location information.

if (!$_session[' mylocation ')
{
Get positional coordinates
$location = Getallheaders ();
$this->get_location ($location); Calling the encapsulated method
}

Finally, a method is encapsulated to request that the Baidu interface return specific location information and coexist in the session.

Public Function get_location ($location)
{
$lat  = $location [' Latitude '];
$LNG = $location [' Longitude '];

if ($lat && $lng)
{
$url = ' Http://api.map.baidu.com/geocoder/v2/?ak=5BFNbSgnVF5g2O72NpvT Dxfm&location= '. $lat. ‘,‘ . $lng. ' &output=json&pois=1 ';
$html = Json_decode (file_get_contents ($url), true);

if ($html)
{
$myLocation = array (
' city '   = $html [' result '] [' ADDRESSC Omponent ' [' City '],
' addr ' =  isset ($html [' Result '] [' addresscomponent '] [' street '])? $html [' Result '] [ ' Addresscomponent ' [' Street ']: $html [' result '] [' formatted_address '],
' LNG '    => $html [' Result '] [' Location '] [' LNG '],
' lat '     = $html [' result '] [location '] [lat '],
);

$_session[' mylocation '] = $myLocation;
}
}
}

PHP obtains the coordinates that iOS or Android pass through the header (header), obtains the specific city and address through the Baidu interface, and deposits it into the session.

Related Article

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.