最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javaweb實現(xiàn)百度GPS定位接口(經(jīng)緯度)

 更新時間:2020年02月10日 14:33:42   作者:Asia1752  
這篇文章主要介紹了javaweb實現(xiàn)百度GPS定位接口(經(jīng)緯度),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

百度web GPS定位(經(jīng)緯度)

注冊賬號及配置地址

http://lbsyun.baidu.com/apiconsole/key

主類 BaiduWebAPI

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.webber.cm.common.util.HttpClient;
import com.webber.cm.common.util.JsonUtil;

public class BaiduWebAPI {

 static Logger logger = Logger.getLogger(BaiduWebAPI.class);

 // 配置地址:http://lbsyun.baidu.com/apiconsole/key
 private static final String APP_ID = "18**********";
 private static final String AK = "XGXnh8tB7e*******************";

 public static void main(String[] args) {
 //BaiduWebAPI.ipLocation("127.0.0.1");
 BaiduWebAPI.gpsLocation("116.840213","39.196272");
 }

 // GPS接口
 public static String gpsLocation(String lng, String lat) {
 String result = null;
 try {
  String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=MY_AK&output=json&coordtype=wgs84ll&location=LAT_VALUE,LNG_VALUE";
  url = url.replace("MY_AK", AK).replace("LNG_VALUE", lng).replace("LAT_VALUE", lat);
  String reqResult = HttpClient.doGet(url);
  System.out.println(reqResult);
  Map<String, Object> map = JsonUtil.parseJSON2Map(reqResult);
  Map ac = (Map) ((Map) map.get("result")).get("addressComponent");
  result = ac.get("city").toString() + ac.get("district").toString();
 } catch (Exception e) {
  logger.error("GPS接口異常:", e);
 }
 logger.info("GPS接口:{lng:" + lng + ",lat:" + lat + ",result:" + result + "}");
 return result;
 }

 // IP接口
 public static String ipLocation(String ip) {
 if(BaiduWebAPI.isLan(ip)) {
  return "內(nèi)網(wǎng)IP";
 }
 String result = null;
 try {
  String url = "http://api.map.baidu.com/location/ip?ak=MY_AK&ip=IP_VALUE&coor=bd09ll";
  url = url.replace("MY_AK", AK).replace("IP_VALUE", ip);
  String reqResult = decodeUnicode(HttpClient.doGet(url));
  System.out.println(reqResult);
  Map<String, Object> map = JsonUtil.parseJSON2Map(reqResult);
  result=((Map) map.get("content")).get("address").toString();
  result=result.replace("省", "").replace("市", "");
 } catch (Exception e) {
  logger.error("IP接口異常:", e);
 }
 logger.info("IP接口:{ip:" + ip + ",result:" + result + "}");
 return result;
 }

 // unicode轉(zhuǎn)化漢字
 public static String decodeUnicode(final String unicode) {
 StringBuffer string = new StringBuffer();

 String[] hex = unicode.split("\\\\u");

 for (int i = 0; i < hex.length; i++) {

  try {
  // 漢字范圍 \u4e00-\u9fa5 (中文)
  if (hex[i].length() >= 4) {// 取前四個,判斷是否是漢字
   String chinese = hex[i].substring(0, 4);
   try {
   int chr = Integer.parseInt(chinese, 16);
   boolean isChinese = isChinese((char) chr);
   // 轉(zhuǎn)化成功,判斷是否在 漢字范圍內(nèi)
   if (isChinese) {// 在漢字范圍內(nèi)
    // 追加成string
    string.append((char) chr);
    // 并且追加 后面的字符
    String behindString = hex[i].substring(4);
    string.append(behindString);
   } else {
    string.append(hex[i]);
   }
   } catch (NumberFormatException e1) {
   string.append(hex[i]);
   }

  } else {
   string.append(hex[i]);
  }
  } catch (NumberFormatException e) {
  string.append(hex[i]);
  }
 }
 return string.toString();
 }

