// 定位相关 LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner(); private MyLocationConfiguration.LocationMode mCurrentMode; BitmapDescriptor mCurrentMarker; // private static final int accuracyCircleFillColor = 0xAAFFFF88; // private static final int accuracyCircleStrokeColor = 0xAA00FF00; MapView mMapView; BaiduMap mBaiduMap; // UI相关 RadioGroup.OnCheckedChangeListener radioButtonListener; Button requestLocButton; boolean isFirstLoc = true; // 是否首次定位 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { SDKInitializer.initialize(getApplicationContext()); } catch (Exception e) { // TODO: handle exception ToastUtils.shortToast(getApplicationContext(), "地图api初始化失败"); return; } setContentView(R.layout.activity_zhi_yuan_jiu_zhu); initView(); mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL; mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.ditu_dingwei);//图标 // 地图初始化 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(getApplicationContext()); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000); mLocClient.setLocOption(option); mLocClient.start(); mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker, 0, 0)); } /** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(0).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } } public void onReceivePoi(BDLocation poiLocation) { } } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onResume() { mMapView.onResume(); super.onResume(); } @Override protected void onDestroy() { // 退出时销毁定位 mLocClient.stop(); // 关闭定位图层 mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; super.onDestroy(); }
百度地图--定位
最新推荐文章于 2022-09-07 19:53:58 发布