javaweb实现百度GPS定位接口(经纬度)
百度web GPS定位(经纬度)
成都创新互联公司不只是一家网站建设的网络公司;我们对营销、技术、服务都有自己独特见解,公司采取“创意+综合+营销”一体化的方式为您提供更专业的服务!我们经历的每一步也许不一定是最完美的,但每一步都有值得深思的意义。我们珍视每一份信任,关注我们的成都做网站、成都网站设计质量和服务品质,在得到用户满意的同时,也能得到同行业的专业认可,能够为行业创新发展助力。未来将继续专注于技术创新,服务升级,满足企业一站式全网营销推广需求,让再小的品牌网站建设也能产生价值!
注册账号及配置地址
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); Mapmap = 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 "内网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 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转化汉字 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); // 转化成功,判断是否在 汉字范围内 if (isChinese) {// 在汉字范围内 // 追加成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; } // 是否为局域网 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地址是否为内网地址 * 内网IP网段: * 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;// 返回结果字符串 try { // 创建远程url连接对象 URL url = new URL(httpurl); // 通过远程url连接对象打开一个连接,强转成httpURLConnection类 connection = (HttpURLConnection) url.openConnection(); // 设置连接方式:get connection.setRequestMethod("GET"); // 设置连接主机服务器的超时时间:15000毫秒 connection.setConnectTimeout(15000); // 设置读取远程返回的数据时间:60000毫秒 connection.setReadTimeout(60000); // 发送请求 connection.connect(); // 通过connection连接,获取输入流 if (connection.getResponseCode() == 200) { is = connection.getInputStream(); // 封装输入流is,并指定字符集 br = new BufferedReader(new InputStreamReader(is, "UTF-8")); // 存放数据 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); // 设置读取主机服务器返回数据超时时间:60000毫秒 connection.setReadTimeout(60000); // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true connection.setDoOutput(true); // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无 connection.setDoInput(true); // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0 connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); // 通过连接对象获取一个输出流 os = connection.getOutputStream(); // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 os.write(param.getBytes()); // 通过连接对象获取一个输入流,向远程读取 if (connection.getResponseCode() == 200) { is = connection.getInputStream(); // 对输入流对象进行包装:charset根据工作项目组的要求来设置 br = new BufferedReader(new InputStreamReader(is, "UTF-8")); 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 != 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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
分享标题:javaweb实现百度GPS定位接口(经纬度)
地址分享:http://myzitong.com/article/gpdocp.html