Baidu map radar / geocoding function use

I have been optimizing the bugs of the software recently.. Then there may be major changes in the future.. So take advantage of the time in these two days to write two blog posts..=.=

The map function can be said to be the most commonly used function in the APP now... uh.. one of..
whether it is e-commerce, social, o2o, b2c, p2p, kunjin copy, hot hot, you need to use the map to assist..

There are already many basic map implementations in the blog.. Positioning (LBS) .. marker (pin) .. pop-up window covering (InfoWindow). Dude, I suggest you read the writing of my male god Hongshen first.

Android Baidu Map SDK v3.0.0

Note that option.setCoorType(“bd09ll”); in the LocationClientOption located in it must be added.. All positioning must be based on this TYPE... Otherwise, the positioning is very inaccurate.. The difference is nearly one kilometer.. As for why there is Offset=.= It is said to be out of national security concerns... I tell you... My party is awesome.. It is said that... our map location.. Wait... I'll take it easy

The API of Baidu Maps has not changed much.. There is still no problem in implementing the basic functions. Why are my colleagues so irritable..

map radar

Let’s talk about the official explanation first. The
peripheral radar function is a set of SDK function interfaces for mobile developers. Synchronization supports Android and iOS. Its essence is an intermediate service connecting Baidu LBS open platform front-end SDK products and back-end LBS cloud.
(._°☆ ╲ (- – ).. What are you talking about..

To put it bluntly, you can see the location of people who use the same software around.. And this data is maintained by Baidu.. It greatly reduces the pressure on the server.. Otherwise, the server has to provide data.. Calculate the distance between users.. These can be done by the radar..

1. Basic use

The basic implementation is actually very simple.. It is nothing more than retrieving information.. You can get the list of surrounding users by calling the radar.. Then traverse the list to get the user you want..

1. First you need to register your radar in your API console..

Register Radar

2. Initialize and inject your message

RadarSearchManager mManager = RadarSearchManager.getInstance();//初始化

//设置信息
//周边雷达设置监听
mManager.addNearbyInfoListener(this);
//周边雷达设置用户身份标识,id为空默认是设备标识
mManager.setUserID(userID); //注意ID是一定要设置的.. 而且必须是String类型..如果不设置拿回来的ID是设备号(我也不知道是啥).. 反正跟server上的肯定对不上号..

Among them, you need to make your own class implement
RadarSearchListener, RadarUploadInfoCallback

One is the monitoring after the upload is completed.. The other is the callback after retrieving radar information

3. Start uploading

Upload is divided into single upload and multiple upload.. It’s enough to know about a single upload. It is estimated that no company has this demand.. Just uploading once can’t guarantee that you have your real-time location in the radar. Isn’t it meaningless..

single upload

//上传位置
RadarUploadInfo info = new RadarUploadInfo();
info.comments = “用户备注信息”;
info.pt = pt; //pt里封装了你的经纬度.. 可以从LBS(百度定位)的回调里获取..
mManager.uploadInfoRequest(info);
然后在RadarUploadInfoCallback重写的方法里对上传后的事件做处理
@Override
public void onGetUploadState(RadarSearchError error) {
    // TODO Auto-generated method stub
    if (error == RadarSearchError.RADAR_NO_ERROR) {
        //上传成功
        Toast.makeText(RadarDemo.this, "单次上传位置成功", Toast.LENGTH_LONG)
                .show();
    } else {
        //上传失败
        Toast.makeText(RadarDemo.this, "单次上传位置失败", Toast.LENGTH_LONG)
                .show();
    }
}

Repeat uploads regularly

//设置自动上传的callback和时间间隔(单位毫秒)
mManager.startUploadAuto(this, 5000);
//实现上传callback,自动上传 
//自动上传这里直接在接口里设置好就行了... pt的经纬度数据可以由OnLocationListener里动态的去修改提供
@Override
public RadarUploadInfo OnUploadInfoCallback() {
    // TODO Auto-generated method stub
    RadarUploadInfo info = new RadarUploadInfo();
    info.comments = “用户备注信息”;
    info.pt = pt;
    return info;
}

This way the data can be uploaded in real time.

4. Retrieve information

Of course there is storage.. The method of taking is actually very simple... But there is an ancient giant pit here.. I will talk about the processing method later
.

//构造请求参数,其中centerPt是自己的位置坐标
RadarNearbySearchOption option = new RadarNearbySearchOption()
.centerPt(pt)//中心点
.pageNum(pageIndex) //页码...? 这是啥 此处大坑
.radius(2000); //半径.. 不用说了
//发起查询请求
mManager.nearbyInfoRequest(option);
@Override
public void onGetNearbyInfoList(RadarNearbyResult result,
        RadarSearchError error) {
    // TODO Auto-generated method stub
    if (error == RadarSearchError.RADAR_NO_ERROR) {
        Toast.makeText(RadarDemo.this, "查询周边成功", Toast.LENGTH_LONG)
            .show();
        //获取成功,处理数据
    } else {
        //获取失败
        Toast.makeText(RadarDemo.this, "查询周边失败", Toast.LENGTH_LONG)
            .show();
    }
}

You will be very happy to find that there is a list in the result in the callback.. Take it out and take a look at the variables.. The objects in the list have userID.. There are pt.. You may be naive to think that you are done when testing.. Actually = =; I'm sorry..

5. Finish work

Finish the process first.. Finally, you need to finish the work..

//移除监听
mManager.removeNearbyInfoListener(this);
//清除用户信息
//如果你希望雷达里保留用户最后的位置.. 可以不做这一步..
mManager.clearUserInfo();
//释放资源
mManager.destroy();
mManager = null;

So far.. The official document process has been completed.. Then it can be used.. If you combine the marker, you can already see the position of the surrounding colleagues.. Then excitedly take the finished product to the product Manager.. Looking forward to promotion and salary increase.. Become general manager and become CEO to marry Bai Fumei..
( ̄ε(# ̄)☆╰╮( ̄▽ ̄///) I’m dreaming..

2. Yuangu Giant Pit

One of the unkind things about Baidu is that it omits the explanation in the above fourth step... In fact.. The retrieval of the list is paginated.
In fact, it is reasonable to do so. Otherwise, tens of thousands of pieces of data can be retrieved directly. ...what if it blows up... so you can pick it up according to your needs..

But the problem comes again.. How are the paging divided.. Where is the maximum page number.. How many entries are there on a page?

In fact, you still need to continue to add parameters here..

 //构造请求参数,其中centerPt是自己的位置坐标
RadarNearbySearchOption option = new RadarNearbySearchOption()
.centerPt(pt)//中心点
.pageNum(pageIndex) //页码...? 这是啥 此处大坑
.radius(2000); //半径.. 不用说了
.pageCapacity(50) //一页容量(1号坑:而且经过亲测.. 一页容量最大为50 .. 如果你写了>50的容量的话.. 他就按默认容量10取回了.. 坑的不行)
.sortType(RadarNearbySearchSortType.distance_from_far_to_near) //排序类型.. 按距离和按最后上传时间排序都行.. 
2号坑:而且在我用的百度地图v4.0.0里这个参数是有问题的... distance_from_far_to_near反而是由近及远.. distance_from_near_to_far是由远及近.. 反过来了.. 所以使用起来非常要注意

Damn.. if you have a hidden attribute... the hidden attribute also has its own confusion skills... it's scary.. so be careful when using it..

Of course this only fetches the first page..
you want in the callback..

@Override
    public void onGetNearbyInfoList(RadarNearbyResult radarNearbyResult, RadarSearchError radarSearchError) {

        if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) {
            if (radarNearbyResult.pageIndex < radarNearbyResult.pageNum -1 ) {//如果当前页码还没到最后一页的话
                radarInfos.addAll(radarNearbyResult.infoList);//数据保存起来

                option.pageNum(radarNearbyResult.pageIndex + 1);//页码加一

                mManager.nearbyInfoRequest(option);//再次发起请求




            } else { //如果已经达到最后一页
                radarInfos.addAll(radarNearbyResult.infoList);//保存数据
                addInfosOverlay(radarInfos);// 添加图层

            }

        } else {//如果获取失败
            mManager.nearbyInfoRequest(option);//重新去获取当前页数据



        }
    }

After doing this.. the bug is finally fixed.. Finally, it will not be invisible because there are more than ten users.. But repeat the request dozens of times (for 2000 people) .. This is very stuck.. Then we can only use progressbar to make users wait. After all, the data is not maintained by ourselves.. No way TAT

So far, the Baidu Map Radar function is perfect.. However, I was scolded by the manager... Look at how there are no bugs in the IOS... Damn, TAT IOS really retrieved tens of thousands of items at once... IOS buddies don't even know there are paging There's nothing wrong with this...

Geocoding

The geocoding function is divided into forward coding and reverse coding.. To put it bluntly.. That is, do you want to take the address to check the coordinates or take the coordinates to check the address..

The forward geocoding official website has been written very clearly.. There is no problem.. Reference

Geocoding

And the use of reverse geocoding is similar.. only need to be in


geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(new LatLng(latitude, longtitude))); //把经纬度传进去开始查找

OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {  
    public void onGetGeoCodeResult(GeoCodeResult result) {  
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  
            //没有检索到结果  
        }  else {
            Toast.showToast(getActivity(),result.getAddress()); //把地址吐司出来
            //获取地理编码结果  你就可以做爱做的事情了=.= 嘿嘿嘿 
        }

    }  

    @Override  
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {  
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  
            //没有找到检索结果  
        }  
        //获取反向地理编码结果  
    }  
};

Geocoding is still very simple to use...

renderings

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325414775&siteId=291194637