 /**
 * 判斷是否為中文字符
 * 
 * @param c
 * @return
 */
 public static boolean isChinese(char c) {
 Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
 if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
  || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
  || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
  || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
  || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
  || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
  return true;
 }
 return false;
 }
 
 // 是否為局域網(wǎng)
 private static Boolean isLan(String ip) {
    if("127.0.0.1".equals(ip)) {
     return true;
    }
    
    if (!StringUtils.isEmpty(ip) && ip.length() > 15) {
      ip = ip.substring(0, ip.indexOf(","));
    }
    /*
     * 判斷客戶單IP地址是否為內(nèi)網(wǎng)地址
     * 內(nèi)網(wǎng)IP網(wǎng)段:
     * 10.0.0.0-10.255.255.255
     * 172.16.0.0-172.31.255.255
     * 192.168.0.0-192.168.255.255
     */
    String reg = "^(192\\.168|172\\.(1[6-9]|2\\d|3[0,1]))(\\.(2[0-4]\\d|25[0-5]|[0,1]?\\d?\\d)){2}$|^10(\\.([2][0-4]\\d|25[0-5]|[0,1]?\\d?\\d)){3}$";
    Pattern p = Pattern.compile(reg);
    Matcher matcher = p.matcher(ip);
    return matcher.find();
  }
}

