百度定位附近位置功能

2024-08-29 15:48

本文主要是介绍百度定位附近位置功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现类似的附近位置功能

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;
    }

}


这篇关于百度定位附近位置功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1118286

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.