• 根据经纬度坐标定位用户所在城市


    package com.henu.controller;
    
    import java.net.URL;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    public class GetLocation {
        public static void main(String[] args) {
            // lat 39.97646
            // log 116.3039
            String add = getAdd("113.595688", "34.798947");
            JSONObject jsonObject = JSONObject.fromObject(add);
            JSONArray jsonArray = JSONArray.fromObject(jsonObject
                    .getString("addrList"));
            JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));
            String allAdd = j_2.getString("admName");
            String arr[] = allAdd.split(",");
            System.out.println("省:" + arr[0] + "
    市:" + arr[1] + "
    区:" + arr[2]);
            String admCode = j_2.getString("admCode");
            System.out.println(admCode);
        }
    
        public static String getAdd(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);
            return res;
        }
    
    }

    运行结果:

    {"queryLocation":[34.798947,113.595688],"addrList":[{"type":"poi","status":1,"name":"阿登饭庄","id":"ANB017316EI9","admCode":"410102","admName":"河南省,郑州市,中原区,","addr":"","nearestPoint":[113.59577,34.79889],"distance":10.296}]}
    
    省:河南省
    市:郑州市
    区:中原区
    410102
  • 相关阅读:
    Python正课48 —— 匿名函数及其应用
    Python正课47 —— 面向过程编程思想
    Python正课46 —— 二分法
    Python正课45 —— 函数的递归调用
    Python正课44 —— 生成式
    Python正课43 —— 三元表达式
    SQL数据库操作命令大全
    css 高度写死 下滚动时ios 滚动不协调处理
    记录:js删除数组中某一项或几项的几种方法
    本次存储存数组对象
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/7516010.html
Copyright © 2020-2023  润新知