我是靠谱客的博主 贤惠信封,最近开发中收集的这篇文章主要介绍android 百度周边雷达,android 百度地图SDK 3.7.0 周边雷达-Radar 周边搜索-PoiSearch,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

因为项目需要,最近研究了下百度地图的SDK,简单记录一些点。

准备工作

1.下载SDK及demo

2.申请key

key需要用到app的包名和签名证书的SHA1值

如果需要使用 周边雷达 功能,需要再注册一个,否则无法使用

测试代码

周边雷达-Radar相关

import android.content.Context;

import android.os.Handler;

import android.util.Log;

import com.baidu.location.BDLocation;

import com.baidu.location.BDLocationListener;

import com.baidu.location.LocationClient;

import com.baidu.location.LocationClientOption;

import com.baidu.mapapi.model.LatLng;

import com.baidu.mapapi.radar.RadarNearbyInfo;

import com.baidu.mapapi.radar.RadarNearbyResult;

import com.baidu.mapapi.radar.RadarNearbySearchOption;

import com.baidu.mapapi.radar.RadarSearchError;

import com.baidu.mapapi.radar.RadarSearchListener;

import com.baidu.mapapi.radar.RadarSearchManager;

import com.baidu.mapapi.radar.RadarUploadInfo;

public class BDRadarTest {

private static final String TAG = BDRadarTest.class.getSimpleName();

private static RadarSearchManager mRSManager = null;

private static LocationClient mLocClient = null;

private static LatLng mLatLng = null;

private static Handler mHandler = null;

private static Context mContext = null;

public static void init(Context context) {

mContext = context;

mHandler = new Handler() {

@Override

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case 0:

goStartRadar();

break;

case 1:

goGetLocation();

break;

case 2:

goUploadInfo(mLatLng);

break;

case 3:

goRadar(mLatLng);

break;

case 4:

goClear();

break;

case 5:

goDestory();

break;

default:

break;

}

}

};

}

private static RadarSearchListener mRSListener = new RadarSearchListener() {

@Override

public void onGetUploadState(RadarSearchError err) {

Log.i(TAG, "UploadState");

if (err == RadarSearchError.RADAR_NO_ERROR) {

Log.i(TAG, "UploadState OK");

mHandler.sendEmptyMessage(3);

} else {

Log.i(TAG, "UploadState err:" + err);

mHandler.sendEmptyMessage(4);

}

}

@Override

public void onGetNearbyInfoList(RadarNearbyResult result,

RadarSearchError err) {

Log.i(TAG, "NearbyInfoList");

if (err == RadarSearchError.RADAR_NO_ERROR) {

Log.i(TAG, "NearbyInfoList OK");

//totalNum:3

//pageIndex:0

//pageNum:1

//infoList.size:3

//info userid:123

//info distance:814

//info comments:1

//info timeStamp:Mon Feb 15 18:24:49 格林尼治标准时间+0800 2016

Log.i(TAG, "totalNum:" + result.totalNum);// 总结果个数

Log.i(TAG, "pageIndex:" + result.pageIndex);// 页码

Log.i(TAG, "pageNum:" + result.pageNum);// 总页数

Log.i(TAG, "infoList.size:" + result.infoList.size());

for (int i = 0; i < result.infoList.size(); i++) {

RadarNearbyInfo info = result.infoList.get(i);

Log.i(TAG, "info userid:" + info.userID);// userid

Log.i(TAG, "info distance:" + info.distance);// 距离

Log.i(TAG, "info comments:" + info.comments);// 备注

Log.i(TAG, "info timeStamp:" + info.timeStamp);// 上传位置时的时间戳

}

} else {

Log.i(TAG, "NearbyInfoList err:" + err);

}

mHandler.sendEmptyMessage(4);

}

@Override

public void onGetClearInfoState(RadarSearchError err) {

Log.i(TAG, "ClearInfoState");

if (err == RadarSearchError.RADAR_NO_ERROR) {

Log.i(TAG, "ClearInfoState OK");

} else {

Log.i(TAG, "ClearInfoState err:" + err);

}

mHandler.sendEmptyMessage(5);

}

};

public static void startRadar() {

Log.i(TAG, "startRadar");

mHandler.sendEmptyMessage(0);

}

