Android_百度地图基本用法3

农村四月闲人少,勤学苦攻把名扬。这篇文章主要讲述Android_百度地图基本用法3相关的知识,希望能为你提供帮助。

1 /* 2*1.实现定位 3*2.引入方向传感器 4*3.自定义位置图标 5* */ 6 public class MainActivity extends Activity { 7MapView mapView; 8BaiduMap mBaiduMap; 9// 定位相关 10private LocationClient mLocationClient; 11private MyLocationListener mLocationListener; 12private boolean isFirst = true; 13// 记录经纬度 14private double mLatitude; 15private double mLongtitude; 16// 自定义位置图标 17private BitmapDescriptor mIconLocation; 18private MyOrientationListener myOrientationListener; 19private float mCurrentX; 20 21@Override 22protected void onCreate(Bundle savedInstanceState) { 23super.onCreate(savedInstanceState); 24SDKInitializer.initialize(getApplicationContext()); 25requestWindowFeature(Window.FEATURE_NO_TITLE); 26setContentView(R.layout.fragment_main); 27mapView = (MapView) findViewById(R.id.bmapView); 28mBaiduMap = mapView.getMap(); 29// 设置地图标尺500m 30MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); 31mBaiduMap.setMapStatus(msu); 32 33// 初始化定位 34 35mLocationClient = new LocationClient(this); 36mLocationListener = new MyLocationListener(); 37mLocationClient.registerLocationListener(mLocationListener); 38LocationClientOption option = new LocationClientOption(); 39option.setCoorType("bd09ll"); // 坐标类型 40option.setIsNeedAddress(true); 41option.setOpenGps(true); 42option.setScanSpan(1000); // 每隔1秒请求 43mLocationClient.setLocOption(option); 44 45// 初始化图标 46mIconLocation = BitmapDescriptorFactory 47.fromResource(R.drawable.navi_map_gps_locked); 48myOrientationListener = new MyOrientationListener(this); 49myOrientationListener 50.setOnOrientationListener(new OnOrientationListener() { 51 52@Override 53public void onOrientationChanged(float X) { 54// TODO Auto-generated method stub 55mCurrentX = X; 56} 57}); 58 59} 60 61public void but(View view) { 62switch (view.getId()) { 63 64// 定位到我的位置 65case R.id.back: 66LatLng latLng = new LatLng(mLatitude, mLongtitude); 67MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng); 68mBaiduMap.animateMapStatus(msu); 69 70break; 71 72} 73 74} 75 76@Override 77protected void onResume() { 78 79super.onResume(); 80mapView.onResume(); 81} 82 83@Override 84protected void onStart() { 85 86super.onStart(); 87// 开启定位 88mBaiduMap.setMyLocationEnabled(true); 89if (!mLocationClient.isStarted()) { 90mLocationClient.start(); 91} 92// 开启方向传感器 93myOrientationListener.start(); 94 95} 96 97@Override 98protected void onStop() { 99// TODO Auto-generated method stub 100super.onStop(); 101// 停止定位 102mBaiduMap.setMyLocationEnabled(false); 103mLocationClient.stop(); 104// 停止方向传感器 105myOrientationListener.stop(); 106 107} 108 109@Override 110protected void onPause() { 111// TODO Auto-generated method stub 112super.onPause(); 113mapView.onPause(); 114} 115 116@Override 117protected void onDestroy() { 118// TODO Auto-generated method stub 119super.onDestroy(); 120mapView.onDestroy(); 121} 122 123private class MyLocationListener implements BDLocationListener { 124 125@Override 126public void onReceiveLocation(BDLocation arg0) { 127// 精度、纬度 128MyLocationData data = https://www.songbingjia.com/android/new MyLocationData.Builder() 129.direction(mCurrentX).accuracy(arg0.getRadius()) 130.latitude(arg0.getLatitude()) 131.longitude(arg0.getLongitude()).build(); 132mBaiduMap.setMyLocationData(data); 133 134// 设置自定义图标 135MyLocationConfiguration config = new MyLocationConfiguration( 136com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL, 137true, mIconLocation); 138mBaiduMap.setMyLocationConfigeration(config); 139 140// 更新经纬度 141mLatitude = arg0.getLatitude(); 142mLongtitude = arg0.getLongitude(); 143// 第一次进入,设置用户当前位置 144if (isFirst) { 145LatLng latLng = new LatLng(arg0.getLatitude(), 146arg0.getLongitude()); 147MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng); 148mBaiduMap.animateMapStatus(msu); 149isFirst = false; 150// 显示地址 151Toast.makeText(MainActivity.this, arg0.getAddrStr(), 0).show(); 152 153} 154 155} 156 157@Override 158public void onConnectHotSpotMessage(String arg0, int arg1) { 159// TODO Auto-generated method stub 160 161} 162 163} 164 165 }

 
1 public class MyOrientationListener implements SensorEventListener { 2private SensorManager mSensorManager; 3private Context mContext; 4private Sensor mSensor; 5 6private float lastX; 7 8public MyOrientationListener(Context Context) { 9this.mContext = Context; 10} 11//开始监听 12public void start() { 13mSensorManager = (SensorManager) mContext 14.getSystemService(Context.SENSOR_SERVICE); 15if (mSensorManager != null) { 16// 获得方向传感器 17mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 18 19} 20if (mSensor != null) { 21mSensorManager.registerListener(this, mSensor, 22SensorManager.SENSOR_DELAY_UI); 23} 24else { 25Toast.makeText(mContext, "没有方向传感器", 1).show(); 26} 27 28} 29 30public void stop() { 31mSensorManager.unregisterListener(this); 32 33} 34 35@Override 36public void onSensorChanged(SensorEvent event) { 37 38if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { 39float x = event.values[SensorManager.DATA_X]; 40if (Math.abs(x - lastX) > 1.0) { 41if (mOnOrientationListener != null) { 42mOnOrientationListener.onOrientationChanged(x); 43} 44} 45lastX = x; 46} 47} 48 49@Override 50public void onAccuracyChanged(Sensor sensor, int accuracy) { 51 52} 53 54private OnOrientationListener mOnOrientationListener; 55 56public void setOnOrientationListener( 57OnOrientationListener mOnOrientationListener) { 58this.mOnOrientationListener = mOnOrientationListener; 59} 60 61public interface OnOrientationListener { 62void onOrientationChanged(float X); 63} 64 }

【Android_百度地图基本用法3】 

    推荐阅读