《高德地图实时路况对接》由会员分享,可在线阅读,更多相关《高德地图实时路况对接(6页珍藏版)》请在金锄头文库上搜索。
1、import java.io.IOException;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import mons.httpclient.HttpClient;import mons.httpclient.HttpException;import mons.httpclient.HttpMethod;import mons.httpclient.methods.GetMethod;import org.dom4j.Document;import org.dom4j.Docume
2、ntException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/* 对接高德地图实时路况对接* author glf*/public class RealTimeTrafficUtil protected static final Logger LOG = LoggerFactory.getLogger(RealTimeTrafficUtil.class);SimpleDateFormat sdf = new
3、SimpleDateFormat(yyyy-MM-dd HH:mm:ss);/* 获取前方路况播 报* param lat 纬度* param lon 经度* param speed 速度* param heading 角度* return*/public String RealTimeTraffic(String lat, String lon, String speed, String heading) StringBuffer sb = new StringBuffer();try HttpClient httpClient = new HttpClient();String url =
4、 http:/ lat+ ,+ lon+ ,+ speed+ ,+ heading+ ;&flag=12&userName=*&pcode=MDAwMDAwMDA=;HttpMethod method = new GetMethod(url);byte data = null;try httpClient.executeMethod(method);data = method.getResponseBody();method.releaseConnection(); catch (HttpException e) e.printStackTrace(); catch (IOException
5、e) e.printStackTrace();Document doc = null;try String xml = new String(data, UTF-8);doc = DocumentHelper.parseText(xml); catch (DocumentException e) e.printStackTrace();LOG.error(parse xml error.); catch (UnsupportedEncodingException e) e.printStackTrace();Element rootElem = doc.getRootElement();if
6、(0).equals(rootElem.elementText(status) / 没有此路信息,不做 处理String description = rootElem.element(front).elementText(description);sb.append(description); catch (Exception e) return null;sb = jq(sb);return sb.toString();/* 获取矩形范 围内的实时 路况* param x1 左上角经度* param y1 左上角纬度* param x2 右下角经度* param y2 右下角纬度* retu
7、rn*/public StringBuffer rectangle(double x1, double y1 , double x2 , double y2) StringBuffer sb = new StringBuffer();try HttpClient httpClient = new HttpClient();String url = http:/ method = new GetMethod(url);byte data = null;try httpClient.executeMethod(method);data = method.getResponseBody();meth
8、od.releaseConnection(); catch (HttpException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();Document doc = null;try String xml = new String(data, UTF-8);doc = DocumentHelper.parseText(xml); catch (DocumentException e) e.printStackTrace();LOG.error(parse xml error.); catch (Unsuppo
9、rtedEncodingException e) e.printStackTrace();Element rootElem = doc.getRootElement();String description = rootElem.elementText(description);sb.append(description); catch (Exception e) / sb = jq(sb);return sb;/* 获取圆 形内的实时路况* param x 中心点经度* param y 中心点纬度* return*/public StringBuffer circle(double x, d
10、ouble y) StringBuffer sb = new StringBuffer();try HttpClient httpClient = new HttpClient();String url = http:/ method = new GetMethod(url);byte data = null;try httpClient.executeMethod(method);data = method.getResponseBody();method.releaseConnection(); catch (HttpException e) e.printStackTrace(); ca
11、tch (IOException e) e.printStackTrace();Document doc = null;try String xml = new String(data, UTF-8);doc = DocumentHelper.parseText(xml); catch (DocumentException e) e.printStackTrace();LOG.error(parse xml error.); catch (UnsupportedEncodingException e) e.printStackTrace();Element rootElem = doc.getRootElement();String description = rootElem.elementText(description);sb.append(description); catch (Exception e) / sb = jq(sb);return sb;public static void main(String args) RealTimeTrafficUtil line = new RealTimeTrafficUtil();line.RealTimeTraffic(39.944078, 116.355343, 248.710000, 33.050000);