对接高德地图API规划路线

xiaoxiao2021-03-01  48

                                                                     对接高德地图API规划路线

一,首先是去高德官网申请

这边是java服务端调用api接口,所以应该社区web服务的appkey

 

二,然后写几个工具类

a.发送http请求

b.参数对象

c.返回接收对象

httpRequest

package com.gaodeMap.utils;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.URL;

import java.net.URLConnection;

import java.util.List;

import java.util.Map;

 

/**

*

* @author 郑文

*

*/

public class HttpRequest {

/**

* 向指定URL发送GET方法的请求

*

* @param url 发送请求的URL

* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。

* @return URL 所代表远程资源的响应结果

*/

public static String sendGet(String url, String param) {

String result = "";

BufferedReader in = null;

try {

String urlNameString = url + "?" + param;

URL realUrl = new URL(urlNameString);

// 打开和URL之间的连接

URLConnection connection = realUrl.openConnection();

// 设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 建立实际的连接

connection.connect();

// 获取所有响应头字段

Map<String, List<String>> map = connection.getHeaderFields();

// 遍历所有的响应头字段

for (String key : map.keySet()) {

// System.out.println(key + "--->" + map.get(key));

}

// 定义 BufferedReader输入流来读取URL的响应

in = new BufferedReader(new InputStreamReader(

connection.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送GET请求出现异常!" + e);

e.printStackTrace();

}

// 使用finally块来关闭输入流

finally {

try {

if (in != null) {

in.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

return result;

}

 

/**

* 向指定 URL 发送POST方法的请求

*

* @param url 发送请求的 URL

* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。

* @return 所代表远程资源的响应结果

*/

public static String sendPost(String url, String param) {

PrintWriter out = null;

BufferedReader in = null;

String result = "";

try {

URL realUrl = new URL(url);

// 打开和URL之间的连接

URLConnection conn = realUrl.openConnection();

// 设置通用的请求属性

conn.setRequestProperty("accept", "*/*");

conn.setRequestProperty("connection", "Keep-Alive");

conn.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 发送POST请求必须设置如下两行

conn.setDoOutput(true);

conn.setDoInput(true);

// 获取URLConnection对象对应的输出流

out = new PrintWriter(conn.getOutputStream());

// 发送请求参数

out.print(param);

// flush输出流的缓冲

out.flush();

// 定义BufferedReader输入流来读取URL的响应

in = new BufferedReader(

new InputStreamReader(conn.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送 POST 请求出现异常!" + e);

e.printStackTrace();

}

//使用finally块来关闭输出流、输入流

finally {

try {

if (out != null) {

out.close();

}

if (in != null) {

in.close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

return result;

}

}

 

MapNavResults

package com.gaodeMap.utils;

 

/**

*

* @author 郑文

*

*/

public class MapNavResults {

private String distance;//行驶距离

private String duration;//行驶时间(单位:秒)

private String tolls;//道路收费(单位:元)

public String getDistance() {

return distance;

}

public void setDistance(String distance) {

this.distance = distance;

}

public String getDuration() {

return duration;

}

public void setDuration(String duration) {

this.duration = duration;

}

public String getTolls() {

return tolls;

}

public void setTolls(String tolls) {

this.tolls = tolls;

}

@Override

public String toString() {

return "MapNavResults [distance=" + distance + ", duration=" + duration

+ ", tolls=" + tolls + "]";

}

}

 

MapNavUtil

package com.gaodeMap.utils;

 

import org.apache.commons.lang.StringUtils;

 

import com.tjhx.core.util.PropertiesUtils;

import com.yaysyc.ysyc.BussinessConstant;

 

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

 

/**

*

* @author 郑文

*

*/

public class MapNavUtil {

private String startCoordinate;

private String endCoordinate;

private String applicationKey;

private String param;

/**

* 必须要构造参数

* @param startCoordinate 起点经纬度 经度在前,纬度在后

* @param endCoordinate 终点经纬度 经度在前,纬度在后

* @param applicationKey 高德地图应用key,需要Web服务类型的key

*/

public MapNavUtil(String startCoordinate, String endCoordinate,

String applicationKey) {

this.startCoordinate = startCoordinate;

this.endCoordinate = endCoordinate;

this.applicationKey = applicationKey;

String param = "origin="+this.startCoordinate+"&destination="+this.endCoordinate+"&key="+this.applicationKey;

//这里事读取properties文件里配置的规划路线规则,参数详情见后面的补充说明

String strategy = PropertiesUtils.getProperty("planPathRule");

if(StringUtils.isNotBlank(strategy)) {

param += "&strategy=" + strategy;

}

this.param = param;

}

/**

* 获取地图导航返回值

* @return

*/

public MapNavResults getResults(){

String apiUrl = PropertiesUtils.getProperty(BussinessConstant.GAODE_DRIVER_API_URL);

if(StringUtils.isBlank(apiUrl)) {

apiUrl = "https://restapi.amap.com/v3/direction/driving";

}

String sendGet = HttpRequest.sendGet(apiUrl, param);

JSONObject jsonObject=JSONObject.fromObject(sendGet);

String routeJsonString = jsonObject.get("route").toString();

JSONObject routeObject=JSONObject.fromObject(routeJsonString);

JSONArray jsonArray = routeObject.getJSONArray("paths");

JSONObject zuiJson = jsonArray.getJSONObject(0);

MapNavResults mapResult=new MapNavResults();

mapResult.setDistance(zuiJson.get("distance").toString());

mapResult.setDuration(zuiJson.get("duration").toString());

mapResult.setTolls(zuiJson.get("tolls").toString());

return mapResult;

}

}

 

顺便送上测试类

TestRequest

package com.gaodeMap.utils;

 

/**

*

* @author 郑文

*

*/

public class TestRequest {

public static void main(String[] args) {

String origin="104.07,30.67";//出发点经纬度 -- 四川省成都市青羊区草市街街道锣锅巷40号棉麻大厦

String destination="104.46,29.23";//目的地经纬度 -- 四川省自贡市贡井区五宝镇高林村

String key="yourkey";//高德用户key

MapNavUtil mapResult=new MapNavUtil(origin, destination, key);

System.out.println(mapResult.getResults().toString());

}

}

 

api接口请求的url拼接的参数见官方文档

https://lbs.amap.com/api/webservice/guide/api/direction

 

 

转载请注明原文地址: https://www.6miu.com/read-4150023.html

最新回复(0)