工具類 HttpClient

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpClient {
 
 public static void main(String[] args) {
 
 }
 
 public static String doGet(String httpurl) {
 HttpURLConnection connection = null;
 InputStream is = null;
 BufferedReader br = null;
 String result = null;// 返回結(jié)果字符串
 try {
  // 創(chuàng)建遠程url連接對象
  URL url = new URL(httpurl);
  // 通過遠程url連接對象打開一個連接,強轉(zhuǎn)成httpURLConnection類
  connection = (HttpURLConnection) url.openConnection();
  // 設置連接方式:get
  connection.setRequestMethod("GET");
  // 設置連接主機服務器的超時時間:15000毫秒
  connection.setConnectTimeout(15000);
  // 設置讀取遠程返回的數(shù)據(jù)時間:60000毫秒
  connection.setReadTimeout(60000);
  // 發(fā)送請求
  connection.connect();
  // 通過connection連接,獲取輸入流
  if (connection.getResponseCode() == 200) {
  is = connection.getInputStream();
  // 封裝輸入流is,并指定字符集
  br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  // 存放數(shù)據(jù)
  StringBuffer sbf = new StringBuffer();
  String temp = null;
  while ((temp = br.readLine()) != null) {
   sbf.append(temp);
   sbf.append("\r\n");
  }
  result = sbf.toString();
  }
 } catch (MalformedURLException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  // 關閉資源
  if (null != br) {
  try {
   br.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  }

  if (null != is) {
  try {
   is.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  }

  connection.disconnect();// 關閉遠程連接
 }

 return result;
 }

 public static String doPost(String httpUrl, String param) {
 HttpURLConnection connection = null;
 InputStream is = null;
 OutputStream os = null;
 BufferedReader br = null;
 String result = null;
 try {
  URL url = new URL(httpUrl);
  // 通過遠程url連接對象打開連接
  connection = (HttpURLConnection) url.openConnection();
  // 設置連接請求方式
  connection.setRequestMethod("POST");
  // 設置連接主機服務器超時時間:15000毫秒
  connection.setConnectTimeout(15000);
  // 設置讀取主機服務器返回數(shù)據(jù)超時時間:60000毫秒
  connection.setReadTimeout(60000);

  // 默認值為:false,當向遠程服務器傳送數(shù)據(jù)/寫數(shù)據(jù)時,需要設置為true
  connection.setDoOutput(true);
  // 默認值為:true,當前向遠程服務讀取數(shù)據(jù)時,設置為true,該參數(shù)可有可無
  connection.setDoInput(true);
  // 設置傳入?yún)?shù)的格式:請求參數(shù)應該是 name1=value1&name2=value2 的形式。
  connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  // 設置鑒權(quán)信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
  connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  // 通過連接對象獲取一個輸出流
  os = connection.getOutputStream();
  // 通過輸出流對象將參數(shù)寫出去/傳輸出去,它是通過字節(jié)數(shù)組寫出的
  os.write(param.getBytes());
  // 通過連接對象獲取一個輸入流,向遠程讀取
  if (connection.getResponseCode() == 200) {

  is = connection.getInputStream();
  // 對輸入流對象進行包裝:charset根據(jù)工作項目組的要求來設置
  br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

  StringBuffer sbf = new StringBuffer();
  String temp = null;
  // 循環(huán)遍歷一行一行讀取數(shù)據(jù)
  while ((temp = br.readLine()) != null) {
   sbf.append(temp);
   sbf.append("\r\n");
  }
  result = sbf.toString();
  }
 } catch (MalformedURLException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  // 關閉資源
  if (null != br) {
  try {
   br.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
  if (null != os) {
  try {
   os.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
  if (null != is) {
  try {
   is.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
  // 斷開與遠程地址url的連接
  connection.disconnect();
 }
 return result;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • MyBatis數(shù)組與集合判斷空問題

    MyBatis數(shù)組與集合判斷空問題

    這篇文章主要介紹了MyBatis數(shù)組與集合判斷空問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java Swing中的表格(JTable)和樹(JTree)組件使用實例

    Java Swing中的表格(JTable)和樹(JTree)組件使用實例

    這篇文章主要介紹了Java Swing中的表格(JTable)和樹(JTree)組件使用實例,本文同時講解了表格和樹的基本概念、常用方法、代碼實例,需要的朋友可以參考下
    2014-10-10
  • SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問題解決

    SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問題解決

    這篇文章給大家介紹了Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依賴不兼容問題,文中通過代碼示例和圖文介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • java String 類的一些理解 關于==、equals、null

    java String 類的一些理解 關于==、equals、null

    在對字符串的相等判斷,==判斷的是地址是否相同,equal()判斷的是字符值是否相同。大多數(shù)時候==跟equal()的結(jié)果都是相同的。
    2009-06-06
  • Java 中的字符串替換方法之replace, replaceAll 和 replaceFirst示例詳解

    Java 中的字符串替換方法之replace, replaceAll 和 rep

    在Java中,字符串的替換是一種常見的操作,特別是在處理文本和格式化輸出時,本文將詳細討論這些方法的用法、區(qū)別以及示例,感興趣的朋友一起看看吧
    2024-12-12
  • Lombok注解之@SuperBuilder--解決無法builder父類屬性問題

    Lombok注解之@SuperBuilder--解決無法builder父類屬性問題

    這篇文章主要介紹了Lombok注解之@SuperBuilder--解決無法builder父類屬性問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴}

    淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴}

    這篇文章主要介紹了淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴},具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring中的Schedule動態(tài)添加修改定時任務詳解

    Spring中的Schedule動態(tài)添加修改定時任務詳解

    這篇文章主要介紹了Spring中的Schedule動態(tài)添加修改定時任務詳解,可能有人會問,為啥不用Quartz,Quartz自然是非常方便強大的,但不是本篇要講的內(nèi)容,本篇就偏要使用SpringSchedule來實現(xiàn)動態(tài)的cron表達式任務,需要的朋友可以參考下
    2023-11-11
  • Java?獲取Word中所有的插入和刪除修訂的方法

    Java?獲取Word中所有的插入和刪除修訂的方法

    這篇文章主要介紹了Java?獲取Word中所有插入和刪除修訂,在?Word?文檔中啟用跟蹤更改功能后,會記錄文檔中的所有編輯行為,例如插入、刪除、替換和格式更改。對插入或刪除的內(nèi)容,本文介紹獲取方法,需要的朋友可以參考下
    2022-04-04
  • SpringMVC方法返回值多種情況代碼實例

    SpringMVC方法返回值多種情況代碼實例

    這篇文章主要介紹了SpringMVC方法返回值多種情況代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09

最新評論

华容县| 札达县| 郎溪县| 长沙县| 错那县| 页游| 喜德县| 中西区| 菏泽市| 蓝山县| 垦利县| 元江| 连城县| 南平市| 陈巴尔虎旗| 新昌县| 丰都县| 同江市| 黄山市| 铜山县| 信宜市| 洪湖市| 虹口区| 金溪县| 合川市| 宣化县| 乐都县| 白玉县| 措勤县| 榆林市| 望江县| 抚远县| 霍林郭勒市| 温宿县| 策勒县| 资中县| 北碚区| 南安市| 綦江县| 广西| 东阳市|