Exemplo n.º 1
0
 @Override
 public List<Article> makeArticleList(List<BaiduPlace> placeList, String bd09Lng, String bd09Lat) {
   // 项目的根路径
   String basePath = ValueUtil.PROJECT_ROOT;
   List<Article> list = new ArrayList<Article>();
   BaiduPlace place = null;
   logger.info("组装地点的图文消息");
   for (int i = 0; i < placeList.size(); i++) {
     place = placeList.get(i);
     Article article = new Article();
     article.setTitle(place.getName() + "\n距离约" + place.getDistance() + "米");
     // P1表示用户发送的位置(坐标转换后),p2表示当前POI所在位置
     article.setUrl(
         String.format(
             basePath + "map/route?p1=%s,%s&p2=%s,%s",
             bd09Lng,
             bd09Lat,
             place.getLng(),
             place.getLat()));
     // 将首条图文的图片设置为大图
     if (i == 0) {
       article.setPicUrl(basePath + "images/map_head.jpg");
     } else {
       article.setPicUrl(basePath + "images/map_point.png");
     }
     list.add(article);
   }
   return list;
 }
Exemplo n.º 2
0
  @Override
  public List<BaiduPlace> parsePlaceXml(String xml) {
    List<BaiduPlace> placeList = null;
    try {
      Document document = DocumentHelper.parseText(xml);
      // 得到xml根元素
      Element root = document.getRootElement();
      // 从根元素获取<results>
      Element resultsElement = root.element("results");
      // 从<results>中获取<result>集合
      @SuppressWarnings("unchecked")
      List<Element> resultElementList = resultsElement.elements("result");
      // 判断<result>集合的大小
      if (resultElementList.size() > 0) {
        placeList = new ArrayList<BaiduPlace>();
        // POI名称
        Element nameElement = null;
        // POI地址信息
        Element addressElement = null;
        // POI经纬度坐标
        Element locationElement = null;
        // POI电话信息
        Element telephoneElement = null;
        // POI扩展信息
        Element detailInfoElement = null;
        // 距离中心点的距离
        Element distanceElement = null;
        // 遍历<result>集合
        for (Element resultElement : resultElementList) {
          nameElement = resultElement.element("name");
          addressElement = resultElement.element("address");
          locationElement = resultElement.element("location");
          telephoneElement = resultElement.element("telephone");
          detailInfoElement = resultElement.element("detail_info");

          BaiduPlace place = new BaiduPlace();
          place.setName(nameElement.getText());
          place.setAddress(addressElement.getText());
          place.setLng(locationElement.element("lng").getText());
          place.setLat(locationElement.element("lat").getText());
          // 当<telephone>元素存在时获取电话号码
          if (null != telephoneElement) {
            place.setTelephone(telephoneElement.getText());
          }
          // 当<detail_info>元素存在时获取<distance>
          if (null != detailInfoElement) {
            distanceElement = detailInfoElement.element("distance");
            if (null != distanceElement) {
              place.setDistance(Integer.parseInt(distanceElement.getText()));
            }
          }
          placeList.add(place);
        }
        // 按距离由近及远排序
        Collections.sort(placeList);
      }
    } catch (Exception e) {
      logger.info("转换时出现异常:" + e.getLocalizedMessage());
    }
    return placeList;
  }