通过经纬度获取地理位置

public function positioning(){
     
        if(IS_POST){
     
            header("Content-type:text/html;charset=utf-8"); 
            
            //维度
            $lat=I('post.lat');
            
            //经度
            $lng=I('post.lng'); 
            

            // $lat = '38.03';
            // $lng = '114.48';
            $str = "http://api.map.baidu.com/geocoder?location=$lat,$lng&output=xml&key=28bcdd84fae25699606ffad27f8da77b";  
            $xml = file_get_contents($str);  
              
            $data=$this->toArray($xml);
             // dump($data['result']);die;
            $area=$data['result']['formatted_address'];
            $address=$data['result']['addressComponent']['city'];
        }
    }
public function toArray($xml){
        
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
        return $result;
    }

你可能感兴趣的:(通过经纬度获取地理位置)