需求
为了进行打车预估价格,就需要高德的路径规划,我的需求是驾车路径规划,通过接口获取到出发地和目的地的路程距离和时间,从而可以计算预估价格

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 {@AutowiredValuationService 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解析

高德路径规划预估打车价格相关推荐

  1. 2020FME博客大赛——基于FME利用高德路径规划AP实现公共服务设施可达性分析——以厦门山海健康步道为例

    作者:郭文义 单位:厦门市环境科学研究院 等时圈(siochrone),指从某点出发,以某种交通方式在特定时间内能到达的距离覆盖的范围(来自于网络).(An isochrones is an isol ...

  2. 高德地图API路径规划结果清除

    关于高德地图API之路径规划结果的清除(js API) 偶然用到高德地图API做路径规划,在做的过程中发现连续进行不同的路径规划产生的结果是叠加的.如何清除上一次的路径规划呢?开发手册上有一个 cle ...

  3. 高德地图api接口文档_在 R 语言里面调用高德地图接口:地理编码与路径规划

    你知道从广州南站去珠江新城怎么走么?今天就让我们一起使用 R 语言调用高德地图的地理编码(地址转经纬度)接口和路径规划接口来回答这个问题. 准备工作 注册高德地图,创建应用添加 Key(注意申请 Ke ...

  4. python路径规划仿真实验_【python实战】批量获得路径规划——高德地图API

    1.需求 在上篇中,已经批量获得了经纬度信息,现在需要根据经纬度来进行路径规划,想知道两点之间的距离和路程.花费等信息. 这时候就需要用到高德地图API中的路径规划功能了. 2.过程 1. 构造经纬度 ...

  5. vue[高德地图行车路径规划以及路线记录绘制操作]

    最近的一个项目中需要根据需求将地图上画出一条高速公路,然后将这条高速公路的行车轨迹绘制成一条带有颜色路线以便后续插入内容. 看遍了不少高德地图的api内容以及搜索了不少的网上资源,发现可以通过路径规划 ...

  6. mapboxGL和高德API结合实现路径规划

    概述 本文将mapboxGL和高德地图API结合起来,实现路径规划. 效果 实现 高德地图路径规划API说明如上图,有行走.公交.驾车等多种路径,本文以行走为例来说明. 添加点.线图层 map.on( ...

  7. Cesium结合高德SDK路径规划

    Cesium结合高德SDK路径规划 申请高德SDK的使用 链接: 点这里 按官网的提示申请,具体流程这里就不在赘述了. 路径规划API:点这里 注意:其中的sig签名:点这里 ![在这里插入图片描述] ...

  8. 关于调用高德地图路径规划清除问题解决方案【js】

    关于调用高德地图路径规划清除问题解决方案[js] 参考文章: (1)关于调用高德地图路径规划清除问题解决方案[js] (2)https://www.cnblogs.com/qk523/articles ...

  9. 高德地图API武汉某区域的步行路径规划

    高德地图API的武汉某区域的步行路径规划 利用python的requests函数,基于高德地图 网页服务API 使用地理编码化.步行路径规划等开发方法 以武汉武昌.青山交界处以区域为例 做已有出行点至 ...

最新文章

  1. 图像柔光效果(SoftGlow)的原理及其实现。
  2. 想找首歌来表达心情!
  3. 目标检测学习笔记--DSSD算法
  4. 自己实现spring核心功能 三
  5. 【疼逊】致广大QQ用户的一封信
  6. [Jinkey 原创]震惊!iOS 系统居然自带悬浮窗口调试工具
  7. [html] 如何使用纯HTML实现跑马灯的效果?
  8. 陕西省2021年高考成绩结果查询,陕西招生考试信息网:2021年陕西高考成绩查询入口、查分系统...
  9. Exception:No identifier specified for entity
  10. ant design pro 水印(删除、修改)
  11. 来自Carson_Ho大神的最6的Android事件分发机制详解
  12. 浏览器不能调用JAVA打印_网页调用IE浏览器的打印功能
  13. echarts立体柱状图
  14. 逆向分析学习入门教程
  15. 1415-2结对分组情况
  16. dell r720xd 裸机配置系列 3 配置网络
  17. MySQL5.5版本服务端安装图解
  18. VSCode下载及各种实用插件安装教程
  19. 小程序 wx.showModal
  20. CVPR 2018 | 腾讯AI Lab关注的三大方向与55篇论文

热门文章

  1. 实验1 Bean管理
  2. 自己电脑服务器地址的查询办法
  3. Oauth 第三方授权登陆 facebook google twitter instagram
  4. echarts 官网访问慢
  5. UICollectionView 适配 iPhone 7 Plus
  6. jsonpath学习资料
  7. 解决:Activiti7与SpringBoot整合时,默认生成的activiti数据库中只有17张表,无另外8张历史表
  8. php 信用卡通道api,php – PayPal REST API为信用卡令牌返回500 Server错误
  9. Led台灯对眼睛好吗?2022双十一不伤眼的护眼灯推荐
  10. 重磅!谷歌突然宣布!百度、滴滴懵了