• Java后台通过传参得到对应的坐标值的方法


    public List<String> getMap(String address, String city) {
    String returnXml="";
    List<String> list=new ArrayList<String>();
    try {
    address=URLEncoder.encode(address, "UTF-8");
    city=URLEncoder.encode(city, "UTF-8");
    String urls="http://api.map.baidu.com/geocoder/v2/?ak=07dbeabb4d734364eea7f2eb49eb472b&callback=renderOption&output=xml&address="+address+"&city="+city;
    URL url =new URL(urls);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setConnectTimeout(5*1000);
    conn.setRequestMethod("GET");

    //解析返回来的xml消息体
    byte[] msgBody = null;
    if (conn.getResponseCode() != 200){
    System.out.println("error");
    }
    InputStream is = (InputStream) conn.getInputStream();//获取返回数据

    byte[] temp = new byte[1024];
    int n = 0;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while ((n = is.read(temp)) != -1)
    {
    bos.write(temp, 0, n);
    }
    msgBody = bos.toByteArray();
    bos.close();
    is.close();
    returnXml= new String(msgBody, "UTF-8").trim();
    conn.disconnect();

    // 以下解析XML
    System.out.println(returnXml);
    Map<String, String> map=new HashMap<String, String>();
    SAXReader reader=new SAXReader();
    Document document = reader.read(new ByteArrayInputStream(returnXml.getBytes("UTF-8")));
    Element rootelement = document.getRootElement();
    for (Iterator iterator = rootelement.elementIterator(); iterator.hasNext();) {
    Element root = (Element) iterator.next();
    for (Iterator iterator1 =root.elementIterator(); iterator1.hasNext();) {
    Element it = (Element) iterator1.next();
    for (Iterator iterator2 = it.elementIterator(); iterator2.hasNext();) {
    Element it1 = (Element) iterator2.next();
    if(it1.getName()=="lat"){
    String name = it1.getName();
    String text = it1.getTextTrim();
    map.put(name, text);
    }if(it1.getName()=="lng"){
    String name = it1.getName();
    String text = it1.getTextTrim();
    map.put(name, text);
    }
    }
    }
    }
    for (String key : map.keySet()) {
    list.add(map.get(key));
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return list;
    }

    我这个传过来的参数是省(市)和具体地址,返回得到的是一个xml文件。通过解析xml得到相对应的坐标值,返回的还可以是一个json格式的文件,像下面给出的例子:

    private void convertGps2Baidu(List<DBObject> gpsData) {

    String ak = configProperties.getProperty("map_ak");
    // String ak = "07dbeabb4d734364eea7f2eb49eb472b";
    String from = "1";
    String to = "5";

    String url = configProperties.getProperty("map_url");
    // String url = "http://api.map.baidu.com/geoconv/v1/";
    Resty r = new Resty();

    int maxCount = 100;

    int loopCount = gpsData.size() / maxCount;
    if (gpsData.size() % maxCount > 0) {
    loopCount++;
    }

    for (int i = 0; i < loopCount; i++) {
    String gps = "";

    for (int j = 0; j < maxCount; j++) {
    int k = j + (i * maxCount);
    if (k >= gpsData.size()) {
    break;
    }

    DBObject dbObj = gpsData.get(k);
    DBObject gpsObj = (DBObject) dbObj.get("gps");
    gps += gpsObj.get("latitude").toString() + "," + gpsObj.get("longitude").toString() + ";";
    }

    // 去掉最后的分号
    if (gps.length() > 0) {
    gps = gps.substring(0, gps.length() - 1);
    }

    JSONResource obj;
    try {
    obj = r.json(url,
    form(data("coords", gps), data("ak", ak), data("from", from), data("to", to)));
    int status = (Integer) obj.get("status");
    if (status == 0) {
    JSONArray baiduGps = (JSONArray) obj.get("result");
    for (int j = 0; j < baiduGps.length(); j++) {
    DBObject baiduObj = gpsData.get(j + (i * maxCount));
    JSONObject baiduMap = (JSONObject) baiduGps.get(j);
    DBObject baiduMongoObj = new BasicDBObject();
    baiduMongoObj.put("x", baiduMap.get("x"));
    baiduMongoObj.put("y", baiduMap.get("y"));
    baiduMongoObj.put("s", ((DBObject)baiduObj.get("gps")).get("vss"));

    baiduObj.put("b_gps", baiduMongoObj);
    //vehDataDao.saveGpsData(baiduObj);
    }
    } else {
    logger.error("baidu GPS convert Error.... status=" + status);
    logger.error("GPS row string: " + gps);
    }

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    OpenCV 限制对比度 图像增强
    2000 * 1000的图像截取大小为20 * 20
    在opencv3中的机器学习算法练习:对OCR进行分类
    OpenCV KNN加载训练好的模型
    OpenCV KNN数字分类
    vim简易配置
    shell简明笔记
    命令行关闭和开启ubuntu图形界面
    忘记Oracle数据库中sys等密码的解决方法
    Oracle常见练习题(2020082201)
  • 原文地址:https://www.cnblogs.com/SH03/p/5219598.html
Copyright © 2020-2023  润新知