Php obtains coordinates transmitted from the header of ios or android, obtains the specific city and address through the Baidu interface, and saves them to the session .,
First, encapsulate a method to obtain the header file 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 this method in the corresponding controller to obtain data. First, check whether there is any location information in the session.
If (! $ _ SESSION ['mylocation'])
{
// Obtain the positioning Coordinate
$ Location = getallheaders ();
$ This-> get_location ($ location); // call the encapsulated Method
}
Finally, encapsulate a method to request the Baidu interface to return specific location information and coexist in the session.
Public function get_location ($ location)
{
$ Lat = $ location ['latitude '];
$ Lng = $ location ['longyun'];
If ($ lat & $ lng)
{
$ Url = 'HTTP: // api.map.baidu.com/geocoder/v2 /? Ak = 5BFNbSgnVF5g2O72NpvTDxFm & location = '. $ lat.', '. $ lng.' & output = json & pois = 1 ';
$ Html = json_decode (file_get_contents ($ url), true );
If ($ html)
{
$ MyLocation = array (
'City' => $ html ['result'] ['address'] ['city'],
'Addr '=> isset ($ html ['result'] ['address'] ['street'])? $ Html ['result'] ['address'] ['street']: $ html ['result'] ['formatted _ address'],
'Lng '=> $ html ['result'] ['location'] ['lng'],
'Lat' => $ html ['result'] ['location'] ['lat'],
);
$ _ SESSION ['mylocation'] = $ MyLocation;
}
}
}