百度地图用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不一致,其他的都差不多

原文地址:https://www.cnblogs.com/leinuo2016/p/5853134.html