利用百度LBS做一个小Demo

简介:
  • 申请ak(即获取密钥

  • 拼写发送http请求的url

    • 譬如这样的调用

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

下面看看代码

/**
 * 接口常量
 * @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";
    
}

    

/*
 * 百度地图API
 */
public final class BaiDuMapApi {

    private static Map<StringString> params = CollectionUtil.newHashMap();

    private static final List<String> LOCAL_IP = Arrays.asList("127.0.0.1""localhost");

    /*
     * 根据ip获取经纬度
     */
    public static Map<StringString> getPoint(String ip) {
        params.clear();
        Map<StringStringpoint = 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<StringObjectmap = JSONUtil.json2Map(json);
        Map<StringObject> content = JSONUtil.json2Map(map.get("content").toString());
        Map<StringString> xy = JSONUtil.json2Map(content.get("point").toString());
        point.put("latitude", xy.get("y"));
        point.put("longitude", xy.get("x"));
        return point;
    }

    /*
     * 根据城市和地址获取经纬度
     */
    public static Map<StringString> getPoint(String city, String address) {
        params.clear();
        Map<StringStringpoint = 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<StringObjectmap = JSONUtil.json2Map(json);
            Map<StringObject> content = JSONUtil.json2Map(map.get("result").toString());
            Map<StringObject> 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<StringString> getAddress(String ip) {
        params.clear();
        Map<StringString> 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<StringObjectmap = JSONUtil.json2Map(json);
        Map<StringObject> content = JSONUtil.json2Map(map.get("content").toString());
        address_detail = JSONUtil.json2Map(content.get("address_detail").toString());
        return address_detail;
    }

    /*
     * 根据经纬度获取详细地址
     */
    public static Map<StringObject> getAddress(String latitude, String longitude) {
        params.clear();
        Map<StringObject> 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<StringObjectmap = JSONUtil.json2Map(json);
            info = JSONUtil.json2Map(map.get("result").toString());
        }
        return info;
    }

}

/*
 * 百度地图API测试
 */
public class Test {

    public static void main(String[] args) {
        //获取地址信息
        Map<StringString> m1 = BaiDuMapApi.getAddress(null);
        System.out.println(m1);
        
        //获取经纬度信息
        Map<StringString> m2 = BaiDuMapApi.getPoint(null);
        System.out.println(m2);
        
        //根据城市获取经纬度
        Map<StringString> m3 = BaiDuMapApi.getPoint("上海市""东方明珠");
        System.out.println(m3);
        
        //根据经纬度获取地址
        Map<StringObject> m4 = BaiDuMapApi.getAddress(m3.get("latitude"), m3.get("longitude"));
        System.out.println(m4);
    }
}

    

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

目录
相关文章
|
5月前
|
UED
【实训项目】“在校园”APP-大学校园周边服务搜索平台
【实训项目】“在校园”APP-大学校园周边服务搜索平台
|
9月前
|
移动开发 小程序 JavaScript
微信小程序学习实录5(H5嵌入小程序、map组件、地图调起功能、腾讯百度高德导航页、返回web-view页)
微信小程序学习实录5(H5嵌入小程序、map组件、地图调起功能、腾讯百度高德导航页、返回web-view页)
307 0
|
10月前
|
JavaScript 小程序
微信小程序精准定位百度
微信小程序精准定位百度
81 0
|
API Android开发
京东万象--新闻接口实现简单的新闻app
京东万象--新闻接口实现简单的新闻app
486 0
|
定位技术 开发工具 Android开发
android 跳转第三方地图(百度,高德,谷歌)
android 跳转第三方地图(百度,高德,谷歌)
|
人工智能 小程序 搜索推荐
从工具到生态,百度App是如何构建搜索护城河的?
“搜索这个赛道从来不乏竞争者,平均每两年都有一个新的入局者,但百度始终保持80%以上的市场份额。”
从工具到生态,百度App是如何构建搜索护城河的?
|
自然语言处理 搜索推荐 算法
海量数据搜索---demo展示百度、谷歌搜索引擎的实现
百度、谷歌等网站之所以能很快在海量数据中找到需要的数据,得益于其搜索引擎,本文将介绍搜索引擎的基本知识及中文分词的方法,并通过demo演示如何进行数据检索。
|
数据采集 数据可视化 iOS开发
微博(APP)榜单爬虫及数据可视化
前言 今天继续APP爬虫,今天爬取的是微博榜单(24小时榜)的数据,采集的字段有: 用户id 用户地区 用户性别 用户粉丝 微博内容 发布时间 转发、评论和点赞量 该文分以下内容: 爬虫代码 用户分析 微博分析 ...
2186 0
|
搜索推荐 前端开发 JavaScript

热门文章

最新文章