百度地图LBS与系统数据交互记录

前一阵使用了百度的LBS来与系统进行交互,简单来说就是将我系统表中的点与虎鲸数据平台上的点相对应,在使用百度地图控件进行显示时,请求虎鲸的数据,这里对问题进行一次总结。

在使用之前,应该读一下平台的接口文档

https://download.csdn.net/download/fye449304142/9477758

开始使用,建一个数据平台上的表


这里面基本字段是平台上的表中自带的字段,自定义字段用来对应我数据库中的数据,主要是用那个pkId来对应,不过这方面也有坏处,因为当建数据或更改数据的时候,最方便的是使用这个数据平台上给数据自己生成的ID,如果使用自己字段的pkId来对应的话,比较麻烦,也导致了后面有一些问题。

手动给表加上一些测试数据,与表中相对应。


在系统中与平台数据进行交互大致可分为两种,根据请求方式的不同,一种get请求,一种post请求。

使用get请求将数据请求过来,在接口后面将参数拼接到后面即可,我倾向于在js中直接进行数据请求,不走后端。

//向地图中添加点
function addPosition(i) {
    var marker;
    var label;
    var pt = null;

    $.ajax({
        type: "GET",//方法类型
        dataType: "JSONP",//预期服务器返回的数据类型
        url: "http://api.map.baidu.com/geosearch/v3/bound?ak=发的ak&geotable_id=表的id&bounds="+map.getBounds().getSouthWest().lng+","+map.getBounds().getSouthWest().lat+";"+map.getBounds().getNorthEast().lng+","+map.getBounds().getNorthEast().lat+"&page_size=50&page_index="+i+"" ,//url
        success: function (data) {
            for (var j = 0;j < data.size;j++) {
                //点聚合
                pt = new BMap.Point(data.contents[j].location[0],data.contents[j].location[1]);
                marker = new BMap.Marker(pt);
                label = new BMap.Label(data.contents[j].equip_id, {offset: new BMap.Size(20, 0)}); //创建marker点的标记

                label.setStyle({display:"none"});//对label 样式隐藏
                marker.setLabel(label);  //把label设置到maker上
                //添加点击事件
                marker.addEventListener('click',function (e) {//点击事件
                    window.parent.location.href = "/cloudPlatform/equipDetailC/showDetail?pkId="+e.target.getLabel().content+"";
                });
                markers.push(marker);
            }

            i++;
            if(data.total > i * 50){
                addPosition(i);
            } else {
                //最简单的用法,生成一个marker数组,然后调用markerClusterer类即可。
                var markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
            }
        }
    });
}

里面使用了,百度地图的点聚合,请求是根据地图的西南角和东北角坐标进行请求的。因为是分页查询pagesize是限定一页多少条,pageindex是限定查询第几页。

返回参数示例

