获取地理位置


    /**
     * 
     * @param unknown $longitude
     * @param unknown $latitude
     * @return {
        "status": "1",
        "regeocode": {
            "addressComponent": { 详细地址信息的数组
                "city": "邯郸市",
                "province": "河北省",
                "adcode": "130426",
                "district": "涉县",
                "towncode": "130426202000",
                "streetNumber": {
                    "number": [],
                    "direction": [],
                    "distance": [],
                    "street": []
                },
                "country": "中国",
                "township": "偏店乡",
                "businessAreas": [
                    []
                ],
                "building": {
                    "name": [],
                    "type": []
                },
                "neighborhood": {
                    "name": [],
                    "type": []
                },
                "citycode": "0310"
            },
            "formatted_address": "河北省邯郸市涉县偏店乡下窑则村" 地址详细信息
        },
        "info": "OK",
        "infocode": "10000"
                        }
     */
    public function address($longitude,$latitude){
        //固定好的key值,用的是高德地图的api接口
        $key="1af36fb65ad9d404a3ada40b150b00c1";
        //根据经纬度获取详细地址
        //$longitude="113.63142";//经度113.799176
        //$latitude="34.75344";//纬度36.659962
        $local="$longitude,$latitude";
        try {
            $data_location=file_get_contents("https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=$local&key=$key");
            $result_local=json_decode($data_location,true);
            //返回数据状态1 为成功 0 为失败
            $local_status=$result_local['status'];
            //返回状态码 10000 为正确 其他为错误
            $local_infocode=$result_local['infocode'];
            if($local_status==1 && $result_local['infocode']== 10000 ){
                //地址信息的数组
//                 $local_regeocode=$result_local['regeocode'];
                //详细地址信息的数组
//                 $addressComponent=$local_regeocode['addressComponent'];
//                 $address['country']=$addressComponent['country'];//国家
//                 $address['province']=$addressComponent['province'];//省份
//                 $address['city']=$addressComponent['city'];//城市
//                 $address['district']=$addressComponent['district'];//区县
//                 $address['adcode']=$addressComponent['adcode'];//地区编码
                return [
                    'code' => ReturnCode::SUCCESS_CODE,
                    'msg' => '获取地址信息成功',
                    'data'=>$result_local['regeocode']
                ];
            }else{
                throw new \Exception($result_local['info']);
            }
        } catch (\Exception $e) {
            return [
                'code' => ReturnCode::ERROR_CODE,
                'msg' => '获取地址信息失败:'.$e->getMessage(),
                'data'=>''
            ];
        }
        return $result_local;
    }



评论0



    0.279468s