利用百度LBS做一个小Demo

摘要  这里通过java调用baidu的地图api获取一些我们想要的信息,当然这里没有做站内地图,那个有现成的js api可以直接使用。

  • 申请ak(即获取密钥

  • 拼写发送http请求的url

    • 譬如这样的调用

    • http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=E4805d16520de693a3fe707cdc962045&callback=showLocation
  • 接收http请求返回的数据

下面看看代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
  * 接口常量
  * @author rex
  *
  */
public  interface  BaiDuApi {
 
     /*
      * 根据ip获取信息
      */
     public  static  final  String BD_IP_LOCATION_API =  "http://api.map.baidu.com/location/ip" ;
     
     /*
      *根据城市/经纬度获取信息
      */
     public  static  final  String BD_LOCATION2POINT_API =  "http://api.map.baidu.com/geocoder/v2/" ;
     
     /*
      * 百度lbs ak密钥
      */
     public  static  final  String BD_LBS_AK =  "你申请的AK" ;
     
}

    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
  * 百度地图API
  */
public  final  class  BaiDuMapApi {
 
     private  static  Map<String, String> params = CollectionUtil.newHashMap();
 
     private  static  final  List<String> LOCAL_IP = Arrays.asList( "127.0.0.1" "localhost" );
 
     /*
      * 根据ip获取经纬度
      */
     public  static  Map<String, String> getPoint(String ip) {
         params.clear();
         Map<String, String> point = CollectionUtil.newHashMap();
         if  ( null  != ip && !LOCAL_IP.contains(ip)) {
             params.put( "ip" , ip);
         }
         params.put( "ak" , BaiDuApi.BD_LBS_AK);
         params.put( "coor" "bd09ll" );
         String r = HttpKit.get(BaiDuApi.BD_IP_LOCATION_API, params);
         String json = StringUtils.unicodeToString(r);
         Map<String, Object> map = JSONUtil.json2Map(json);
         Map<String, Object> content = JSONUtil.json2Map(map.get( "content" ).toString());
         Map<String, String> xy = JSONUtil.json2Map(content.get( "point" ).toString());
         point.put( "latitude" , xy.get( "y" ));
         point.put( "longitude" , xy.get( "x" ));
         return  point;
     }
 
     /*
      * 根据城市和地址获取经纬度
      */
     public  static  Map<String, String> getPoint(String city, String address) {
         params.clear();
         Map<String, String> point = CollectionUtil.newHashMap();
         if  (StringUtils.isNotBlank(city) && StringUtils.isNotBlank(address)) {
             params.put( "ak" , BaiDuApi.BD_LBS_AK);
             params.put( "callback" "renderOption" );
             params.put( "output" "json" );
             params.put( "city" , city);
             params.put( "address" , address);
             String r = HttpKit.get(BaiDuApi.BD_LOCATION2POINT_API, params);
             String json = StringUtils.unicodeToString(r.substring(r.indexOf( "{" ), r.lastIndexOf( "}" ) +  1 ));
             Map<String, Object> map = JSONUtil.json2Map(json);
             Map<String, Object> content = JSONUtil.json2Map(map.get( "result" ).toString());
             Map<String, Object> location = JSONUtil.json2Map(content.get( "location" ).toString());
             point.put( "latitude" , location.get( "lat" ).toString());
             point.put( "longitude" , location.get( "lng" ).toString());
         }
         return  point;
     }
 
     /*
      * 根据ip获取address
      */
     public  static  Map<String, String> getAddress(String ip) {
         params.clear();
         Map<String, String> address_detail = CollectionUtil.newHashMap();
         if  ( null  != ip && !LOCAL_IP.contains(ip)) {
             params.put( "ip" , ip);
         }
         params.put( "ak" , BaiDuApi.BD_LBS_AK);
         String r = HttpKit.get(BaiDuApi.BD_IP_LOCATION_API, params);
         String json = StringUtils.unicodeToString(r);
         Map<String, Object> map = JSONUtil.json2Map(json);
         Map<String, Object> content = JSONUtil.json2Map(map.get( "content" ).toString());
         address_detail = JSONUtil.json2Map(content.get( "address_detail" ).toString());
         return  address_detail;
     }
 
     /*
      * 根据经纬度获取详细地址
      */
     public  static  Map<String, Object> getAddress(String latitude, String longitude) {
         params.clear();
         Map<String, Object> info = CollectionUtil.newHashMap();
         if  (StringUtils.isNotBlank(latitude) && StringUtils.isNotBlank(longitude)) {
             params.put( "callback" "renderReverse" );
             params.put( "ak" , BaiDuApi.BD_LBS_AK);
             params.put( "location" , latitude +  ","  + longitude);
             params.put( "output" "json" );
             params.put( "pois" "0" );
             String r = HttpKit.get(BaiDuApi.BD_LOCATION2POINT_API, params);
             String json = StringUtils.unicodeToString(r.substring(r.indexOf( "{" ), r.lastIndexOf( "}" ) +  1 ));
             Map<String, Object> map = JSONUtil.json2Map(json);
             info = JSONUtil.json2Map(map.get( "result" ).toString());
         }
         return  info;
     }
 
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
  * 百度地图API测试
  */
public  class  Test {
 
     public  static  void  main(String[] args) {
         //获取地址信息
         Map<String, String> m1 = BaiDuMapApi.getAddress( null );
         System.out.println(m1);
         
         //获取经纬度信息
         Map<String, String> m2 = BaiDuMapApi.getPoint( null );
         System.out.println(m2);
         
         //根据城市获取经纬度
         Map<String, String> m3 = BaiDuMapApi.getPoint( "上海市" "东方明珠" );
         System.out.println(m3);
         
         //根据经纬度获取地址
         Map<String, Object> m4 = BaiDuMapApi.getAddress(m3.get( "latitude" ), m3.get( "longitude" ));
         System.out.println(m4);
     }
}

    

?
1
2
3
4
{province=上海市, city=上海市, street=, district=, street_number=, city_code=289}
{latitude=31.24916171, longitude=121.48789949}
{latitude=31.244750205504, longitude=121.50713723717}
{formatted_address=上海市浦东新区陆家嘴环路1388号, business=东外滩,陆家嘴,外滩, cityCode=289, location={ "lat" :31.244750051136, "lng" :121.50713723717}, addressComponent={ "city" : "上海市" , "district" : "浦东新区" , "province" : "上海市" , "street" : "陆家嘴环路" , "street_number" : "1388号" }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值