百度地图集成 地图、定位、周边位置检索

开发中需要集成百度地图,用到三个功能 

1.点开地图定位当前位置。

2.地图中间点定位图标显示,滑动地图定位图标始终在地图中间。

3.根据上面定位图标,检索出附近位置信息。

效果图

拖动地图位置后

1.首先先申请百度账号,并申请AK,等信息并下载sdk 

官网地址

http://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-project/androidstudio

选择需要的服务,我用到了这三种服务

然后 开始集成,

第一步:根据手机版本申请权限,需要动态申请的动态申请。

功能1;打开界面定位;

     mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);


        //根据定位样式需求可以在这里进行设置
//        mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.location_img);
//        MyLocationConfiguration configuration = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker);
//        mBaiduMap.setMyLocationConfiguration(configuration);

        //定位初始化
        mLocationClient = new LocationClient(getApplicationContext());

      //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setIsNeedLocationDescribe(true);
        option.setIsNeedLocationPoiList(true);
        option.setIsNeedAddress(true);
//设置locationClientOption
        mLocationClient.setLocOption(option);
//注册LocationListener监听器
        MyLocationListener1 myLocationListener = new MyLocationListener1();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();
 public class MyLocationListener1 extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            StringBuilder currentPosition = new StringBuilder();
            //获取经纬度
            currentPosition.append("纬度:").append(location.getLatitude()).append("\n");
            currentPosition.append("经线:").append(location.getLongitude()).append("\n");
            //获取详细地址信息
            currentPosition.append("国家:").append(location.getCountry()).append("\n");
            currentPosition.append("省:").append(location.getProvince()).append("\n");
            currentPosition.append("市:").append(location.getCity()).append("\n");
            currentPosition.append("区:").append(location.getDistrict()).append("\n");
            currentPosition.append("街道:").append(location.getStreet()).append("\n");
            //获取定位方式
            currentPosition.append("定位方式:");
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                currentPosition.append("GPS 定位");
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                currentPosition.append("网络 定位");
            }

//            Log.e(TAG, "onReceiveLocation: " + currentPosition.toString());

//            Log.e(TAG, "onReceiveLocation: " + location.getLatitude() + location.getLongitude() +           location.getAddrStr());


            if (isFirstLocate) {
                LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
                mBaiduMap.animateMapStatus(update);
                update = MapStatusUpdateFactory.zoomTo(16f);
                mBaiduMap.animateMapStatus(update);
                isFirstLocate = false;
            }

            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);

            List<Poi> poiList = location.getPoiList();
            //这里也可以获取poi 但是这里只有5条数据
            for (Poi poi : poiList) {
//                Log.e(TAG, "onReceiveLocation: " + poi.getName());

            }
        }
    }

这里有个很重要的点就是需要打开手机的定位(位置信息)手动(下滑和控制屏幕旋转在一个级别),如果这个不打开的话,定位不能成功获取,包括百度地图的demo也是一样的。

问题二:地图中间点定位图标显示,滑动地图定位图标始终在地图中间

这个一开始 ,不知道是如何做到跟随的,开始用回调实现,但是定位图标会闪动,效果不好,

实际解决方法为在地图布局上放一个定位图标,始终在中间,完成了定位图标在中间的效果

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical">

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

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerInParent="true"
        android:paddingBottom="13dp"
        android:src="@mipmap/locationnew" />
</RelativeLayout>

问题三:根据上面定位图标,检索出附近位置信息

这个是用到了地理反编码的功能,监听地图中间点经纬度,再去反编码获取;

mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus, int i) {

    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        latLng = mapStatus.target;
        Log.e(TAG, "onMapStatusChangeFinish: " + latLng.latitude);
        searchNearBy(latLng);

    }
});
public void searchNearBy(LatLng latLng) {
    geoCoder = GeoCoder.newInstance();
    geoCoder.setOnGetGeoCodeResultListener(listener);

    geoCoder.reverseGeoCode(new ReverseGeoCodeOption()
            .location(latLng)
            .radius(1000));

}
从而实现了附近信息的搜索。

源码地址

https://download.csdn.net/download/rfgreeee/11150951

猜你喜欢

转载自blog.csdn.net/rfgreeee/article/details/89669558