调用百度地图SDK显示当前定位位置

根据经纬度显示位置和地址

public class LocationFragment extends BaseFragment implements View.OnClickListener, OnGetGeoCoderResultListener {

    @Override
    public View onCreateViewIfNull(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Intent intent = this.getActivityIntent();
        if (intent != null) {
            Bundle bundle = intent.getBundleExtra(KeyContants.ACTIVITYBUNDLE);
            if (bundle != null) {
                double latitude = bundle.getDouble(KeyContants.LATITUDE);
                double longitude = bundle.getDouble(KeyContants.LONGITUDE);
                if (latitude > 0 && longitude > 0) {
                    shopPoint = new LatLng(latitude, longitude);
                }
                shopAddress =bundle.getString(KeyContants.ADDRESSS);
            }
        }

        mParentView = inflater.inflate(R.layout.zeno_frag_location, null);
        ViewUtils.inject(this, mParentView);
        initView();
        initData();
        initListener();
        return mParentView;
    }

    @Override
    public TitleBarInfo onCreateTitleBar() {
        TitleBarInfo info = Factory.getInstance().getTitleResManager().createStandardWithBack(getActivity());
        info.setTitle("店铺位置");
        return info;
    }

    private void initView() {
        tvShopAddress = (TextView) View.inflate(getActivity(), R.layout.zeno_layout_address_marker, null);

        BaiduMapOptions mapOptions = new BaiduMapOptions().compassEnabled(false).zoomControlsEnabled(true).rotateGesturesEnabled(false);
        mMapView = new MapView(getActivity(), mapOptions);
        mRLMap.addView(mMapView, 0);

        // 获取当前定位城市
        GeoPoint gp = Factory.getInstance().getGeoMap().getGP();
        myPoint = new LatLng(gp.getLatitude(), gp.getLongitude());

        mGeoCoder = GeoCoder.newInstance();
        mGeoCoder.setOnGetGeoCodeResultListener(this);

        mBaiduMap = mMapView.getMap();

        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                //地图加载完成后再设置Bound
                hasLoaded=true;
                Logger.e("leon","onMapLoaded");
                //调整缩放比例,将定位和店铺坐标同时显示
                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                if(myPoint!=null && shopPoint!=null){
                    Logger.e("leon","onMapLoaded mypoint:"+myPoint.latitude+":"+myPoint.longitude+"   shop point:"+shopPoint.latitude+":"+shopPoint.longitude);
                    builder.include(myPoint).include(shopPoint);
                    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
                    Logger.e("leon","onMapLoaded zoom:"+mBaiduMap.getMapStatus().zoom);
                }

            }
        });

        // // 设置缩放级别
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.5f);
        mBaiduMap.setMapStatus(msu);

        //显示定位图标
        // 修改为自定义marker
        mCurrentMarker = BitmapDescriptorFactory
                .fromResource(R.drawable.zeno_ic_map_center_location);
        mBaiduMap
                .setMyLocationConfigeration(new MyLocationConfiguration(
                        mCurrentMode, true, mCurrentMarker));

        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        if (myPoint != null && myPoint.latitude > 0 && myPoint.longitude > 0) {
            setMyLocation(myPoint);
        } else {//重新获取定位坐标
            mLocClient = new LocationClient(getActivity());
            mLocClient.registerLocationListener(myListener);
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 打开gps
            option.setCoorType("bd09ll"); // 设置坐标类型
            option.setScanSpan(5000);
            mLocClient.setLocOption(option);
            mLocClient.start();
        }

        LatLng point = DEFAULT_LOCATION;
        if (shopPoint != null && shopPoint.latitude > 0 && shopPoint.longitude > 0) {
            point = shopPoint;
            //有店铺经纬度,则去取详细地址,并显示在地图上
            mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(shopPoint));
        } else {
            MsgToast.geToast().setLongMsg("该店铺尚未设置地图位置");
            if (myPoint != null && myPoint.latitude > 0 && myPoint.longitude > 0) {
                //没有店铺经纬度,显示当前定位
                point = myPoint;
            }
            moveTo(point);
        }
    }

    private void setMyLocation(LatLng point) {
        MyLocationData locData = new MyLocationData.Builder()
                .latitude(point.latitude)
                .longitude(point.longitude).build();
        mBaiduMap.setMyLocationData(locData);
    }

    private void initData() {

    }

    private void initListener() {
        mIVMyLocation.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mIVMyLocation == v) {
            //跳转到定位
            if (myPoint != null) {
                moveTo(myPoint);
            } else {
                if (mLocClient != null) {
                    mLocClient.start();
                }
            }
        }
    }

    private void moveTo(LatLng point) {
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(point);
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            // 没有找到
            moveTo(myPoint);
            return;
        }

        try {
            //找到了则移动到店铺地址
            String address = result.getAddress();
            //在地图上添加图标
            tvShopAddress.setText(!TextUtils.isEmpty(shopAddress)?shopAddress:address);
            MarkerOptions shopMarker = new MarkerOptions().position(result.getLocation()).icon(BitmapDescriptorFactory.fromView(tvShopAddress));
            mBaiduMap.addOverlay(shopMarker);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {

            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            myPoint = new LatLng(location.getLatitude(), location.getLongitude());
            setMyLocation(myPoint);

            //定位成功,停止定位
            if(mLocClient != null){
                mLocClient.stop();
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    @Override
    public void onPause() {
        // MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
        if (mMapView != null) {
            mMapView.onPause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
        if (mMapView != null) {
            mMapView.onResume();
        }
        super.onResume();
    }

    @Override
    public void onDestroy() {
        try {
            if (mGeoCoder != null) {
                mGeoCoder.destroy();
                mGeoCoder = null;
            }

            // 退出时销毁定位
            if(mLocClient!=null){
                mLocClient.stop();
            }

            // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
            if (mMapView != null) {
                mMapView.onDestroy();
                mMapView = null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    /**
     * 默认位置,公司坐标
     */
    private static final LatLng DEFAULT_LOCATION = new LatLng(22.518143, 113.930614);


    private View mParentView;

    private MapView mMapView = null;
    @ViewInject(R.id.zeno_mRLMap)
    private RelativeLayout mRLMap;

    private BaiduMap mBaiduMap = null;
    private LatLng myPoint;//当前定位
    private LatLng shopPoint;//店铺位置
    private String shopAddress;
    private TextView tvShopAddress;

    /**
     * 根据地理坐标查找地址
     */
    private GeoCoder mGeoCoder = null;

    // 定位相关
    @ViewInject(R.id.mIVMyLocation)
    private ImageView mIVMyLocation;

    private LocationClient mLocClient;
    public MyLocationListenner myListener = new MyLocationListenner();
    private MyLocationConfiguration.LocationMode mCurrentMode;
    private BitmapDescriptor mCurrentMarker;

    private boolean hasLoaded=false;
}

 

你可能感兴趣的:(百度地图,显示当前定位位置)