百度地图--定位

第一步 :导入相关的包



第二步 :
权限……
第三步:
package com.allview.xjcbxt.activitys.baidumap;

import android.view.View;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.allview.xjcbxt.R;
import com.allview.xjcbxt.activitys.baidumap.MapConfig.MyOrientationListener;
import com.allview.xjcbxt.activitys.base.ActBase;
import com.allview.xjcbxt.commons.utils.AppToast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.navisdk.comapi.mapcontrol.MapParams;

import java.util.Timer;

/**
* Created by zhm on 2015/11/24.
* 百度定位
*/

public class LocationMapActivity extends ActBase {
private MapView mMapView;
private BaiduMap mBaiduMap;
private LocationClient mLocationClient;
private TextView tv_title;
private volatile boolean isFirstLoc = true;
// 自定义定位图标

private float mCurrentX;
private MapParams.Const.LocationMode mLocationMode;
private MyOrientationListener myOrientationListener;
private BitmapDescriptor mIconLocation;

//获取经纬度
private double mCurrentLantitude;
private double mCurrentLongitude;

@Override
public void init() {
    SDKInitializer.initialize(getApplicationContext());
    setContentView(R.layout.activity_locationmap);
}

@Override
public void setView() {
    mMapView = (MapView) findViewById(R.id.bmapView);
    mMapView.removeViewAt(1);//去除百度地图图片链接
    //去除放大镜和缩小镜
    int childCount = mMapView.getChildCount();
    View zoom = null;
    for (int i = 0; i < childCount; i++) {
        View child = mMapView.getChildAt(i);
        if (child instanceof ZoomControls) {
            zoom = child;
            break;
        }
    }
    zoom.setVisibility(View.GONE);

 /*   // 隐藏比例尺控件

    int count = mMapView.getChildCount();

    View scale = null;

    for (int i = 0; i < count; i++) {

        View child = mMapView.getChildAt(i);

        if (child instanceof ZoomControls) {

            scale = child;

            break;

        }

    }

    scale.setVisibility(View.GONE);

*/

    BaiDuInit();
}

public void BaiDuInit() {
    //初始化map
    mBaiduMap = mMapView.getMap();
    //开启定位图层
    mBaiduMap.setMyLocationEnabled(true);
    // 普通地图
    mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

    //定位初始化
    mLocationClient = new LocationClient(this);
    //监听注册
    mLocationClient.registerLocationListener(new MyLocationListener());
    // 定位操作属性设置
    LocationClientOption option = new LocationClientOption();
    option.setLocationMode(com.baidu.location.LocationClientOption.LocationMode.Hight_Accuracy);// 高精度;
    // Battery_Saving:低精度.
    option.setCoorType("bd09ll"); // 设置坐标类型 返回国测局经纬度坐标系:gcj02 返回百度墨卡托坐标系
    option.setOpenGps(true);// 是否开启GPS
    // 返回百度经纬度坐标系 :bd09ll
    option.setScanSpan(1000);// 设置扫描间隔,单位毫秒,当<1000(1s)时,定时定位无效
    option.setIsNeedAddress(true);// 设置是否需要地址信息,默认为无地址
    option.setNeedDeviceDirect(true);// 在网络定位时,是否需要设备方向
    mLocationClient.setLocOption(option);

    //1
    // 初始化图标
    mIconLocation = BitmapDescriptorFactory
            .fromResource(R.drawable.navi_map_gps_locked);
    //方位传感
    myOrientationListener = new MyOrientationListener(LocationMapActivity.this);
    myOrientationListener
            .setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
                @Override
                public void onOrientationChanged(float x) {
                    mCurrentX = x;

                }
            });

}

@Override
public void setListener() {

}

//事件监听
public class MyLocationListener implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation location) {
        // map view 销毁后不在处理新接收的位置
        if (location == null || mMapView == null) {
            AppToast.show_SHORT(LocationMapActivity.this, "定位失败,网络信号不好请重试!");
            return;
        }

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

        mBaiduMap.setMyLocationData(locData);
        //图标
        MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation);
        mBaiduMap.setMyLocationConfigeration(config);
        mCurrentLantitude = location.getLatitude();
        mCurrentLongitude = location.getLongitude();

        // 是否首次定位
        if (isFirstLoc) {
            isFirstLoc = false;
            LatLng ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
            mBaiduMap.animateMapStatus(u);
        }
    }

}

@Override
protected void onStart() {

    super.onStart();


    //开启定位
    mBaiduMap.setMyLocationEnabled(true);
    mLocationClient.start();
    if (!mLocationClient.isStarted()) {
        mLocationClient.start();
    }
    myOrientationListener.start();
}


@Override
protected void onPause() {
    super.onPause();
    //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
    mMapView.onPause();
}

@Override
protected void onStop() {
    super.onStop();

    mBaiduMap.setMyLocationEnabled(false);
    mLocationClient.stop();
    myOrientationListener.stop();
}

@Override
protected void onResume() {
    super.onResume();
    //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
    mMapView.onResume();
}

@Override
protected void onDestroy() {

    super.onDestroy();
    // 退出时销毁定位
    mLocationClient.stop();
    // 关闭定位图层
    mMapView.onDestroy();
    mMapView = null;
    mBaiduMap.setMyLocationEnabled(false);
}

}

效果:

你可能感兴趣的:(百度地图)