资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
import java.io.IOException;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.GetMethod;import org.dom4j.Document;import org.dom4j.DocumentException;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 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 = http:/online.mapabc.com/traffic?config=trafficinfo&datatype=0&gpsdata=+ 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 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 (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 右下角纬度* return*/public StringBuffer rectangle(double x1, double y1 , double x2 , double y2) StringBuffer sb = new StringBuffer();try HttpClient httpClient = new HttpClient();String url = http:/online.mapabc.com/traffic?config=rectangle&x1=+x1+&y1=+y1+&x2=+x2+&y2=+y2+&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 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;/* 获取圆 形内的实时路况* param x 中心点经度* param y 中心点纬度* return*/public StringBuffer circle(double x, double y) StringBuffer sb = new StringBuffer();try HttpClient httpClient = new HttpClient();String url = http:/online.mapabc.com/traffic?config=circle&x=+x+&y=+y+&userName=*&pcode=MDAwMDAwMDA=&radius=600;HttpMethod method = new GetMethod(url);byte data = null;try httpClient.executeMethod(method);data = method.getResponseBody();method.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 (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);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号