android集成百度地图定位、poi检索、marker标记功能,以及marker点击事件

获取开发这密钥以及下载相关的jar包百度开发者文档有详细的说明,不多说,直接上代码。

1.<com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

调用setContentView之前需要先调用
 SDKInitializer.initialize(getApplicationContext());
不出意外此时地图已经显示出来了。

2.定位:

  mBaiduMap.setMyLocationEnabled(true);
        LocationClient   mLocationClient = new LocationClient(this);
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置高精度定位定位模式
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        mLocationClient.setLocOption(option);
        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                mBaiduMap.setMyLocationEnabled(true);
                locations = location;
            // 构造定位数据
                MyLocationData locData = new MyLocationData.Builder()
                        .accuracy(location.getRadius())
                        // 此处设置开发者获取到的方向信息,顺时针0-360
                        .direction(0).latitude(location.getLatitude())
                        .longitude(location.getLongitude()).build();
                // 设置定位数据
                mBaiduMap.setMyLocationData(locData);
            // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
                BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
                        .fromResource(R.mipmap.ic_launcher);
                MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING, true, null);
                mBaiduMap.setMyLocationConfiguration(config);
                // 当不需要定位图层时关闭定位图层
            }
        });
        mLocationClient.start();

此时,完成定位功能,当前所在位置为地图中心点。


3.poi检索功能分三种,城市检索,周边检索,矩形区域检索

首先注册一个检索监听者,在回掉中标记marker点,灰色背景的代码即为设置marker点的方法,同时将检索位置的信息通过bundle传入marker信息中,方便对marker的后续操作。

    PoiSearch  mPoiSearch = PoiSearch.newInstance();
 OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
            public void onGetPoiResult(PoiResult result) {
                //获取POI检索结果
                if (result.getAllPoi() != null) {
                    mData = result.getAllPoi();
                    //定义Maker坐标点
                    for (PoiInfo p : mData) {
                        LatLng point = new LatLng(p.location.latitude, p.location.longitude);
//构建Marker图标
                        BitmapDescriptor bitmap = BitmapDescriptorFactory
                                .fromResource(R.mipmap.ic_launcher);
//构建MarkerOption,用于在地图上添加Marker
                        OverlayOptions option = new MarkerOptions()
                                .position(point)
                                .icon(bitmap);
                        Bundle bundle = new Bundle();
                        //info必须实现序列化接口
                        bundle.putParcelable("info", p);
                        mBaiduMap.addOverlay(option).setExtraInfo(bundle);
//在地图上添加Marker,并显示
                    }

                  
                }

            }

            public void onGetPoiDetailResult(PoiDetailResult result) {
                //获取Place详情页检索结果
                Log.e("result", result.toString());
            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        };
 mPoiSearch.setOnGetPoiSearchResultListener(poiListener);

周边检索:

 mPoiSearch.searchNearby((new PoiNearbySearchOption().pageCapacity(50)).radius(10000)
                            .location(new LatLng(locations.getLatitude(),locations.getLongitude()))
                            .keyword(et.getText().toString()));

城市检索:

  mPoiSearch.searchInCity((new PoiCitySearchOption().pageCapacity(50))
                            .city("烟台")
                            .keyword(et.getText().toString()));

矩形检索:

 LatLng southwest = new LatLng(locations.getLatitude() - 0.01, locations.getLongitude() - 0.012);// 西南
                    LatLng northeast = new LatLng(locations.getLatitude() + 0.01, locations.getLongitude() + 0.012);// 东北
                    mPoiSearch.searchInBound(new PoiBoundSearchOption().bound(new LatLngBounds.Builder().include(southwest).include(northeast).build()).keyword("酒店"));

4.为检索出的marker点设置点击事件,我这里设置的是点击弹出二维码记录marker点的位置信息

  mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                //从marker中获取info信息
                Bundle bundle = marker.getExtraInfo();
                PoiInfo infoUtil = (PoiInfo) bundle.getParcelable("info");
               // mSpeechSynthesizer.speak(infoUtil.name);
                ImageView tv = new ImageView(MapActivity.this);
                BitmapDescriptor bitmapDescriptor;
                bitmapDescriptor = BitmapDescriptorFactory.fromView(tv);
//infowindow位置
                LatLng latLng = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
//infowindow点击事件
                InfoWindow.OnInfoWindowClickListener listener = new InfoWindow.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick() {
                        //隐藏infowindow
                        mBaiduMap.hideInfoWindow();
                    }
                };
//显示infowindow,-47是偏移量,使infowindow向上偏移,不会挡住marker
                InfoWindow infoWindow = new InfoWindow(bitmapDescriptor, latLng, -107, listener);
                mBaiduMap.showInfoWindow(infoWindow);
                return true;
            }
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值