package com.threeti.cpanel.web.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* 返回信息:
* OK,用于表示相关响应包含一个有效的 result。
* NOT_FOUND,用于表示请求的起点、终点或路标中指定的至少一个位置无法进行地理编码。
* ZERO_RESULTS,用于表示在起点和终点之间找不到路线。
* MAX_WAYPOINTS_EXCEEDED,用于表示请求中提供的 waypoints 过多。允许的最大 waypoints 数为 8,其中包括起点和终点(Google Maps API for Business 客户可以在请求中使用最多 23 个路标)。
* INVALID_REQUEST,用于表示提供的请求无效。
* OVER_QUERY_LIMIT,用于表示该服务在允许的时间段内从您的应用收到的请求过多。
* REQUEST_DENIED,用于表示该服务已拒绝您的应用使用路线服务。
* UNKNOWN_ERROR,用于表示路线请求因服务器出错而无法得到处理。如果您重试一次,该请求可能就会成功。
* @author msguo
*
*/
public class GoogleMapRangeUtil {
private static String RETURN_STATUS_1 = "OK";
private static String RETURN_STATUS_2 = "NOT_FOUND";
private static String RETURN_STATUS_3 = "ZERO_RESULTS";
private static String RETURN_STATUS_4 = "MAX_WAYPOINTS_EXCEEDED";
private static String RETURN_STATUS_5 = "INVALID_REQUEST";
private static String RETURN_STATUS_6 = "OVER_QUERY_LIMIT";
private static String RETURN_STATUS_7 = "REQUEST_DENIED";
private static String RETURN_STATUS_8 = "UNKNOWN_ERROR";
private static String ERROR_MESSAGE_1 = "请检查地址是否正确!";
private static String ERROR_MESSAGE_2 = "距离计算失败!";
public String getRange(String houseAddr,String collegeAddr){
String origin = null;
String destination = null;
String rangeStr = null;
try {
origin = java.net.URLEncoder.encode(houseAddr, "UTF-8");
destination = java.net.URLEncoder.encode(collegeAddr, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return ERROR_MESSAGE_2;
}
// String key = "zh-CN";
// String url = String.format("http://maps.googleapis.com/maps/api/directions/json?origin=%s&sensor=false&destination=%s&language=%s", origin, destination, key);
String url = String.format("http://maps.googleapis.com/maps/api/directions/json?origin=%s&sensor=false&destination=%s", origin, destination);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader bf = new BufferedReader(insr);
StringBuffer sBuffer = new StringBuffer();
String data = "";
while ((data = bf.readLine()) != null) {
sBuffer.append(data + "\r\n");
}
insr.close();
JSONObject jsonObject = new JSONObject(sBuffer.toString());
if (RETURN_STATUS_1.equals(jsonObject.get("status"))) {
JSONArray jsonArray = jsonObject.getJSONArray("routes");
JSONObject jb = (JSONObject) jsonArray.get(0);
JSONObject legs = jb.getJSONArray("legs").getJSONObject(0);
rangeStr = legs.getJSONObject("distance").getString("text");
}else if(RETURN_STATUS_2.equals(jsonObject.get("status")) || RETURN_STATUS_3.equals(jsonObject.get("status"))){
return ERROR_MESSAGE_1;
}else {
return ERROR_MESSAGE_2;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return rangeStr;
}
}
googleMap 计算两个地址的距离
最新推荐文章于 2020-12-24 02:59:11 发布