腾讯位置服务封装预估里程和时间(Java)
腾讯位置服务的API文档:
https://lbs.qq.com/service/webService/webServiceGuide/webServiceMatrix

@Service
@Slf4j
public class MapServiceImpl implements MapService {
//预估里程的API地址
private String distanceUrl = "https://apis.map.qq.com/ws/distance/v1/matrix/";
@Value("${tencent.map.key}")
private String key;
public HashMap estimateOrderMileageAndMinute(String mode,
String startPlaceLatitude,
String startPlaceLongitude,
String endPlaceLatitude,
String endPlaceLongitude) {
HttpRequest req = new HttpRequest(distanceUrl);
req.form("mode", mode);
req.form("from", startPlaceLatitude + "," + startPlaceLongitude);
req.form("to", endPlaceLatitude + "," + endPlaceLongitude);
req.form("key", key);
HttpResponse resp = req.execute();
JSONObject json = JSONUtil.parseObj(resp.body());
int status = json.getInt("status");
String message = json.getStr("message");
System.out.println(message);
if (status != 0) {
log.error(message);
throw new HxdsException("预估里程异常:" + message);
}
JSONArray rows = json.getJSONObject("result").getJSONArray("rows");
JSONObject element = rows.get(0, JSONObject.class).getJSONArray("elements").get(0, JSONObject.class);
int distance = element.getInt("distance");
String mileage = new BigDecimal(distance).divide(new BigDecimal(1000)).toString();
int duration = element.getInt("duration");
String temp = new BigDecimal(duration).divide(new BigDecimal(60), 0, RoundingMode.CEILING).toString();
int minute = Integer.parseInt(temp);
HashMap map = new HashMap() {{
put("mileage", mileage);
put("minute", minute);
}};
return map;
}
}

浙公网安备 33010602011771号