private static void goStartRadar() {

Log.i(TAG, "goStartRadar");

// 获取本地位置--上传位置信息--等待回调--回调成功--开始查询--等待回调--回调中打印结果

mRSManager = RadarSearchManager.getInstance();

mRSManager.setUserID("test01");

mRSManager.addNearbyInfoListener(mRSListener);

// 定位

mHandler.sendEmptyMessage(1);

}

private static void goGetLocation() {

Log.i(TAG, "goGetLocation");

LocationClientOption locOption = new LocationClientOption();

locOption.setIgnoreKillProcess(false);

locOption.setEnableSimulateGps(true);

locOption.setCoorType("bd09ll");

mLocClient = new LocationClient(mContext);

mLocClient.setLocOption(locOption);

mLocClient.registerLocationListener(new BDLocationListener() {

@Override

public void onReceiveLocation(BDLocation location) {

Log.i(TAG, "onReceiveLocation");

mLatLng = new LatLng(location.getLatitude(),

location.getLongitude());

mHandler.sendEmptyMessage(2);

// 定位成功后销毁

mLocClient.stop();

}

});

mLocClient.start();

}

private static void goUploadInfo(LatLng ll) {

Log.i(TAG, "goUploadInfo");

RadarUploadInfo info = new RadarUploadInfo();

info.comments = "m";

info.pt = ll;

mRSManager.uploadInfoRequest(info);

}

private static void goRadar(LatLng ll) {

Log.i(TAG, "goRadar");

RadarNearbySearchOption option = new RadarNearbySearchOption();

option.centerPt(ll);// 中心点

option.pageCapacity(10);// 每页包含的结果数

option.pageNum(0);// 当前需要查询的页码index,从0开始

option.radius(1000);// 搜索半径

mRSManager.nearbyInfoRequest(option);

}

private static void goClear() {

Log.i(TAG, "goClear");

mRSManager.clearUserInfo();

}

private static void goDestory() {

Log.i(TAG, "goDestory");

mRSManager.removeNearbyInfoListener(mRSListener);

mRSManager.destroy();

}

}

调用代码:

// 初始化SDK

SDKInitializer.initialize(getApplicationContext());

// 初始化handler

BDRadarTest.init(context);

// 开始

BDRadarTest.startRadar();

周边搜索-PoiSearch相关

import android.content.Context;

import android.os.Handler;

import android.util.Log;

import com.baidu.location.BDLocation;

import com.baidu.location.BDLocationListener;

import com.baidu.location.LocationClient;

import com.baidu.location.LocationClientOption;

import com.baidu.mapapi.model.LatLng;

import com.baidu.mapapi.search.core.PoiInfo;

import com.baidu.mapapi.search.core.SearchResult.ERRORNO;

import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;

import com.baidu.mapapi.search.poi.PoiDetailResult;

import com.baidu.mapapi.search.poi.PoiNearbySearchOption;

import com.baidu.mapapi.search.poi.PoiResult;

import com.baidu.mapapi.search.poi.PoiSearch;

import com.baidu.mapapi.search.poi.PoiSortType;

public class BDPoiTest {

private static final String TAG = BDPoiTest.class.getSimpleName();

private static PoiSearch mPoiSManager = null;

private static LocationClient mLocClient = null;

private static LatLng mLatLng = null;

private static Handler mHandler = null;

private static Context mContext = null;

public static void init(Context context) {

mContext = context;

mHandler = new Handler() {

@Override

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case 0:

goStartPoi();

break;

case 1:

goGetLocation();

break;

case 2:

goPoiSearch(mLatLng);

break;

case 3:

goDestory();

break;

default:

break;

}

}

};

}

