本文主要是介绍百度定位附近位置功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现类似的附近位置功能
demo http://download.csdn.net/detail/u013134722/9732998
上代码
//添加定位
public class GetLocationActivity extends Activity implements OnClickListener,
OnGetSuggestionResultListener, BDLocationListener {
private EditText input_position;
private ListView lvTop, lvNear;// 选择的地址,附近的地址
private TextView current_addfess, current_city;
private ImageView iv_refresh, iv_search;
private ImageView release_content_back;
//
private SuggestionSearch mSuggestionSearch = null;
private PositionAdapter topAdapter, buttomAdater;
private boolean isRefreshTop;// false 刷新下面的附近数据 true 刷新search的数据
// 测试二
private PoiSearch mPoiSearch;
protected PoiResult searchResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.near_address);
initView();
//
mSuggestionSearch = SuggestionSearch.newInstance();
mSuggestionSearch.setOnGetSuggestionResultListener(this);
//
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
startBaiduLocation();
}
public void initView() {
findViewById(R.id.release_content_back).setOnClickListener(this);
input_position = (EditText) findViewById(R.id.input_position);
lvTop = (ListView) findViewById(R.id.lsitview_top);
lvNear = (ListView) findViewById(R.id.below_listview);
current_addfess = (TextView) findViewById(R.id.current_addfess);
current_city = (TextView) findViewById(R.id.current_city);
iv_refresh = (ImageView) findViewById(R.id.iv_refresh);
iv_search = (ImageView) findViewById(R.id.iv_search);
//
iv_refresh.setOnClickListener(this);
iv_search.setOnClickListener(this);
// shezhiadapter
topAdapter = new PositionAdapter();
buttomAdater = new PositionAdapter();
lvTop.setAdapter(topAdapter);
lvNear.setAdapter(buttomAdater);
// 初始化 要显示的数据
current_addfess.setText("正在定位...");
}
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
@Override
public void onGetPoiResult(PoiResult result) {
// 获取POI检索结果
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
// Toast.makeText(SearchOnMap.this,
// getResources().getString(R.string.search_map_no_key_info)/*"未搜索到相关信息!请更换其他关键字"*/,
// 15)
// .show();
return;// searchResult.getAllPoi().get(position).name
}
List<MyLocationModel> mList = new ArrayList<MyLocationModel>();
for (int i = 0; i < result.getAllPoi().size(); i++) {// 将搜索数据设置到适配器
PoiInfo pf = result.getAllPoi().get(i);
Log.i("after", " 实际数据 " + pf.address
+ " " + pf.city + " " + pf.name
+ " ");
MyLocationModel mData = new MyLocationModel(pf, true);
Log.i("after", "onGetPoiResult " + mData.toString());
mList.add(mData);
}
if (isRefreshTop) {
topAdapter.setData(true, mList);
topAdapter.notifyDataSetChanged();
} else {
buttomAdater.setData(true, mList);
buttomAdater.notifyDataSetChanged();
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
return;
} else {
Toast.makeText(
GetLocationActivity.this,
getResources().getString(
R.string.search_map_search_error)/* "搜索出错,请稍后再试!" */,
15).show();
}
}
@Override
public void onGetPoiDetailResult(PoiDetailResult result) {
// 获取Place详情页检索结果
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv_refresh:
iv_refresh.setClickable(false);
isRefreshTop = false;
current_addfess.setText("正在定位...");
startBaiduLocation();
break;
case R.id.iv_search:
// iv_search.setClickable(false);
isRefreshTop = true;
String strCity = current_city.getText().toString();
String strSearchWord = input_position.getText().toString().trim();
if (TextUtils.isEmpty(strSearchWord)) {
return;
}
isRefreshTop = true;
// mSuggestionSearch.requestSuggestion((new
// SuggestionSearchOption())
// .keyword(strSearchWord+",").city(strCity));
mPoiSearch.searchInCity((new PoiCitySearchOption()).city(strCity)
.keyword(strSearchWord).pageNum(0));
break;
case R.id.release_content_back:
// startActivity(new Intent(this, StartApp.class));
finish();
break;
default:
break;
}
}
@Override
public void onGetSuggestionResult(SuggestionResult res) { //这种方式也可以实现
// TODO Auto-generated method stub
if (res == null || res.getAllSuggestions() == null) {
return;
}
List<MyLocationModel> mList = new ArrayList<MyLocationModel>();
for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
if (info.key != null)
Log.i("after", "onGetSuggestionResult " + info.key
+ " " + info.district + " "
+ info.city);
MyLocationModel mData = new MyLocationModel();
mData.setAddressDetail("" + info.key);
mData.setCityName("" + info.city + info.district);
mList.add(mData);
}
buttomAdater.setData(true, mList);
buttomAdater.notifyDataSetChanged();
}
// 首次进入 后定位
private LocationClient mLocationClient;
public void startBaiduLocation() {
// Log.i("after", "StartLocation S");
mLocationClient = new LocationClient(getApplicationContext());
try {
// if (!isOpenLocation) // 如果没有打开
// {
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向;
option.setOpenGps(true);// 设置是否打开gps,使用gps前提是用户硬件打开gps。默认是不打开gps的
option.setProdName(HJAppConfig.CookieName + "Lo");// 设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(this);// 注册监听函数
// isOpenLocation = true; // 标识为已经打开了定位
mLocationClient.start();
Log.i("after", "StartLocation Success");
// }
} catch (Exception e) {
Log.i("after", "打开定位异常" + e.toString());
iv_refresh.setClickable(true);
}
Log.i("after", "StartLocation E");
}
// 定位回调方法
@Override
public void onReceiveLocation(BDLocation arg0) {// /设置刷新 并且开启搜索 ,然后停止
// TODO Auto-generated method stub
// addressDetail = location.getAddrStr();
// cityName = location.getCity();
// Street = location.getStreet() + location.getStreetNumber();
// current_addfess, current_city;
if (arg0 != null) {
isRefreshTop = false;
current_addfess.setText("" + arg0.getAddrStr());
current_city.setText("" + arg0.getCity());
// 开启搜索
mPoiSearch.searchInCity((new PoiCitySearchOption())
.city("" + arg0.getCity()).keyword("" + arg0.getAddrStr())
.pageNum(0));
mLocationClient.stop();
} else {
}
iv_refresh.setClickable(true);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationClient.unRegisterLocationListener(this);
mLocationClient.stop();
}
public class PositionAdapter extends BaseAdapter {
List<MyLocationModel> mList = new ArrayList<MyLocationModel>();
public void setData(boolean isClear, List<MyLocationModel> data) {
if (isClear) {
mList.clear();
mList.addAll(data);
} else {
mList.addAll(data);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList != null ? mList.size() : 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(
GetLocationActivity.this.getApplicationContext())
.inflate(R.layout.nearlist_item, null);
holder = new ViewHolder();
holder.tv_title = (TextView) convertView
.findViewById(R.id.tv_title);
holder.tv_content = (TextView) convertView
.findViewById(R.id.tv_content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// PoiInfo mPoiInfo=
final MyLocationModel myModle = mList.get(position);
holder.tv_title.setText("" + myModle.getAddressDetail());
holder.tv_content.setText("" + myModle.getCityName()
+ myModle.getAreaName());
// convertView.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// }
// });
return convertView;
}
}
public class ViewHolder {
TextView tv_title;
TextView tv_content;
}
}
这篇关于百度定位附近位置功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!