百度地图(BaiduMap)定位工具类封装和使用

(转载请注明出处哦)本篇博客主要是提供思路,具体的百度地图权限和apikey配置不叙述,地图定位工具类如下

/**

 * @ClassName: LocationHelper

 * @author: hewei

 * @version: v1.0

 */

public class LocationHelper {

    private LocationCallBackcallBack;

    private static LocationHelperhelper;

 

    private LocationClientlocationClient;

    private BDLocationListenerlocationListener = new MyBDLocationListener();

 

    private LocationHelper() {

        //第一步实例化定位核心类

        locationClient = newLocationClient(BaseApp.getInstance(), getLocOption());

        //第二步设置位置变化回调监听

       locationClient.registerLocationListener(locationListener);

    }

 

    public static LocationHelpergetInstance() {

        if (helper == null) {

            helper = newLocationHelper();

        }

        return helper;

}

 

    public void start() {

//        第三步开始定位

        locationClient.start();

    }

 

//一般会在Activity的OnDestroy方法调用

    public void stop() {

        if (locationClient !=null) {

           locationClient.unRegisterLocationListener(locationListener);

           locationClient.stop();

            locationClient = null;

        }

    }

 

    private LocationClientOptiongetLocOption() {

        LocationClientOptionoption = new LocationClientOption();

       option.setAddrType("all");

        //设置定位坐标系

       option.setCoorType("bd09ll");

       //重新定位时间间隔

       //option.setScanSpan(60*1000);

        //设置是否打开gps

        option.setOpenGps(true);

      //设置定位模式

       option.setLocationNotify(true);

        //是否需要poi结果

        option.setPoiDistance(1000);

       option.setPoiExtraInfo(true);

        return option;

    }

 

    private classMyBDLocationListener implements BDLocationListener {

        @Override

        public voidonReceiveLocation(BDLocation bdLocation) {

            if (callBack != null&&bdLocation!=null){

               callBack.callBack(bdLocation.getAddrStr(),bdLocation.getStreetNumber(),bdLocation.getLatitude(),bdLocation.getLongitude(),bdLocation.getDistrict(),bdLocation.getStreet(),bdLocation.getCity(),bdLocation.getProvince());

            }

      //多次定位必须要调用stop方法

           locationClient.stop();

        }

 

           @Override

           public voidonReceivePoi(BDLocation arg0) {

                

           }

    }

 

    public interfaceLocationCallBack {

        void callBack(Stringaddr,String streetnumber ,double lat, double lng, String district ,Stringstreet,String city,String province);

    }

 

    public LocationCallBackgetCallBack() {

        return callBack;

    }

 

    public void setCallBack(LocationCallBackcallBack) {

        this.callBack = callBack;

    }

}

 

那我们如何调用定位工具呢,利用接口回调传递数据,覆盖物等功能请自行添加,具体如下

public class HomePageActivity extends BaseActivity implements LocationCallBack{

      private BaiduMap baiduMap;

      private LocationHelperhelper;

      private MapView mapView;

      private TextView addrtext;

 

      @Override

      protected voidonCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

                     setContentView(R.layout.activity_homepage_hasmenukey_layout);

           mapView=(MapView)findViewById(R.id.bmapView);

           addrtext=(TextView)findViewById(R.id.addrtext);

           setBaiduMap();

           startLocation();

      }

 

      //百度地图设置

      public void setBaiduMap(){

           //去掉百度地图自带的放大缩小按钮,以及左下角的小图标

           mapView.showZoomControls(false);

           mapView.showScaleControl(false);

           for (int i = 0; i <mapView.getChildCount(); i++) {

            View child =mapView.getChildAt(i);

            if (child instanceofImageView) {

               child.setVisibility(View.INVISIBLE);

                break;

            }

        }

           baiduMap=mapView.getMap();

           baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

           baiduMap.setMyLocationEnabled(true);

           baiduMap.setOnMapTouchListener(this);

           baiduMap.setOnMapLoadedCallback(this);

      }

 

      //定位返回的数据

      @Override

      public void callBack(Stringaddr, String streetnumber, double lat, double lng, String district,

           String street, Stringcity,String province) {

           addrtext.setText(addr);

      }

 

      //开启定位

      public void startLocation(){

           helper=LocationHelper.getInstance();

           helper.setCallBack(this);

           helper.start();

      }

 

 

      @Override

      public void onDestroy(){ 

           helper.stop();

           baiduMap.setMyLocationEnabled(false);

                   mapView.onDestroy();

                   mapView=null;

                   super.onDestroy();

}

 

  @Override 

    protected void onResume() { 

        super.onResume(); 

        mapView.onResume(); 

        } 

   

    @Override 

    protected void onPause() { 

        super.onPause(); 

        mapView.onPause(); 

        }

}

 

本篇博客主要提供的是思路,喜欢的朋友可以关注和收藏哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值