先说思路
先拿元数据测试,
a.所有公交线路信息, allbuslines.json
b.所有公交站点信息 allstation.json
c.实时公交站点信息GPS 数据返回
先读取两个json 文件
然后以key value的形式存在map中
把数据放到map中保存,是因为接口取数据有点慢,特别是这种数据不经常变动的数据。
因为公交线路可能有变动,那就设置个定时器,每天更新一次或者每周更新一次就行,调用初始化方法
下面是测试数据
测试代码
package com.zte.base.home.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import wsdl.taffic.service.ChargePileServicePortTypeProxy; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; import com.zte.base.home.data.bean.Points; /** * * @author emode * */ public class TafficCacheManager { private static final Logger logger = LoggerFactory.getLogger(TafficCacheManager.class); //公交线路 private static HashMap<String, Object> busLineCacheMap = new HashMap<String, Object>(); //公交站点 private static HashMap<String, Object> busStationCacheMap = new HashMap<String, Object>(); private static String LINENAME = "allbusline.json"; private static String STATIONNAME = "allstation.json"; private static String savePath = ConfigReadUtil.getTrafficConfigAsProperties("savePath"); private TafficCacheManager(){ init(); } //初始化写入文件 public static void init(){ /*savePath = ConfigReadUtil.getTrafficConfigAsProperties("savePath"); putJsonFile("1001",LINENAME); putJsonFile("1002",STATIONNAME);*/ initdata(); } //初始化数据 public static void initdata(){ String bus_line_data =getBusInfo("1001"); String bus_station_data = getBusInfo("1002"); Gson gson = new Gson(); if(bus_line_data==null||bus_station_data==null){ } JsonObject bus_line_json = (JsonObject) new JsonParser().parse(bus_line_data); JsonObject bus_station_json = (JsonObject) new JsonParser().parse(bus_station_data); if(bus_line_json.isJsonNull()||bus_station_json.isJsonNull()){ logger.info("数据为空!"); }else{ JsonArray lineArray = bus_line_json.get("DATA").getAsJsonArray(); JsonArray stationArray = bus_station_json.get("DATA").getAsJsonArray(); for(int i=0;i<lineArray.size();i++){ Map<String, Object> map = new HashMap<String, Object>(); JsonObject jobj = lineArray.get(i).getAsJsonObject(); map.put("LINE_NO", jobj.get("LINE_NO").getAsString()); map.put("LINE_NAME", jobj.get("LINE_NAME").getAsString()); //上行线路 List<Points> up_list = new ArrayList<Points>(); JsonElement up_array = jobj.get("UP_POINT"); //获取上行线路、 up_list = gson.fromJson(up_array,new TypeToken<ArrayList<Points>>() {}.getType()); Collections.sort(up_list, new Comparator<Points>() { public int compare(Points arg0, Points arg1) { return arg0.getP_NO().compareTo(arg1.getP_NO()); //正序 } }); //下行线路 List<Points> down_list = new ArrayList<Points>(); JsonElement down_array = jobj.get("DOWN_POINT"); //获取下行线路 down_list = gson.fromJson(down_array,new TypeToken<ArrayList<Points>>() {}.getType()); Collections.sort(down_list, new Comparator<Points>() { public int compare(Points arg0, Points arg1) { return arg0.getP_NO().compareTo(arg1.getP_NO()); //正序 } }); map.put("UP_POINT",gson.toJson(up_list)); map.put("DOWN_POINT", gson.toJson(down_list)); busLineCacheMap.put(jobj.get("LINE_NAME").getAsString(), map); } for(int i=0;i<stationArray.size();i++){ Map<String, Object> map = new HashMap<String, Object>(); JsonObject jobj = stationArray.get(i).getAsJsonObject(); map.put("LINE_NO", jobj.get("LINE_NO").getAsString()); map.put("UP_STATION", gson.toJson(jobj.get("UP_STATION"))); map.put("DOWN_STATION", gson.toJson(jobj.get("DOWN_STATION"))); busStationCacheMap.put(jobj.get("LINE_NO").getAsString(), map); } } } //返回消息 public static String getBusInfo(String msg){ ChargePileServicePortTypeProxy proxy = new ChargePileServicePortTypeProxy(); Map<String , Object> map = new HashMap<String, Object>(); map.put("MSG_NO", msg); String message = null; try { message = proxy.handleMessage(map.toString()); return message; } catch (RemoteException e) { logger.info("网络访问失败! network connection failed"); //e.printStackTrace(); } return message; } //保存json文件 public static void putJsonFile(String msg,String filename){ ChargePileServicePortTypeProxy proxy = new ChargePileServicePortTypeProxy(); Map<String , Object> map = new HashMap<String, Object>(); map.put("MSG_NO", msg); String message; try { message = proxy.handleMessage(map.toString()); File file = new File(savePath+filename); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(message); bw.close(); } catch (RemoteException e) { logger.info("网络访问失败! network connection failed"); } catch (IOException e) { logger.info("不能写入文件!can not create file!"); e.printStackTrace(); } } //通过key值获取 数据 public static Object getbusLineValueByKey(String key){ //当数据为空值时,读取数据一遍 if(busLineCacheMap==null||busLineCacheMap.size()<=0){ initdata(); } Iterator<Entry<String, Object>> it = busLineCacheMap.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String, Object> entry = it.next(); if(key.equals(entry.getKey())){ return entry.getValue(); } } return null; } //通过key值获取 公交站点数据 public static Object getbusStationValueByKey(String key){ if(busStationCacheMap==null||busStationCacheMap.size()<=0){ initdata(); } Iterator<Entry<String, Object>> it = busStationCacheMap.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String, Object> entry = it.next(); if(key.equals(entry.getKey())){ return entry.getValue(); } } return null; } //读取文件 public static String ReadFile(String path) { File file = new File(path); BufferedReader reader = null; String laststr = ""; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); String tempString = null; while ((tempString = reader.readLine()) != null) { laststr = laststr + new String(tempString.getBytes(),"GBK"); } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return laststr; } }
这种数据返回到前端需要重新编码才能绘制到supermap上,具体情况根据服务商提供都数据从而采用什么编码