1、使用:
RxBusUtil.register(this);
//定位成功后走这里
@Subscribe
public void onGetLocation(AreaBean bean) {
mMapAreaBean = bean;
mTvArea.setText(bean.getAddress());
String longitude = bean.getLongitude();
String latitude = bean.getLatitude();
LogUtil.d("longitude:" + longitude + ",latitude:" + latitude);
doSearchQuery(bean.getCity(), Double.parseDouble(latitude),
Double.parseDouble(longitude));
}
@Override
public void onDestroy() {
super.onDestroy();
RxBusUtil.unRegister(this);
context.stopService(new Intent(context, GetLocationService.class));
}
2、申请权限成功,开始定位
private void initPermissions() {
RxPermissions permissions = new RxPermissions(this);
permissions.request(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS
).subscribe(new BaseObserver<Boolean>(context, false) {
@Override
public void receiveResult(Boolean result) {
if (result) {
context.startService(new Intent(context, GetLocationService.class));
} else {
ToastUtil.showShortToast("请授予相应的权限,否则无法使用");
}
}
});
}
/**
* 开始进行poi搜索
*/
protected void doSearchQuery(String city, double latitude, double longitude) {
String mType = "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施";
// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
query = new PoiSearch.Query("", mType, city);
query.setPageSize(20);// 设置每页最多返回多少条poiitem
query.setPageNum(1);// 设置查第一页
PoiSearch poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
//以当前定位的经纬度为准搜索周围5000米范围
// 设置搜索区域为以lp点为圆心,其周围5000米范围
poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 1000, true));//
poiSearch.searchPOIAsyn();// 异步搜索
}
//回调的方法
@Override
public void onPoiItemSearched(PoiItem arg0, int arg1) {
}
@Override
public void onPoiSearched(PoiResult result, int arg1) {
if (arg1 == 1000) {
if (result != null && result.getQuery() != null) {// 搜索poi的结果
if (result.getQuery().equals(query)) {// 是否是同一条
List<PoiItem> poiItems = result.getPois();// 取得第一页的poiitem数据,页数从数字0开始
List<PoiBean> tem = new ArrayList();
if (poiItems != null && poiItems.size() > 0) {
for (int i = 0; i < poiItems.size(); i++) {
PoiItem poiItem = poiItems.get(i); //写一个bean,作为数据存储
PoiBean bean = new PoiBean();
bean.setTitleName(poiItem.getTitle());
bean.setCityName(poiItem.getCityName());
bean.setAd(poiItem.getAdName());
bean.setSnippet(poiItem.getSnippet());
bean.setPoint(poiItem.getLatLonPoint());
Log.e("yufs", "" + poiItem.getTitle() + "," + poiItem.getProvinceName() + ","
+ poiItem.getCityName() + ","
+ poiItem.getAdName() + ","//区
+ poiItem.getSnippet() + ","
+ poiItem.getLatLonPoint() + "\n");
tem.add(bean);
}
poiData.addAll(tem);
// mAdapter.notifyDataSetChanged(); //解析成功更新list布局
}
}
}
}
}
3、GetLocationService类
public class GetLocationService extends Service {
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
private AMapLocationClient mLocationClient;
//初始化AMapLocationClientOption对象
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initLocation();
return START_STICKY;
}
private void initLocation() {
//声明定位回调监听器
AMapLocationListener locationListener = aMapLocation -> {
mLocationClient.stopLocation();
mLocationClient.onDestroy();
RxBusUtil.postEvent(new AreaBean(String.valueOf(aMapLocation.getLongitude()), String.valueOf(aMapLocation.getLatitude()))
.setProvince(aMapLocation.getProvince())
.setCity(aMapLocation.getCity())
.setArea(aMapLocation.getDistrict())
.setAddressDetail(aMapLocation.getStreet())
.setCityCode(aMapLocation.getAdCode())
);
GetLocationService.this.stopSelf();
};
//声明AMapLocationClient类对象
mLocationClient = new AMapLocationClient(getApplicationContext());
mLocationClient.setLocationListener(locationListener);
mLocationOption = new AMapLocationClientOption();
mLocationOption.setOnceLocation(true);//获取一次定位结果
mLocationOption.setNeedAddress(true);//设置是否返回地址信息(默认返回地址信息)
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
}