zoukankan      html  css  js  c++  java
  • 百度地图用ip获取当前位置的经纬度(高精度)

    步骤比较简单
    先上百度地图API官网,申请一个应用AK(访问凭据);查看一下高进度定位的API,看看是否都符合要求
    下面直接上代码
    /**
     * 根据ip获取地理坐标
     * @param ip
     * @return
     */
    public JSONObject getCoorsByIp(String ip){
       if (null == ip) {
          ip = "";
       }
       try {
          URL url = new URL("http://api.map.baidu.com/highacciploc/v1?qcip="+ip+
                "&qterm=pc&ak="+*********+"&coord=bd09ll");
          InputStream inputStream = url.openStream();
          InputStreamReader inputReader = new InputStreamReader(inputStream);
          BufferedReader reader = new BufferedReader(inputReader);
          String results=reader.readLine();
          if(!StringUtils.hasText(results)){
             return null;
          }
          JSONObject resultsJson = JSONObject.fromObject(results); //返回值为标准json格式
          JSONObject resultJson = JSONObject.fromObject(resultsJson.get("result"));
          String result = resultJson.get("error").toString();
          if(!result.equals("161")){
             logger.info("根据ip获取经纬度失败!");
             return null;
          }
          JSONObject contentJson = JSONObject.fromObject(resultsJson.get("content"));
          JSONObject coorJson = JSONObject.fromObject(contentJson.get("location"));
          return coorJson;
       } catch (MalformedURLException e) {
          e.printStackTrace();
       } catch (IOException e) {
          e.printStackTrace();
       }
       return null;
    }
    个人感觉百度的高进度定位还是不太准确,只能是定位大致区域

    还有一种是普通定位,无非请求的url不一致,其他的都差不多

  • 相关阅读:
    yanghui杨辉三角--(一维数组
    yanghui杨辉三角--(一维数组)探索1 2
    yanghui杨辉三角--(二维数组
    Fiber VS Coroutine VS Green Thread
    Java8-Reference
    Boolean
    Java-相等
    java.lang.Cloneable
    java.lang.CharSequence
    java.lang.AutoCloseable
  • 原文地址:https://www.cnblogs.com/leinuo2016/p/5853134.html
Copyright © 2011-2022 走看看