用户端在小程序认证通过后会自动进行定位,也可以在首页手动定位,定位成功后用户在查询家政服务项目时会根据定位的城市查询该城市有哪些服务项目。
高德地图配置
小程序端的定位是通过手机的定位模块进行定位,定位成功获取经纬度坐标,平台根据经纬度坐标请求地图服务获取经纬度坐标对应的具体位置。
小程序首先通过微信提供的方法拿到经纬度坐标,然后请求后端获取具体的位置,后端会请求高德地图根据经纬度获取具体的城市信息(也就是citycode),然后与数据库中存在的citycode作比较来确定,该地区是否开通服务,高德地图会提供一个citycode与城市对应的Excel表。
要测试用户端定位的流程首先需要在高德地图开通地图定位服务。
参考“高德地图web服务配置文档” 获取访问接口的key。
进入nacos配置jzo2o-publics.yml中高德地图key。
前端传递的参数这这个 中间养了个%2C 拼接
但是传递到了后端就解析成了
public MapLocationDTO getCityCodeByLocation(String location) {
Map<String, Object> params = new HashMap();
params.put("location", location);
params.put("key", this.amapProperties.getKey());
String jsonStr = HttpRequest.get("https://restapi.amap.com/v3/geocode/regeo?").form(params).execute().body();
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
String cityCode = (String)jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent").get("citycode");
Object province = jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent").get("province");
Object city = jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent").get("city");
Object district = jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent").get("district");
Object fullAddress = jsonObject.getJSONObject("regeocode").get("formatted_address");
return MapLocationDTO.builder().province(ObjectUtil.isEmpty(province) ? null : province.toString()).city(ObjectUtil.isEmpty(city) ? null : city.toString()).district(ObjectUtil.isEmpty(district) ? null : district.toString()).fullAddress(ObjectUtil.isEmpty(fullAddress) ? null : fullAddress.toString()).cityCode(cityCode).build();
}
调用高德的接口,返回结果是个string 字符串,然后把这个字符串解析成 一个JSON对象
最后封装成一个对象返回