package com.qg.lizhanqi.baidumapdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; 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.LogoPosition; 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; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private MapView mMapView; private MapView bmapView; private Button btn_location; private Button btn_traffic; private Button btn_moon; private Button btn_normal; private Button btn_radar; private Button btn_navigation; private BaiduMap mBaiduMap; private Button btn_gap; private Button btn_Heat; boolean isFirstIn = true; private LocationClient mLocationClient; private LatLng latLng;//经纬度数据 private BitmapDescriptor mIconLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在使用SDK各组件之前初始化context信息,传入ApplicationContext //注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); initView(); //更改百度logo的位置 mMapView.setLogoPosition(LogoPosition.logoPostionRightBottom); MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); mBaiduMap.setMapStatus(msu); //定位到当前 location(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_location://定位 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);//更新我的状态位置数据 mBaiduMap.animateMapStatus(msu);//动画效果的回到当前位置,避免了效果过硬的看起来不太友好 break; case R.id.btn_radar://雷达 break; case R.id.btn_navigation://导航 break; case R.id.btn_traffic: //开启实时交通图 mBaiduMap.setTrafficEnabled(true); Toast.makeText(MainActivity.this, "打开了实时交通图", Toast.LENGTH_SHORT).show(); break; case R.id.btn_moon: //卫星地图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); Toast.makeText(MainActivity.this, "打开了卫星地图", Toast.LENGTH_SHORT).show(); break; case R.id.btn_normal: //普通地图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); Toast.makeText(MainActivity.this, "打开了普通地图", Toast.LENGTH_SHORT).show(); break; case R.id.btn_gap: //空白地图, 基础地图瓦片将不会被渲染。在地图类型中设置为NONE,将不会使用流量下 // 载基础地图瓦片图层。使用场景:与瓦片图层一起使用,节省流量,提升自定义瓦片图下载速度。 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE);//空白地图指的是只显示道路而不显示其他任何标志与建筑物 Toast.makeText(MainActivity.this, "打开了空白地图", Toast.LENGTH_SHORT).show(); break; case R.id.btn_Heat: if (mBaiduMap.isBaiduHeatMapEnabled()) {//如果打开就关闭,如果关闭就打开 btn_Heat.setText("打开热力图"); mBaiduMap.setBaiduHeatMapEnabled(false);//关闭热力图 Toast.makeText(MainActivity.this, "关闭热力图", Toast.LENGTH_SHORT).show(); } else { //开启热力图 mBaiduMap.setBaiduHeatMapEnabled(true); btn_Heat.setText("关闭热力图"); Toast.makeText(MainActivity.this, "打开热力图", Toast.LENGTH_SHORT).show(); } break; } } private void location() { mLocationClient = new LocationClient(this);//创建定位端 MyLocationListener myLocationListener = new MyLocationListener();//创建实现BDLocationListener的类 //定位端的监听器 mLocationClient.registerLocationListener(myLocationListener); //通过LocationClientOption对象给mLocationClient定位设置重要设置 LocationClientOption mLocationClientOption = new LocationClientOption(); mLocationClientOption.setCoorType("bd09ll");//坐标类型 mLocationClientOption.setIsNeedAddress(true);//返回当前的位置 mLocationClientOption.setOpenGps(true);//打开GPS mLocationClientOption.setScanSpan(1000);//每隔多少秒定位一次 mLocationClient.setLocOption(mLocationClientOption); } //定位成功的回调 private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation bdLocation) { MyLocationData locData = new MyLocationData.Builder() .accuracy(bdLocation.getRadius())//精度 .direction((float) bdLocation.getLatitude())//方向 .longitude(bdLocation.getLongitude()) .build();//创建当前位置数据 mBaiduMap.setMyLocationData(locData);//设置当前位置 // 初始化图标 BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.locationicon); // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标) //MyLocationConfiguration.LocationMode.COMPASS//罗盘模式,FOLLOWING跟随模式 MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker); mBaiduMap.setMyLocationConfigeration(config); //获得经纬度坐标 latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()); if (isFirstIn) {//用户第一次进来后定位到当前,因为会实时定位不需要每次定位都设置到当前位置,否则的话用户拖拽到其他位置还会被拉回来,所以又一次就行了 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);//更新我的状态位置数据 mBaiduMap.animateMapStatus(msu);//动画效果的回到当前位置,避免了效果过硬的看起来不太友好 Toast.makeText(MainActivity.this, "你当前在:" + bdLocation.getAddrStr(), Toast.LENGTH_SHORT).show(); isFirstIn = false; } } } @Override protected void onStart() { super.onStart(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); if (!mLocationClient.isStarted()) { mLocationClient.start(); } } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); if (mLocationClient.isStarted()) { // 当不需要定位图层时关闭定位图层 mBaiduMap.setMyLocationEnabled(false); } } private void initView() { //获取地图控件引用 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); btn_location = (Button) findViewById(R.id.btn_location); btn_traffic = (Button) findViewById(R.id.btn_traffic); btn_moon = (Button) findViewById(R.id.btn_moon); btn_normal = (Button) findViewById(R.id.btn_normal); btn_radar = (Button) findViewById(R.id.btn_radar); btn_navigation = (Button) findViewById(R.id.btn_navigation); btn_location.setOnClickListener(this); btn_traffic.setOnClickListener(this); btn_moon.setOnClickListener(this); btn_normal.setOnClickListener(this); btn_radar.setOnClickListener(this); btn_navigation.setOnClickListener(this); btn_gap = (Button) findViewById(R.id.btn_gap); btn_gap.setOnClickListener(this); btn_Heat = (Button) findViewById(R.id.btn_Heat); btn_Heat.setOnClickListener(this); } }
时间: 2024-12-13 01:30:03