private static OnGetPoiSearchResultListener mPoiResultListener = new OnGetPoiSearchResultListener() {

@Override

public void onGetPoiResult(PoiResult result) {

if (result.error == ERRORNO.NO_ERROR) {

Log.i(TAG, "GetPoiResult OK");

//totalNum:45

//totalPageNum:5

//pageIndex:0

//pageCapacity:10

//isHasAddrInfo:false

//poi.size:10

//info address:北京市东城区王府井大街277号(近好友世界商场)

//info city:北京市

//info name:酷时代游艺世界

//info phoneNum:(010)65225696

//info postCode:null

//info uid:34059f97e013925471455bce

//info hasCaterDetails:false

//info isPano:true

//info location:latitude: 39.91824597245212, longitude: 116.41649542740966

//info type:POINT

Log.i(TAG, "totalNum:" + result.getTotalPoiNum());// 总结果个数

Log.i(TAG, "totalPageNum:" + result.getTotalPageNum());// 总页数

Log.i(TAG, "pageIndex:" + result.getCurrentPageNum());// 页码

Log.i(TAG, "pageCapacity:" + result.getCurrentPageCapacity());// 本页的结果数,可能小于本页可显示的结果数

Log.i(TAG, "isHasAddrInfo:" + result.isHasAddrInfo());

Log.i(TAG, "poi.size:" + result.getAllPoi().size());

if (result.isHasAddrInfo()) {// 如果为false,则getAllAddr为空

Log.i(TAG, "addr.size:" + result.getAllAddr().size());

}

for (int i = 0; i < result.getAllPoi().size(); i++) {

PoiInfo info = result.getAllPoi().get(i);

Log.i(TAG, "info address:" + info.address);// 地址

Log.i(TAG, "info city:" + info.city);// 城市

Log.i(TAG, "info name:" + info.name);// 名称

Log.i(TAG, "info phoneNum:" + info.phoneNum);// 电话

Log.i(TAG, "info postCode:" + info.postCode);// 邮编

Log.i(TAG, "info uid:" + info.uid);// uid,用于后面查询DetailResult

Log.i(TAG, "info hasCaterDetails:" + info.hasCaterDetails);// 是否有详细信息

Log.i(TAG, "info isPano:" + info.isPano);// 是否有全景

Log.i(TAG, "info location:" + info.location);// 经纬度

Log.i(TAG, "info type:" + info.type);// 类型

}

} else {

Log.i(TAG, "GetPoiResult err:" + result.error);

}

mHandler.sendEmptyMessage(3);

}

@Override

public void onGetPoiDetailResult(PoiDetailResult result) {

mHandler.sendEmptyMessage(3);

}

};

public static void startPoi() {

Log.i(TAG, "startPoi");

mHandler.sendEmptyMessage(0);

}

private static void goStartPoi() {

Log.i(TAG, "goStartPoi");

// 获取本地位置--上传位置信息--等待回调--回调成功--开始查询--等待回调--回调中打印结果

mPoiSManager = PoiSearch.newInstance();

mPoiSManager.setOnGetPoiSearchResultListener(mPoiResultListener);

// 定位

mHandler.sendEmptyMessage(1);

}

private static void goGetLocation() {

Log.i(TAG, "goGetLocation");

LocationClientOption locOption = new LocationClientOption();

locOption.setIgnoreKillProcess(false);

locOption.setEnableSimulateGps(true);

locOption.setCoorType("bd09ll");

mLocClient = new LocationClient(mContext);

mLocClient.setLocOption(locOption);

mLocClient.registerLocationListener(new BDLocationListener() {

@Override

public void onReceiveLocation(BDLocation location) {

Log.i(TAG, "onReceiveLocation");

mLatLng = new LatLng(location.getLatitude(),

location.getLongitude());

mHandler.sendEmptyMessage(2);

// 定位成功后销毁

mLocClient.stop();

}

});

mLocClient.start();

}

private static void goPoiSearch(LatLng ll) {

Log.i(TAG, "goPoiSearch");

PoiNearbySearchOption option = new PoiNearbySearchOption();

option.location(ll);

option.radius(3000);

option.sortType(PoiSortType.distance_from_near_to_far);

option.keyword("棋牌室");

option.pageCapacity(10);// 每页最多含多少条结果

option.pageNum(0);// 查询的页码

mPoiSManager.searchNearby(option);

}

private static void goDestory() {

Log.i(TAG, "goDestory");

mPoiSManager.destroy();

}

}

调用方式与之前相似。

注意!这里有坑

所有流程都在主线程或handler里做,否则可能会报错

上传信息时,用户名和备注有限制,具体限制未知,如果上传失败,可以尝试修改

Option的pageNum指的是要查询的page的index,从0开始

周边雷达,需要上传位置信息后,才能被搜索到

周边雷达的位置信息会一直保留,除非自己主动clear

poi搜索结果中,如果isHasAddrInfo为false,那么getAllAddr将是一个null

最后

以上就是贤惠信封为你收集整理的android 百度周边雷达,android 百度地图SDK 3.7.0 周边雷达-Radar 周边搜索-PoiSearch的全部内容,希望文章能够帮你解决android 百度周边雷达,android 百度地图SDK 3.7.0 周边雷达-Radar 周边搜索-PoiSearch所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部