• java根据GPS(经纬度)获取地理位置


    package cn.antiy.weiqing.utils;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import javax.print.DocFlavor;
    import java.net.URL;
    
    public class ProvinceUtils {
    
        public static void main(String[] args) {
            String province = getProvince("114.21", "31.21");
            System.out.println(province);
        }
    
        public static String getProvince(String log, String lat ){
            //lat 小  log  大
            //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
            String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";
            String res = "";
            try {
                URL url = new URL(urlString);
                java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
                String line;
                while ((line = in.readLine()) != null) {
                    res += line+"
    ";
                }
                in.close();
            } catch (Exception e) {
                System.out.println("error in wapaction,and e is " + e.getMessage());
            }
            System.out.println(res);
            JSONObject jsonObject = JSONObject.parseObject(res);
            JSONArray jsonArray = JSON.parseArray(jsonObject.getString("addrList"));
            JSONObject jsonObject1 = jsonArray.getJSONObject(0);
            String arr[] = jsonObject1.get("admName").toString().split(",");
            System.out.println(arr[0]);
            return arr[0];
        }
    }

    我这边主要是获取省市名称,执行main函数的结果:

    {"queryLocation":[31.21,114.21],"addrList":[{"type":"poi","status":1,"name":"大野冲","id":"ANB02C80NJ66","admCode":"420921","admName":"湖北省,孝感市,孝昌县,","addr":"","nearestPoint":[114.20623,31.20886],"distance":393.859}]}
    
    湖北省
    湖北省
    
    Process finished with exit code 0
  • 相关阅读:
    MySQL效能监控工具mysqlreport安装和中文说明
    mysql的配置文件my.cnf
    MySQL的高可用设计方案的记录
    jQuery 求页面加载等待特效
    如果客户端禁用cookie,session还能使用吗?
    heartbeat重要文件的配置参数说明
    MySQL的索引类型和左前缀索引
    比较一下Linux下的Epoll模型和select模型的区别
    网络防火墙和NAT地址转换
    Postfix+dovecot搭建简单邮箱服务器
  • 原文地址:https://www.cnblogs.com/miaoying/p/7372307.html
Copyright © 2020-2023  润新知