javaweb实现百度GPS定位接口(经纬度)
百度webGPS定位(经纬度)
注册账号及配置地址
http://lbsyun.baidu.com/apiconsole/key
主类BaiduWebAPI
importjava.util.Map; importjava.util.regex.Matcher; importjava.util.regex.Pattern; importorg.apache.commons.lang.StringUtils; importorg.apache.log4j.Logger; importcom.webber.cm.common.util.HttpClient; importcom.webber.cm.common.util.JsonUtil; publicclassBaiduWebAPI{ staticLoggerlogger=Logger.getLogger(BaiduWebAPI.class); //配置地址:http://lbsyun.baidu.com/apiconsole/key privatestaticfinalStringAPP_ID="18**********"; privatestaticfinalStringAK="XGXnh8tB7e*******************"; publicstaticvoidmain(String[]args){ //BaiduWebAPI.ipLocation("127.0.0.1"); BaiduWebAPI.gpsLocation("116.840213","39.196272"); } //GPS接口 publicstaticStringgpsLocation(Stringlng,Stringlat){ Stringresult=null; try{ Stringurl="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); StringreqResult=HttpClient.doGet(url); System.out.println(reqResult); Mapmap=JsonUtil.parseJSON2Map(reqResult); Mapac=(Map)((Map)map.get("result")).get("addressComponent"); result=ac.get("city").toString()+ac.get("district").toString(); }catch(Exceptione){ logger.error("GPS接口异常:",e); } logger.info("GPS接口:{lng:"+lng+",lat:"+lat+",result:"+result+"}"); returnresult; } //IP接口 publicstaticStringipLocation(Stringip){ if(BaiduWebAPI.isLan(ip)){ return"内网IP"; } Stringresult=null; try{ Stringurl="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); StringreqResult=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(Exceptione){ logger.error("IP接口异常:",e); } logger.info("IP接口:{ip:"+ip+",result:"+result+"}"); returnresult; } //unicode转化汉字 publicstaticStringdecodeUnicode(finalStringunicode){ StringBufferstring=newStringBuffer(); String[]hex=unicode.split("\\\\u"); for(inti=0;i =4){//取前四个,判断是否是汉字 Stringchinese=hex[i].substring(0,4); try{ intchr=Integer.parseInt(chinese,16); booleanisChinese=isChinese((char)chr); //转化成功,判断是否在汉字范围内 if(isChinese){//在汉字范围内 //追加成string string.append((char)chr); //并且追加后面的字符 StringbehindString=hex[i].substring(4); string.append(behindString); }else{ string.append(hex[i]); } }catch(NumberFormatExceptione1){ string.append(hex[i]); } }else{ string.append(hex[i]); } }catch(NumberFormatExceptione){ string.append(hex[i]); } } returnstring.toString(); } /** *判断是否为中文字符 * *@paramc *@return */ publicstaticbooleanisChinese(charc){ Character.UnicodeBlockub=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){ returntrue; } returnfalse; } //是否为局域网 privatestaticBooleanisLan(Stringip){ if("127.0.0.1".equals(ip)){ returntrue; } 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 */ Stringreg="^(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}$"; Patternp=Pattern.compile(reg); Matchermatcher=p.matcher(ip); returnmatcher.find(); } }
工具类HttpClient
importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.io.OutputStream; importjava.net.HttpURLConnection; importjava.net.MalformedURLException; importjava.net.URL; publicclassHttpClient{ publicstaticvoidmain(String[]args){ } publicstaticStringdoGet(Stringhttpurl){ HttpURLConnectionconnection=null; InputStreamis=null; BufferedReaderbr=null; Stringresult=null;//返回结果字符串 try{ //创建远程url连接对象 URLurl=newURL(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=newBufferedReader(newInputStreamReader(is,"UTF-8")); //存放数据 StringBuffersbf=newStringBuffer(); Stringtemp=null; while((temp=br.readLine())!=null){ sbf.append(temp); sbf.append("\r\n"); } result=sbf.toString(); } }catch(MalformedURLExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ //关闭资源 if(null!=br){ try{ br.close(); }catch(IOExceptione){ e.printStackTrace(); } } if(null!=is){ try{ is.close(); }catch(IOExceptione){ e.printStackTrace(); } } connection.disconnect();//关闭远程连接 } returnresult; } publicstaticStringdoPost(StringhttpUrl,Stringparam){ HttpURLConnectionconnection=null; InputStreamis=null; OutputStreamos=null; BufferedReaderbr=null; Stringresult=null; try{ URLurl=newURL(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:Bearerda3efcbf-0845-4fe3-8aba-ee040be542c0 connection.setRequestProperty("Authorization","Bearerda3efcbf-0845-4fe3-8aba-ee040be542c0"); //通过连接对象获取一个输出流 os=connection.getOutputStream(); //通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 os.write(param.getBytes()); //通过连接对象获取一个输入流,向远程读取 if(connection.getResponseCode()==200){ is=connection.getInputStream(); //对输入流对象进行包装:charset根据工作项目组的要求来设置 br=newBufferedReader(newInputStreamReader(is,"UTF-8")); StringBuffersbf=newStringBuffer(); Stringtemp=null; //循环遍历一行一行读取数据 while((temp=br.readLine())!=null){ sbf.append(temp); sbf.append("\r\n"); } result=sbf.toString(); } }catch(MalformedURLExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ //关闭资源 if(null!=br){ try{ br.close(); }catch(IOExceptione){ e.printStackTrace(); } } if(null!=os){ try{ os.close(); }catch(IOExceptione){ e.printStackTrace(); } } if(null!=is){ try{ is.close(); }catch(IOExceptione){ e.printStackTrace(); } } //断开与远程地址url的连接 connection.disconnect(); } returnresult; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。