Android仿微信地图定位和位置选择(下)
Posted 一杯清泉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android仿微信地图定位和位置选择(下)相关的知识,希望对你有一定的参考价值。
上篇文章介绍了基本的常用功能,这边文件介绍剩余的位置信息更新、搜索、手势绑定和显示等内容。
一、获取坐标系附近的信息
1、设置默认
boolean fromMarker = false;
测距设置的标识,是否以当前位置为基准测算数据,true表示依据设置的setOriginLaLng点为基础,false表示语句当前定位位置为基准。
2、设置原始的数据
public void setOriginLaLng(LatLng originLaLng) {
this.originLaLng = originLaLng;
}
主要目的是为了测算搜索到的位置信息距离原始位置的距离。
3、获取附近信息
public void getNearAddress(LatLng latLng, int page, boolean fromMarker) {
TencentSearch tencentSearch = new TencentSearch(ApplicationUtils.getContext());
Geo2AddressParam param = new Geo2AddressParam(latLng);
param.getPoi(true);
param.setPoiOptions(new Geo2AddressParam.PoiOptions()
.setAddressFormat(Geo2AddressParam.PoiOptions.ADDRESS_FORMAT_SHORT)
.setPageIndex(page)
.setPageSize(20)
.setPolicy(5)
.setRadius(5000));
tencentSearch.geo2address(param, new HttpResponseListener<Geo2AddressResultObject>() {
@Override
public void onSuccess(int code, Geo2AddressResultObject addressBean) {
if (addressBean.isStatusOk()) {
if (addressBean.result.formatted_addresses == null
|| addressBean.result.address_component == null) {
view.onFailure(addressBean.status, "获取位置信息失败");
return;
}
if (addressBean.status == 0) {
List<MapLocationBean> mapList = new ArrayList<>();
if (page == 1) {
MapLocationBean bean = new MapLocationBean();
bean.name = addressBean.result.formatted_addresses.recommend;
bean.address = addressBean.result.address_component.district;
float dist = 0;
if (fromMarker && originLaLng != null) {
dist = (float) TencentLocationUtils.distanceBetween(originLaLng.latitude, originLaLng.longitude, latLng.latitude, latLng.longitude);
}
if (dist <= 100) {
bean.distance = "100m以内";
} else if (dist > 1000) {
bean.distance = StringUtils.getOneDecimal(dist / 1000 + "") + "km";
} else {
bean.distance = dist + "m";
}
bean.latitude = latLng.latitude;
bean.longitude = latLng.longitude;
LogUtils.d(bean.toString());
mapList.add(bean);
}
if (addressBean.result.pois != null) {
for (int i = 0; i < addressBean.result.pois.size(); i++) {
Poi posBean = addressBean.result.pois.get(i);
MapLocationBean mapLocationBean = new MapLocationBean();
mapLocationBean.name = posBean.title;
if (addressBean.result.ad_info != null && posBean.address != null) {
String s = addressBean.result.ad_info.province + addressBean.result.ad_info.city;
mapLocationBean.address = posBean.address.replace(s, "");
} else {
mapLocationBean.address = "";
}
if (fromMarker && originLaLng != null) {
float v = (float) TencentLocationUtils.distanceBetween(originLaLng.latitude, originLaLng.longitude, latLng.latitude, latLng.longitude);
posBean._distance = v + posBean._distance;
}
if (posBean._distance <= 100) {
mapLocationBean.distance = "100m以内";
} else if (posBean._distance > 1000) {
String s = posBean._distance / 1000 + "";
mapLocationBean.distance = StringUtils.getOneDecimal(s) + "km";
} else {
mapLocationBean.distance = posBean._distance + "m";
}
mapLocationBean.latitude = posBean.latLng.latitude;
mapLocationBean.longitude = posBean.latLng.longitude;
LogUtils.d(mapLocationBean.toString());
mapList.add(mapLocationBean);
}
}
view.loadSuccess(mapList);
} else {
view.onFailure(addressBean.status, addressBean.message);
}
}
}
@Override
public void onFailure(int i, String s, Throwable throwable) {
if (ProcessUtil.assertIsDestroy(view)) {
return;
}
view.onFailure(i, s);
}
});
}
二、设置附近的信息到列表
@Override
public void loadSuccess(List<MapLocationBean> list) {
if (page == 1) {
//设置默认数据
} else {
//翻页返回的数据
}
}
@Override
public void onFailure(int code, String msg) {
LogUtils.d(code + "---" + msg);
}
三、手势滑动绑定到列表数据
重新获取移动后marker所在的的屏幕坐标信息,转为地理位置坐标,再搜索附近信息,回调内容到上一步,更新UI数据。
private MapGestureListener mapGestureListener = new MapGestureListener() {
@Override
public boolean onUp(float v, float v1) {
LogUtils.d("手指抬起了");
if (markerPoint != null) {
showLoading();
position = tencentMap.getProjection().fromScreenLocation(markerPoint);
page = 1;
fromMarker = true;
getNearAddress(position, page, fromMarker);
}
return super.onUp(v, v1);
}
};
四、搜索功能实现
1、弹起软键盘,设置列表高度
private void keyBoardShowStatus() {
listView.setVisibility(View.INVISIBLE);
etSearch.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
etSearch.setPadding(UIUtils.dp2px(context, 10), 0, 0, 0);
etSearch.setFocusable(true);
etSearch.setFocusableInTouchMode(true);
etSearch.setCursorVisible(true);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) llBottom.getLayoutParams();
layoutParams.weight = 1.2f;
llBottom.setLayoutParams(layoutParams);
tvCancelSearch.setVisibility(View.VISIBLE);
etSearch.requestFocus();
KeyboardUtils.showKeyboard(etSearch);
etSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//搜索
fromMarker = true;
searchAddress(getContext(), etSearch.getText().toString().trim());
}
return false;
}
});
}
2、收回软键盘、恢复列表高度
private void keyBoardHiddenStatus() {
listView.setVisibility(View.VISIBLE);
etSearch.setGravity(Gravity.CENTER);
etSearch.setPadding(0, 0, 0, 0);
etSearch.setFocusable(false);
etSearch.setFocusableInTouchMode(false);
etSearch.setCursorVisible(false);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) llBottom.getLayoutParams();
layoutParams.weight = 0.4f;
llBottom.setLayoutParams(layoutParams);
tvCancelSearch.setVisibility(View.GONE);
etSearch.requestFocus();
KeyboardUtils.hideKeyboard(etSearch);
//etSearch.setImeOptions(EditorInfo.IME_NULL);
etSearch.setText("");
}
五、搜索指定的地址
public void searchAddress(Context context, String etText) {
if (TextUtils.isEmpty(etText) || originLaLng == null) {
return;
}
TencentSearch tencentSearch = new TencentSearch(context);
SearchParam searchParam = new SearchParam()
.keyword(etText)
.boundary(new SearchParam.Nearby(originLaLng, 10).autoExtend(true))
.orderby(true)
.pageIndex(1)
.pageSize(20);
tencentSearch.search(searchParam, new HttpResponseListener<SearchResultObject>() {
@Override
public void onSuccess(int code, SearchResultObject searchResultObject) {
if (searchResultObject == null || searchResultObject.status != 0) {
return;
}
ArrayList<MapLocationBean> list = new ArrayList<>();
List<SearchResultObject.SearchResultData> data = searchResultObject.data;
if (data != null) {
for (int i = 0; i < data.size(); i++) {
SearchResultObject.SearchResultData resultData = data.get(i);
MapLocationBean mapLocationBean = new MapLocationBean();
mapLocationBean.name = resultData.title;
if (resultData.ad_info != null && resultData.address != null) {
String s = resultData.ad_info.province + resultData.ad_info.city;
mapLocationBean.address = resultData.address.replace(s, "");
} else {
mapLocationBean.address = "";
}
float dist = 0;
if (originLaLng != null) {
dist = (float) TencentLocationUtils.distanceBetween(originLaLng.latitude, originLaLng.longitude, resultData.latLng.latitude, resultData.latLng.longitude);
}
if (dist <= 100) {
mapLocationBean.distance = "100m以内";
} else if (dist > 1000) {
mapLocationBean.distance = StringUtils.getOneDecimal(dist / 1000 + "") + "km";
} else {
mapLocationBean.distance = dist + "m";
}
mapLocationBean.latitude = resultData.latLng.latitude;
mapLocationBean.longitude = resultData.latLng.longitude;
LogUtils.d(mapLocationBean.toString());
list.add(mapLocationBean);
}
}
view.searchSuccess(list);
}
@Override
public void onFailure(int i, String s, Throwable throwable) {
LogUtils.d("搜索地址失败");
}
});
}
六、地址信息更新到列表
@Override
public void searchSuccess(List<MapLocationBean> list) {
if (list.isEmpty()) {
return;
}
keyBoardHiddenStatus();
//更新数据列表
}
以上是关于Android仿微信地图定位和位置选择(下)的主要内容,如果未能解决你的问题,请参考以下文章