服务端java实现根据地址从百度API获取经纬度

来源:互联网 发布:免费阿里云主机 编辑:程序博客网 时间:2024/05/22 10:33

服务端java实现根据地址从百度API获取经纬度

代码:

 

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

输出结果:104.04701

                   30.548397

 

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

http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=30.548397,104.04701&output=json&pois=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

    }

}

原创粉丝点击