在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java根据百度API获得经纬度,然后根据经纬度在获得城市信息

2016-12-20 13:15| 发布者: zhangjf| 查看: 460| 评论: 0

摘要: import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import j ...
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. /**
  9. * 获取经纬度
  10. *
  11. * @author jueyue 返回格式:Map<String,Object> map map.put("status",
  12. * reader.nextString());//状态 map.put("result", list);//查询结果
  13. * list<map<String,String>>
  14. * 密钥:f247cdb592eb43ebac6ccd27f796e2d2
  15. */
  16. public class GetLatAndLngByBaidu {
  17. /**
  18. * @param addr
  19. * 查询的地址
  20. * @return
  21. * @throws IOException
  22. */
  23. public Object[] getCoordinate(String addr) throws IOException {
  24. String lng = null;//经度
  25. String lat = null;//纬度
  26. String address = null;
  27. try {
  28. address = java.net.URLEncoder.encode(addr, "UTF-8");
  29. }catch (UnsupportedEncodingException e1) {
  30. e1.printStackTrace();
  31. }
  32. String key = "f247cdb592eb43ebac6ccd27f796e2d2";
  33. String url = String .format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key);
  34. URL myURL = null;
  35. URLConnection httpsConn = null;
  36. try {
  37. myURL = new URL(url);
  38. } catch (MalformedURLException e) {
  39. e.printStackTrace();
  40. }
  41. InputStreamReader insr = null;
  42. BufferedReader br = null;
  43. try {
  44. httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
  45. if (httpsConn != null) {
  46. insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
  47. br = new BufferedReader(insr);
  48. String data = null;
  49. int count = 1;
  50. while((data= br.readLine())!=null){
  51. if(count==5){
  52. lng = (String)data.subSequence(data.indexOf(":")+1, data.indexOf(","));//经度
  53. count++;
  54. }else if(count==6){
  55. lat = data.substring(data.indexOf(":")+1);//纬度
  56. count++;
  57. }else{
  58. count++;
  59. }
  60. }
  61. }
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. } finally {
  65. if(insr!=null){
  66. insr.close();
  67. }
  68. if(br!=null){
  69. br.close();
  70. }
  71. }
  72. return new Object[]{lng,lat};
  73. }
  74. public static void main(String[] args) throws IOException {
  75. GetLatAndLngByBaidu getLatAndLngByBaidu = new GetLatAndLngByBaidu();
  76. Object[] o = getLatAndLngByBaidu.getCoordinate("成都市天府四街");
  77. System.out.println(o[0]);//经度
  78. System.out.println(o[1]);//纬度
  79. }
  80. }
复制代码

输出结果:104.04701

30.548397

调用以下路径根据经纬度获取地址,返回数据为JSON格式,

http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=30.548397,104.04701&output=json&pois=1

返回结果:

  1. {"status":0,"result":{"location":{"lng":104.04701001273,"lat":30.548396923974},"formatted_address":"四川省成都市武侯区天府四街","business":"","addressComponent":{"city":"成都市","district":"武侯区","province":"四川省","street":"天府四街","street_number":""},"pois":[{"addr":"四川省成都市武侯区天府四街","cp":"NavInfo","distance":"144","name":"保利·心语花园","poiType":"地产小区","point":{"x":104.04823889466,"y":30.54874673449},"tel":"","uid":"881965b85a8d8a977369d348","zip":""},{"addr":"四川省成都市武侯区天府四街","cp":"NavInfo","distance":"629","name":"逸都国际","poiType":"地产小区","point":{"x":104.05247889667,"y":30.547152788724},"tel":"","uid":"e49f869c4f156be9c6effb6b","zip":""},{"addr":"四川省成都市武侯区剑南大道","cp":"NavInfo","distance":"881","name":"世豪广场","poiType":"地产小区","point":{"x":104.05252381195,"y":30.553318490823},"tel":"","uid":"a2a7b0b167a0e1997adbd206","zip":""},{"addr":"四川省成都市武侯区德赛三街","cp":"NavInfo","distance":"989","name":"顾客停车场","poiType":"交通设施","point":{"x":104.04942465794,"y":30.555798655385},"tel":"","uid":"d135a6e15571911483a00cfe","zip":""}],"cityCode":75}}
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 12:19

Copyright 2015-2025 djqfx

返回顶部