文章目录
- MongoDB数据库
- 引入pom依赖
- 配置yaml配置文件
- 参考POJO
- XLocationRepository
- service服务方法
- 腾讯地图接口
- 申请api key
- 配置api key
- 启动类配置RestTemplate
- 控制层
- 服务层
MongoDB数据库
引入pom依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置yaml配置文件
- 填写mongodb的连接信息,注意数据库需要提前创建
spring:data:mongodb:host: ipport: 27017database: share #指定操作的数据库
参考POJO
@Data
@Schema(description = "x位置")
public class xLocation
{@Schema(description = "id")@Idprivate String id;@Schema(description = "id")private Long xId;@Schema(description = "经纬度")private GeoJsonPoint location;@Schema(description = "创建时间")private Date createTime;
}
XLocationRepository
@Repository
public interface XLocationRepository extends MongoRepository<XLocation, String> {XLocation getByXId(Long xId);
}
service服务方法
@Transactional(rollbackFor = Exception.class)
@Override
public boolean savex(X x) {XLocation xLocation = new XLocation();xLocation.setStationId(x.getId());xLocation.setLocation(new GeoJsonPoint(x.getLongitude().doubleValue(), x.getLatitude().doubleValue()));xLocation.setCreateTime(new Date());xLocationRepository.save(xLocation);return True;
}@Transactional(rollbackFor = Exception.class)
@Override
public boolean updateStation(X x) {String provinceName = regionService.getNameByCode(x.getProvinceCode());String cityName = regionService.getNameByCode(x.getCityCode());String districtName = regionService.getNameByCode(x.getDistrictCode());station.setFullAddress(provinceName + cityName + districtName + station.getAddress());this.updateById(x);StationLocation stationLocation = stationLocationRepository.getByStationId(x.getId());stationLocation.setLocation(new GeoJsonPoint(x.getLongitude().doubleValue(), x.getLatitude().doubleValue()));stationLocationRepository.save(xLocation);return true;
}
腾讯地图接口
申请api key
- 开通地图服务:开通地图服务,首先你要访问腾讯位置服务的官网,然后注册新用户并且登陆。
- 控制台 − > 应用管理 − > 我的应用 − > 创建应用 控制台->应用管理 ->我的应用 ->创建应用 控制台−>应用管理−>我的应用−>创建应用

- 配置应用:创建好应用之后,编辑应用,在弹窗中,把WebService设置成“域名白名单”,然后填写小程序APPID。

- 账号额度:腾讯位置服务给开发者提供免费的调用额度,对于开发者来说是足够用。

配置api key
tencent:map:key: PYOBZ-Y6ZRZ-HMZXP-ZTMES-****-****
启动类配置RestTemplate
@Bean
public RestTemplate restTemplate() {return new RestTemplate();
}
控制层
@Tag(name = "地图接口管理")
@RestController
@RequestMapping("/map")
public class MapController extends BaseController
{@Autowiredprivate IMapService mapService;@Operation(summary = "根据经纬度计算详细地址")@GetMapping("/calculateLatLng/{keyword}")public AjaxResult calculateLatLng(@PathVariable String keyword){return success(mapService.calculateLatLng(keyword));}}
服务层
public interface IMapService {JSONObject calculateLatLng(String keyword);
}
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class MapServiceImpl implements IMapService {@Autowiredprivate RestTemplate restTemplate;@Value("${tencent.map.key}")private String key;@Overridepublic JSONObject calculateLatLng(String keyword) {String url = "https://apis.map.qq.com/ws/geocoder/v1/?address={address}&key={key}";Map<String, String> map = new HashMap<>();map.put("address", keyword);map.put("key", key);JSONObject response = restTemplate.getForObject(url, JSONObject.class, map);if (response.getIntValue("status") != 0) {throw new ServiceException("地图解析异常");}JSONObject result = response.getJSONObject("result");System.out.println(result.toJSONString());return result.getJSONObject("location");}}