• 获取地址的经纬度,根据经纬度反查地址


    package com.qlyd.shop.common.utils;

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.HashMap;
    import java.util.Map;


    @Component
    public class TengxunMapUtil {
    private static final Logger logger = LoggerFactory.getLogger(TengxunMapUtil.class);

    /**
    * 腾讯地图通过定位反查详细地址
    * @param latitude
    * @param longitude
    * @return
    */
    public Map<String, String> getAddress(String latitude, String longitude) {
    Map<String, String> map = new HashMap<String, String>();
    String location=latitude+","+longitude;
    String url="http://apis.map.qq.com/ws/geocoder/v1/?location="+location+"&key=AZLBZ-FD7KK-HNBJV-ABCI2-GYLJV-D6FGC&get_poi=1";
    String json = loadJSON(url);
    System.out.println(json);
    try {
    if (json.length() > 0) {
    JSONObject obj = JSONObject.fromObject(json);
    String return_code=obj.get("status").toString();
    if (return_code.equals("0")) {
    String address=obj.getJSONObject("result").getString("address");
    String formatted_addresses=obj.getJSONObject("result").getJSONObject("formatted_addresses").getString("recommend");
    map.put("result","ok");
    map.put("address", address);
    map.put("formatted_addresses", formatted_addresses);
    } else if (return_code.equals("310")) {
    map.put("result", "no");
    String msg="请求参数信息有误";
    logger.debug("310-----------"+msg);
    map.put("msg", msg);
    } else if (return_code.equals("311")){
    map.put("result", "no");
    String msg="Key格式错误";
    logger.debug("311-----------"+msg);
    map.put("msg", msg);
    }else if (return_code.equals("306")){
    map.put("result", "no");
    String msg="请求有护持信息请检查字符串";
    logger.debug("306-----------"+msg);
    map.put("msg", msg);
    }else if (return_code.equals("110")){
    map.put("result", "no");
    String msg="请求来源未被授权";
    logger.debug("110-----------"+msg);
    map.put("msg", msg);
    }
    } else {
    map.put("result", "no");
    map.put("msg", "未获取取到数据");
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    System.out.println(json);
    map.put("result", "no");
    }
    return map;
    }
    /**
    * 获取是去名
    * @param latitude
    * @param longitude
    * @return
    */
    public Map<String, String> getCity(String latitude, String longitude) {
    Map<String, String> map = new HashMap<String, String>();
    String location=latitude+","+longitude;
    String url="http://apis.map.qq.com/ws/geocoder/v1/?location="+location+"&key=AZLBZ-FD7KK-HNBJV-ABCI2-GYLJV-D6FGC&get_poi=1";
    String json = loadJSON(url);
    try {
    if (json.length() > 0) {
    JSONObject obj = JSONObject.fromObject(json);
    String return_code=obj.get("status").toString();
    if (return_code.equals("0")) {
    String city=obj.getJSONObject("result").getJSONObject("address_component").getString("city");
    map.put("result","ok");
    map.put("city", city);
    } else if (return_code.equals("310")) {
    map.put("result", "no");
    String msg="请求参数信息有误";
    logger.debug("310-----------"+msg);
    map.put("msg", msg);
    } else if (return_code.equals("311")){
    map.put("result", "no");
    String msg="Key格式错误";
    logger.debug("311-----------"+msg);
    map.put("msg", msg);
    }else if (return_code.equals("306")){
    map.put("result", "no");
    String msg="请求有护持信息请检查字符串";
    logger.debug("306-----------"+msg);
    map.put("msg", msg);
    }else if (return_code.equals("110")){
    map.put("result", "no");
    String msg="请求来源未被授权";
    logger.debug("110-----------"+msg);
    map.put("msg", msg);
    }
    } else {
    map.put("result", "no");
    map.put("msg", "未获取取到数据");
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    System.out.println(json);
    map.put("result", "no");
    }
    return map;
    }


    public static String loadJSON(String url) {
    StringBuilder json = new StringBuilder();
    try {
    URL oracle = new URL(url);
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine = null;
    while ((inputLine = in.readLine()) != null) {
    json.append(inputLine);
    }
    in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    return json.toString();
    }


    //java根据经纬度获取地址
    public String getLocation(String log, String lat) {
    // lat 39.97646
    //log 116.3039
    String add = getAdd(log, lat);
    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]);
    return "省:"+arr[0]+" 市:"+arr[1]+" 区:"+arr[2];

    }

    private 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";
    JSONObject salerJson=JSONObject.fromObject(getUrl(urlString));
    JSONObject jsonData=JSONObject.fromObject(salerJson);
    /* JSONArray fileItemsjson = JSONArray.fromObject(jsonData.get("addrList").toString());
    System.out.println("地区"+fileItemsjson);
    JSONObject job = fileItemsjson.getJSONObject(0);*/
    return jsonData.toString();

    }
    private static String getUrl(String url) {
    String resData = null;
    StringBuffer s = new StringBuffer();
    BufferedReader bReader = null;
    try {
    //114.55.248.182
    URL urlWeb = new URL(url);
    URLConnection connection = urlWeb.openConnection();
    bReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
    while (null != (resData = bReader.readLine())) {
    s.append(resData);
    }
    bReader.close();
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    System.out.println(s);
    return s.toString();
    }



    }
  • 相关阅读:
    浅析CString内部实现机制
    ...sourceannotations.h(142) : error C3094: “repeatable”: 不允许匿名使用
    非MFC项目使用CString及如何打印
    GetTextExtentPoint32--获取字符串在屏幕上长度
    窄字符与宽字符相关的操作
    如何给图片添加黑色边框
    react native
    礼仪 习俗 文化
    职业 行业 2 博客
    读书 文摘 笔记 2 人生的支柱
  • 原文地址:https://www.cnblogs.com/sunsing123/p/9719225.html
Copyright © 2020-2023  润新知