解析高德地图返回的城市信息入库
程序员文章站
2022-11-14 18:33:42
1、解析高德地图返回的城市信息入库提示:目前高德返回的城市信息编码存在部分错误的结果,但城市所属的层级关系是OK的,注意:错误的编码已反馈高德,待高德进行修复。import com.alibaba.fastjson.annotation.JSONField;import lombok.Data;import java.util.List;/** * @author lzl * @date: 2020-11-11 14:56 */@Datapublic class Ci......
1、解析高德地图返回的城市信息入库
提示:目前高德返回的城市信息编码存在部分错误,但城市所属的层级关系是OK的,
注意:错误的编码已反馈高德,待高德进行修复。
高德回复: citycode属于内部既定编码,查询citycode值需要您在官方下载最新城市编码表。 https://lbs.amap.com/api/webservice/download
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.util.List;
/**
* @author lzl
* @date: 2020-11-11 14:56
*/
@Data
public class CityInfoV {
@JSONField(name = "districts")
private List<Districts> districts;
}
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.util.List;
/**
* @author lzl
* @date: 2020-11-11 15:37
*/
@Data
public class Districts {
@JSONField(name = "citycode")
private String cityCode;
@JSONField(name = "adcode")
private String adCode;
private String name;
private String level;
@JSONField(name = "districts")
private List<Districts> districts;
}
import lombok.Data;
/**
* table: city
* @author 90767
* @date 2020-11-11 14:53:36
*/
@Data
public class City {
/**
* id
*/
private Integer id;
/**
* 市名称
*/
private String name;
/**
* 行政区划级别
*/
private String level;
/**
* 城市编码
*/
private String cityCode;
/**
* 区域编码
*/
private String adCode;
/**
* 省-编码
*/
private String proviceCode;
}
import lombok.Data;
/**
* table: district_county
* @author 90767
* @date 2020-11-11 14:54:06
*/
@Data
public class DistrictCounty {
/**
* id
*/
private Integer id;
/**
* 区|县名称
*/
private String name;
/**
* 行政区划级别
*/
private String level;
/**
* 城市编码
*/
private String cityCode;
/**
* 区域编码
*/
private String adCode;
}
import lombok.Data;
/**
* table: street
* @author 90767
* @date 2020-11-11 14:54:23
*/
@Data
public class Street {
/**
* id
*/
private Integer id;
/**
* 街道名称
*/
private String name;
/**
* 行政区划级别
*/
private String level;
/**
* 城市编码
*/
private String cityCode;
/**
* 区域编码
*/
private String adCode;
}
import lombok.Data;
/**
* table: province
* @author 90767
* @date 2020-11-11 14:53:50
*/
@Data
public class Province {
/**
* id
*/
private Integer id;
/**
* 省名称
*/
private String name;
/**
* 行政区划级别
*/
private String level;
/**
* 区域编码
*/
private String adCode;
}
调用高德接口key 需要自己申请。
import com.alibaba.fastjson.JSONObject;
import com.common.response.Result;
import com.common.response.ResultBuilder;
import com.utils.HttpRequestUtils;
import com.web.entity.City;
import com.web.entity.DistrictCounty;
import com.web.entity.Province;
import com.web.entity.Street;
import com.web.response.CityInfoV;
import com.web.response.Districts;
import com.web.response.TestToken;
import com.web.service.inter.CityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@Api(value = "/测试token ",tags = "测试token")
@RestController
@RequestMapping("/testTokenController")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestTokenController {
private final HttpRequestUtils httpRequestUtils;
private final CityInfoService cityInfoService;
@GetMapping("/testCityInfo")
@ApiOperation(value = "解析高德城市信息入库 ",notes = "解析高德城市信息入库 ")
public Result testCityInfo(@RequestParam Integer level){
String url = "https://restapi.amap.com/v3/config/district?keywords=中国&subdistrict=%d&key=";
url = String.format(url,level);
log.info(url);
String resultJSON = httpRequestUtils.httpGet(url,null);
CityInfoV infoV = JSONObject.parseObject(resultJSON,CityInfoV.class);
// 国家列表
List<Districts> countryList = infoV.getDistricts();
// 国家
Districts countryDistricts = countryList.get(0);
List<Province> addProvinceList = new ArrayList<>();
List<City> addCityList = new ArrayList<>();
List<DistrictCounty> addDistrictCountyList = new ArrayList<>();
List<Street> addStreetList = new ArrayList<>();
for(Districts provinceDistricts : countryDistricts.getDistricts()){
// 省
Province province = new Province();
BeanUtils.copyProperties(provinceDistricts,province);
addProvinceList.add(province);
for(Districts cityDistricts : provinceDistricts.getDistricts()){
// 市
City city = new City();
BeanUtils.copyProperties(cityDistricts,city);
city.setProviceCode(provinceDistricts.getAdCode());
addCityList.add(city);
for(Districts districtCountyDistricts : cityDistricts.getDistricts()){
// 区县
DistrictCounty districtCounty = new DistrictCounty();
BeanUtils.copyProperties(districtCountyDistricts,districtCounty);
addDistrictCountyList.add(districtCounty);
for(Districts streetDistricts : districtCountyDistricts.getDistricts()){
// 街道
Street street = new Street();
BeanUtils.copyProperties(streetDistricts,street);
addStreetList.add(street);
}
if(!CollectionUtils.isEmpty(addStreetList)){
cityInfoService.insertStreet(addStreetList);
addStreetList.clear();
}
}
if(!CollectionUtils.isEmpty(addDistrictCountyList)){
cityInfoService.insertDistrictCounty(addDistrictCountyList);
addDistrictCountyList.clear();
}
}
if(!CollectionUtils.isEmpty(addCityList)){
cityInfoService.insertCity(addCityList);
addCityList.clear();
}
}
if(!CollectionUtils.isEmpty(addProvinceList)){
cityInfoService.insertProvince(addProvinceList);
addProvinceList.clear();
}
return ResultBuilder.success();
}
}
本文地址:https://blog.csdn.net/lzl907678041/article/details/109636712
上一篇: 取得单条网站评论以数组形式进行输出