需求
为了进行打车预估价格,就需要高德的路径规划,我的需求是驾车路径规划,通过接口获取到出发地和目的地的路程距离和时间,从而可以计算预估价格
controller层
package com.mashibing.servicevaluation.controller;
//import com.mashibing.commonservice.dto.ResponseResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mashibing.servicevaluation.entity.ValuationRequest;
import com.mashibing.servicevaluation.service.ValuationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
@RestController
public class ValuationController {
@Autowired
ValuationService valuationService;
/**
* 根据生成的订单id进行 预估打车价格计算
*/
@PostMapping("/price/forecast")
public BigDecimal valuation(@RequestBody ValuationRequest valuationRequest) throws JsonProcessingException {
System.out.println("b");
//String startAddress = valuationRequest.getStartAddress();
String startLongitude = valuationRequest.getStartLongitude();
String startLatitude = valuationRequest.getStartLatitude();
//String endAddress = valuationRequest.getEndAddress();
String endLongitude = valuationRequest.getEndLongitude();
String endLatitude = valuationRequest.getEndLatitude();
//String cityCode = valuationRequest.getCityCode();
//Integer carLevel = valuationRequest.getCarLevel();
//Integer serviceType = valuationRequest.getServiceType();
//String deviceCode = valuationRequest.getDeviceCode();
String key = valuationRequest.getKey();
//valuationService.calcForecastPrice(startAddress,startLongitude,startLatitude,endAddress,endLongitude,endLatitude,cityCode,carLevel,serviceType,deviceCode,key);
valuationService.calcForecastPrice(startLongitude,startLatitude,endLongitude,endLatitude,key);
System.out.println("a");
return null;
}
}
service层
package com.mashibing.servicevaluation.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.math.BigDecimal;
public interface ValuationService {
/**
* 计算预估价格
* @return 预估价格
*/
BigDecimal calcForecastPrice(String startLongitude, String startLatitude, String endLongitude, String endLatitude,String key) throws JsonProcessingException;
}
Serviceimpl层
package com.mashibing.servicevaluation.service.impl;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashibing.servicevaluation.service.ValuationService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.client.RestTemplate;
import sun.net.www.http.HttpClient;
import java.math.BigDecimal;
import java.math.BigInteger;
@Service
public class ValuationServiceImpl implements ValuationService {
//https://restapi.amap.com/v3/direction/driving?parameters
//https://restapi.amap.com/v3/direction/driving?origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=xml&key=<用户的key>
@Override
//public BigDecimal calcForecastPrice(String startAddress, String startLongitude, String startLatitude, String endAddress, String endLongitude, String endLatitude, String cityCode, Integer carLevel, Integer serviceType, String deviceCode,String key) {
public BigDecimal calcForecastPrice(String startLongitude, String startLatitude, String endLongitude, String endLatitude,String key) throws JsonProcessingException {
String url = "https://restapi.amap.com/v3/direction/driving?"
+ "origin=" + startLongitude +","+ startLatitude
+ "&destination=" + endLongitude +","+ endLatitude
+ "&extensions=all&output=json"
+ "&key=" + key;
//restTemplate是spring中用来请求url的,getForEntity:返回值包含请求头,getForObject:返回值不包含请求头
RestTemplate restTemplate=new RestTemplate();
//请求url后获取返回值
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
//objectMapper是解析json数据的
ObjectMapper mapper = new ObjectMapper();
//将返回值的数据解析为json数据格式
JsonNode jsonNode = mapper.readTree(response.getBody());
JsonNode route = jsonNode.path("route");
JsonNode paths = route.get("paths");
JsonNode distance2 = paths.path(0).get("distance");
JsonNode duration2 = paths.path(0).get("duration");
//JsonNode distance1 = paths.get(0).get("distance");
//JsonNode jsonNode1 = paths.get(0);
// System.out.println(distance1);
// System.out.println(duration1);
//将字符串转换为json格式
//JSONObject jsonobject = JSONObject.fromObject(route);
//System.out.println("jsonobject: "+jsonobject);
//JSONArray pathArray = jsonobject.getJSONObject("route").getJSONArray("paths");
//String distance = pathArray.getJSONObject(0).getString("distance");
//String duration = pathArray.getJSONObject(1).getString("duration");
//BigDecimal distancePrice = BigDecimal.valueOf(Long.parseLong(distance)).multiply(BigDecimal.valueOf(2));
//BigDecimal durationPrice = BigDecimal.valueOf(Long.parseLong(duration)).divide(BigDecimal.valueOf(60)).multiply(BigDecimal.valueOf(2));
BigDecimal distancePrice = new BigDecimal(distance2.asDouble()).multiply(BigDecimal.valueOf(2));
BigDecimal durationPrice = new BigDecimal(duration2.asDouble()).multiply(BigDecimal.valueOf(2));
return distancePrice.add(durationPrice);
}
}
json解析的工具类有很多,jsonObject、jsonArray、Jackson的ObjectMapper、阿里的json、谷歌的gson等
请求url的工具类有很多、有httpClient、Spring的RestTemplate等
将url请求后返回的数据进行json解析