直接上代码
<?php /* * @Author: he4966 */ namespace app\api\controller; use app\common\controller\Api; use think\Db; /** * 高德地图 */ class Amap extends Api { // 无需登录的接口,*表示全部 protected $noNeedLogin = ['*']; // 无需鉴权的接口,*表示全部 protected $noNeedRight = ['*']; /** * 更新省市区县数据 */ public function update() { //接口文档 https://lbs.amap.com/api/webservice/guide/api/district/ $apiKey = 'qq496631085';//小和 你的高德key //仅供测试使用55dcde0dabc8bb324ac286152e9f9d63 $geocodeUrl = 'https://restapi.amap.com/v3/config/district?key=' . $apiKey . '&subdistrict=3'; $geocodeResult = file_get_contents($geocodeUrl); file_put_contents('tes2.map', $geocodeResult); // // $geocodeResult = file_get_contents('tes2.map');//建议第二次后面用本地文件 来调试 $geocodeData = json_decode($geocodeResult, true); // halt($geocodeData); // halt($geocodeData['districts'][0]['districts'][0]['districts'][0]['districts']); $省列表 = $geocodeData['districts'][0]['districts']; $this->save省($省列表); } protected function save省($list) { // CREATE TABLE `fa_area` ( // `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', // `pid` int(10) DEFAULT NULL COMMENT '父id', // `shortname` varchar(100) DEFAULT NULL COMMENT '简称', // `name` varchar(100) DEFAULT NULL COMMENT '名称', // `mergename` varchar(255) DEFAULT NULL COMMENT '全称', // `level` tinyint(4) DEFAULT NULL COMMENT '层级:1=省,2=市,3=区/县', // `pinyin` varchar(100) DEFAULT NULL COMMENT '拼音', // `code` varchar(100) DEFAULT NULL COMMENT '长途区号', // `zip` varchar(100) DEFAULT NULL COMMENT '邮编', // `first` varchar(50) DEFAULT NULL COMMENT '首字母', // `lng` varchar(100) DEFAULT NULL COMMENT '经度', // `lat` varchar(100) DEFAULT NULL COMMENT '纬度', // PRIMARY KEY (`id`), // KEY `pid` (`pid`) // ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='地区表'; foreach ($list as $k => $省) { unset($data); $data['name'] = $省['name']; $data['pid'] = 0; $data['level'] = 1; $res = Db::name('area')->where($data)->find(); if ($res) { //修改信息 // $update['pinyin'] = \fast\Pinyin::get($省['name']); // $update['first'] = \fast\Pinyin::get($省['name'],true); // Db::name('area')->where('id',$res['id'])->update($update); $data = $res; } else { $data['mergename'] = $省['name']; $data['adcode'] = $省['adcode']; $center = explode(',', $省['center']); $data['lng'] = $center[0]; $data['lat'] = $center[1]; $data['pinyin'] = \fast\Pinyin::get($省['name']); $data['first'] = \fast\Pinyin::get($省['name'], true); $data['id'] = Db::name('area')->insertGetId($data); } $this->save市($省['districts'], $data['id'], $data); } } protected function save市($list, $pid, $省 = null) { foreach ($list as $key => $市) { unset($data); $data['name'] = $市['name']; $data['pid'] = $pid; $data['level'] = 2; $res = Db::name('area')->where($data)->find(); if ($res) { //修改信息 // $update['pinyin'] = \fast\Pinyin::get($市['name']); // $update['first'] = \fast\Pinyin::get($市['name'], true); // Db::name('area')->where('id', $res['id'])->update($update); $data = $res; } else { $data['code'] = $市['citycode']; $data['mergename'] = $省['mergename'] . $市['name']; $center = explode(',', $市['center']); $data['lng'] = $center[0]; $data['lat'] = $center[1]; $data['adcode'] = $市['adcode']; $data['pinyin'] = \fast\Pinyin::get($市['name']); $data['first'] = \fast\Pinyin::get($市['name'], true); $data['id'] = Db::name('area')->insertGetId($data); } $this->save区($市['districts'], $data['id'], $data); } } protected function save区($list, $pid, $市 = null) { foreach ($list as $key => $区) { unset($data); $data['name'] = $区['name']; $data['pid'] = $pid; $data['level'] = 3; $res = Db::name('area')->where($data)->find(); if ($res) { //修改信息 // $update['code'] = $区['citycode']; // $update['pinyin'] = \fast\Pinyin::get($区['name']); // $update['first'] = \fast\Pinyin::get($区['name'], true); // Db::name('area')->where('id', $res['id'])->update($update); $data = $res; } else { $data['mergename'] = $市['mergename'] . $区['name']; $center = explode(',', $区['center']); $data['lng'] = $center[0]; $data['lat'] = $center[1]; $data['adcode'] = $区['adcode']; $data['code'] = $区['citycode']; $data['pinyin'] = \fast\Pinyin::get($区['name']); $data['first'] = \fast\Pinyin::get($区['name'], true); $data['id'] = Db::name('area')->insertGetId($data); } } } }
<?php /* * @Author: he4966 */ namespace app\api\controller; use app\common\controller\Api; use think\Db; /** * 高德地图 */ class Amap extends Api { // 无需登录的接口,*表示全部 protected $noNeedLogin = ['*']; // 无需鉴权的接口,*表示全部 protected $noNeedRight = ['*']; /** * 更新省市区县数据 */ public function update() { //接口文档 https://lbs.amap.com/api/webservice/guide/api/district/
$apiKey = 'qq496631085';//小和 你的高德key
//仅供测试使用55dcde0dabc8bb324ac286152e9f9d63
$geocodeUrl = 'https://restapi.amap.com/v3/config/district?key=' . $apiKey . '&subdistrict=3'; $geocodeResult = file_get_contents($geocodeUrl); file_put_contents('tes2.map', $geocodeResult); // // $geocodeResult = file_get_contents('tes2.map');//建议第二次后面用本地文件 来调试 $geocodeData = json_decode($geocodeResult, true); // halt($geocodeData); // halt($geocodeData['districts'][0]['districts'][0]['districts'][0]['districts']); $省列表 = $geocodeData['districts'][0]['districts']; $this->save省($省列表); } protected function save省($list) { // CREATE TABLE `fa_area` ( // `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', // `pid` int(10) DEFAULT NULL COMMENT '父id', // `shortname` varchar(100) DEFAULT NULL COMMENT '简称', // `name` varchar(100) DEFAULT NULL COMMENT '名称', // `mergename` varchar(255) DEFAULT NULL COMMENT '全称', // `level` tinyint(4) DEFAULT NULL COMMENT '层级:1=省,2=市,3=区/县', // `pinyin` varchar(100) DEFAULT NULL COMMENT '拼音', // `code` varchar(100) DEFAULT NULL COMMENT '长途区号', // `zip` varchar(100) DEFAULT NULL COMMENT '邮编', // `first` varchar(50) DEFAULT NULL COMMENT '首字母', // `lng` varchar(100) DEFAULT NULL COMMENT '经度', // `lat` varchar(100) DEFAULT NULL COMMENT '纬度', // PRIMARY KEY (`id`), // KEY `pid` (`pid`) // ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='地区表'; foreach ($list as $k => $省) { unset($data); $data['name'] = $省['name']; $data['pid'] = 0; $data['level'] = 1; $res = Db::name('area')->where($data)->find(); if ($res) { //修改信息 // $update['pinyin'] = \fast\Pinyin::get($省['name']); // $update['first'] = \fast\Pinyin::get($省['name'],true); // Db::name('area')->where('id',$res['id'])->update($update); $data = $res; } else { $data['mergename'] = $省['name']; $data['adcode'] = $省['adcode']; $center = explode(',', $省['center']); $data['lng'] = $center[0]; $data['lat'] = $center[1]; $data['pinyin'] = \fast\Pinyin::get($省['name']); $data['first'] = \fast\Pinyin::get($省['name'], true); $data['id'] = Db::name('area')->insertGetId($data); } $this->save市($省['districts'], $data['id'], $data); } } protected function save市($list, $pid, $省 = null) { foreach ($list as $key => $市) { unset($data); $data['name'] = $市['name']; $data['pid'] = $pid; $data['level'] = 2; $res = Db::name('area')->where($data)->find(); if ($res) { //修改信息 // $update['pinyin'] = \fast\Pinyin::get($市['name']); // $update['first'] = \fast\Pinyin::get($市['name'], true); // Db::name('area')->where('id', $res['id'])->update($update); $data = $res; } else { $data['code'] = $市['citycode']; $data['mergename'] = $省['mergename'] . $市['name']; $center = explode(',', $市['center']); $data['lng'] = $center[0]; $data['lat'] = $center[1]; $data['adcode'] = $市['adcode']; $data['pinyin'] = \fast\Pinyin::get($市['name']); $data['first'] = \fast\Pinyin::get($市['name'], true); $data['id'] = Db::name('area')->insertGetId($data); } $this->save区($市['districts'], $data['id'], $data); } } protected function save区($list, $pid, $市 = null) { foreach ($list as $key => $区) { unset($data); $data['name'] = $区['name']; $data['pid'] = $pid; $data['level'] = 3; $res = Db::name('area')->where($data)->find(); if ($res) { //修改信息 // $update['code'] = $区['citycode']; // $update['pinyin'] = \fast\Pinyin::get($区['name']); // $update['first'] = \fast\Pinyin::get($区['name'], true); // Db::name('area')->where('id', $res['id'])->update($update); $data = $res; } else { $data['mergename'] = $市['mergename'] . $区['name']; $center = explode(',', $区['center']); $data['lng'] = $center[0]; $data['lat'] = $center[1]; $data['adcode'] = $区['adcode']; $data['code'] = $区['citycode']; $data['pinyin'] = \fast\Pinyin::get($区['name']); $data['first'] = \fast\Pinyin::get($区['name'], true); $data['id'] = Db::name('area')->insertGetId($data); } } } }