{
    "status": 0,
    "total": 13,
    "size": 13,
    "contents": [
        {
            "equip_id": 1,
            "cus_id": 7,
            "uid": 2431605840,
            "province": "辽宁省",
            "geotable_id": 186141,
            "modify_time": 1523928371,
            "district": "甘井子区",
            "equip_code": "01011101",
            "create_time": 1521695822,
            "city": "大连市",
            "location": [
                121.550287,
                38.972107999999999
            ],
            "address": "辽宁省大连市甘井子区",
            "title": "大连国际机场",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 2,
            "cus_id": 7,
            "uid": 2431607062,
            "province": "辽宁省",
            "geotable_id": 186141,
            "modify_time": 1523928365,
            "district": "甘井子区",
            "equip_code": "01011102",
            "create_time": 1521695895,
            "city": "大连市",
            "location": [
                121.59354999999999,
                39.015684999999998
            ],
            "address": "辽宁省大连市甘井子区川岭路",
            "title": "圣泰幼儿园",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 3,
            "cus_id": 7,
            "uid": 2431607110,
            "province": "辽宁省",
            "geotable_id": 186141,
            "modify_time": 1523928354,
            "district": "甘井子区",
            "equip_code": "01011103",
            "create_time": 1521695917,
            "city": "大连市",
            "location": [
                121.46821799999999,
                38.934902999999998
            ],
            "address": "辽宁省大连市甘井子区",
            "title": "大连天顺驾校训练场",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 4,
            "cus_id": 7,
            "uid": 2431607175,
            "province": "辽宁省",
            "geotable_id": 186141,
            "modify_time": 1523928346,
            "district": "中山区",
            "equip_code": "01011104",
            "create_time": 1521695939,
            "city": "大连市",
            "location": [
                121.651616,
                38.900317999999999
            ],
            "address": "辽宁省大连市中山区青云街25-2",
            "title": "大连青云街苏军烈士墓园",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 5,
            "cus_id": 7,
            "uid": 2431607225,
            "province": "辽宁省",
            "geotable_id": 186141,
            "modify_time": 1523928339,
            "district": "旅顺口区",
            "equip_code": "01011105",
            "create_time": 1521695958,
            "city": "大连市",
            "location": [
                121.469943,
                38.840989999999998
            ],
            "address": "辽宁省大连市旅顺口区",
            "title": "石子街",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 6,
            "cus_id": 7,
            "uid": 2431607273,
            "province": "辽宁省",
            "geotable_id": 186141,
            "modify_time": 1523928332,
            "district": "金州区",
            "equip_code": "01011106",
            "create_time": 1521695974,
            "city": "大连市",
            "location": [
                121.84478799999999,
                39.013443000000002
            ],
            "address": "辽宁省大连市金州区",
            "title": "大连大富塑料彩印有限公司",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 7,
            "cus_id": 6,
            "uid": 2431607348,
            "province": "北京市",
            "geotable_id": 186141,
            "modify_time": 1523928318,
            "district": "西城区",
            "equip_code": "01011107",
            "create_time": 1521696000,
            "city": "北京市",
            "location": [
                116.38769000000001,
                39.924267
            ],
            "address": "北京市西城区西椅子胡同11号",
            "title": "中国邮政储蓄银行(中南海支行)",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 8,
            "cus_id": 6,
            "uid": 2431607402,
            "province": "北京市",
            "geotable_id": 186141,
            "modify_time": 1523928308,
            "district": "东城区",
            "equip_code": "01011108",
            "create_time": 1521696023,
            "city": "北京市",
            "location": [
                116.415285,
                39.895930999999997
            ],
            "address": "北京市东城区金鱼池东街",
            "title": "金鱼池中区",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 9,
            "cus_id": 6,
            "uid": 2431607455,
            "province": "天津市",
            "geotable_id": 186141,
            "modify_time": 1523928295,
            "district": "东丽区",
            "equip_code": "01011109",
            "create_time": 1521696046,
            "city": "天津市",
            "location": [
                117.491527,
                39.094135000000001
            ],
            "address": "天津市东丽区",
            "title": "和顺欣园11号楼",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 10,
            "cus_id": 6,
            "uid": 2431607494,
            "province": "天津市",
            "geotable_id": 186141,
            "modify_time": 1523928288,
            "district": "滨海新区",
            "equip_code": "01011110",
            "create_time": 1521696063,
            "city": "天津市",
            "location": [
                117.638706,
                39.069040000000001
            ],
            "address": "天津市滨海新区宁海路",
            "title": "友阿奥特莱斯",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 11,
            "cus_id": 6,
            "uid": 2431607556,
            "province": "上海市",
            "geotable_id": 186141,
            "modify_time": 1523928280,
            "district": "黄浦区",
            "equip_code": "01011111",
            "create_time": 1521696086,
            "city": "上海市",
            "location": [
                121.479141,
                31.219297000000001
            ],
            "address": "上海市黄浦区合肥路365号",
            "title": "上海交通大学医学院东区7号楼",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 12,
            "cus_id": 6,
            "uid": 2431607593,
            "province": "上海市",
            "geotable_id": 186141,
            "modify_time": 1523928271,
            "district": "闵行区",
            "equip_code": "01011112",
            "create_time": 1521696101,
            "city": "上海市",
            "location": [
                121.42395,
                31.029366
            ],
            "address": "上海市闵行区沪闵路1448号",
            "title": "畅馨园",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        },
        {
            "equip_id": 666,
            "cus_id": 1,
            "uid": 2457652047,
            "province": "辽宁省",
            "geotable_id": 186141,
            "district": "旅顺口区",
            "equip_code": "222",
            "create_time": 1527035222,
            "city": "大连市",
            "location": [
                121.36581099999999,
                38.901328999999997
            ],
            "address": "辽宁省大连市旅顺口区",
            "title": "英歌石植物园",
            "coord_type": 3,
            "type": 0,
            "distance": 0,
            "weight": 0
        }
    ]

}

使用post请求,传送数据,需要使用表单发送,如果想要在js上使用,需要使用ajaxform,但我倾向于在后端传送数据。

使用示例

/**
 *  向虎鲸数据平台新增设备
 * @param lng
 * @param lat
 * @param sessionUsr
 * @param equipmentId
 * @param pk_id
 * @return
 */
public static AddPostRes addPost(String lng, String lat, String sessionUsr, String equipmentId, int pk_id){
    ObjectMapper mapper = new ObjectMapper();
    String params = "ak=发的ak&geotable_id=表的Id&coord_type=3&latitude="+lng+"&longitude="+lat+"&cus_id="+sessionUsr+"&equip_code="+equipmentId+"&equip_id="+pk_id+"";
    try {
        URL url = new URL("http://api.map.baidu.com/geodata/v3/poi/create");// 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //参数设置
        connection.setDoOutput(true);//设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false;
        connection.setDoInput(true);// 设置是否从httpUrlConnection读入,默认情况下是true;
        connection.setUseCaches(false);// Post 请求不能使用缓存
        connection.setRequestMethod("POST"); // 设置请求方式
        connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
        //connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); // 设置发送数据的格式
        connection.connect();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
        writer.write(params);
        writer.close();
        // 读取响应
        int length = (int) connection.getContentLength();// 获取长度
        InputStream is = connection.getInputStream();
        if (length != -1) {
            byte[] data = new byte[length];
            byte[] temp = new byte[512];
            int readLen = 0;
            int destPos = 0;
            while ((readLen = is.read(temp)) > 0) {
                System.arraycopy(temp, 0, data, destPos, readLen);
                destPos += readLen;
            }
            String result = new String(data, "UTF-8"); // utf-8编码
            AddPostRes addPostRes = mapper.readValue(result, AddPostRes.class);
            return addPostRes;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

其中result是返回的json,这个方法的功能是新增,返回成功的话有3个参数,status状态参数,message信息参数,id新增数据的id。使用objectMapper来接受json参数。

但是,在后台使用删除时遇到问题,此为删除示例


但我这边删除操作,删除的条件不是这个数据的id,因为id没记,也不用。


使用自己的字段来进行删除,不好用。可能是使用的方法有问题。

http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=3367

有个人提问了类似的问题,好像要设置is_search_filed=1,但是我没有看到在哪里设置。


下面开始查询一下,先查出表中列的id,原来列也有id


根据列的id    326749来查询这个列


查询结果中,is_search_field是0,原因可能找到了。

把这个属性更改一下


经过一番查询之后,我妥协了,还是存一下它每条数据的这个id把,删除的时候通过id删除。。。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值