首页 > 代码库 > Android_百度地图基本用法3
Android_百度地图基本用法3
1 /*
2 *1.实现定位
3 *2.引入方向传感器
4 *3.自定义位置图标
5 * */
6 public class MainActivity extends Activity {
7 MapView mapView;
8 BaiduMap mBaiduMap;
9 // 定位相关
10 private LocationClient mLocationClient;
11 private MyLocationListener mLocationListener;
12 private boolean isFirst = true;
13 // 记录经纬度
14 private double mLatitude;
15 private double mLongtitude;
16 // 自定义位置图标
17 private BitmapDescriptor mIconLocation;
18 private MyOrientationListener myOrientationListener;
19 private float mCurrentX;
20
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 SDKInitializer.initialize(getApplicationContext());
25 requestWindowFeature(Window.FEATURE_NO_TITLE);
26 setContentView(R.layout.fragment_main);
27 mapView = (MapView) findViewById(R.id.bmapView);
28 mBaiduMap = mapView.getMap();
29 // 设置地图标尺500m
30 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
31 mBaiduMap.setMapStatus(msu);
32
33 // 初始化定位
34
35 mLocationClient = new LocationClient(this);
36 mLocationListener = new MyLocationListener();
37 mLocationClient.registerLocationListener(mLocationListener);
38 LocationClientOption option = new LocationClientOption();
39 option.setCoorType("bd09ll");// 坐标类型
40 option.setIsNeedAddress(true);
41 option.setOpenGps(true);
42 option.setScanSpan(1000);// 每隔1秒请求
43 mLocationClient.setLocOption(option);
44
45 // 初始化图标
46 mIconLocation = BitmapDescriptorFactory
47 .fromResource(R.drawable.navi_map_gps_locked);
48 myOrientationListener = new MyOrientationListener(this);
49 myOrientationListener
50 .setOnOrientationListener(new OnOrientationListener() {
51
52 @Override
53 public void onOrientationChanged(float X) {
54 // TODO Auto-generated method stub
55 mCurrentX = X;
56 }
57 });
58
59 }
60
61 public void but(View view) {
62 switch (view.getId()) {
63
64 // 定位到我的位置
65 case R.id.back:
66 LatLng latLng = new LatLng(mLatitude, mLongtitude);
67 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
68 mBaiduMap.animateMapStatus(msu);
69
70 break;
71
72 }
73
74 }
75
76 @Override
77 protected void onResume() {
78
79 super.onResume();
80 mapView.onResume();
81 }
82
83 @Override
84 protected void onStart() {
85
86 super.onStart();
87 // 开启定位
88 mBaiduMap.setMyLocationEnabled(true);
89 if (!mLocationClient.isStarted()) {
90 mLocationClient.start();
91 }
92 // 开启方向传感器
93 myOrientationListener.start();
94
95 }
96
97 @Override
98 protected void onStop() {
99 // TODO Auto-generated method stub
100 super.onStop();
101 // 停止定位
102 mBaiduMap.setMyLocationEnabled(false);
103 mLocationClient.stop();
104 // 停止方向传感器
105 myOrientationListener.stop();
106
107 }
108
109 @Override
110 protected void onPause() {
111 // TODO Auto-generated method stub
112 super.onPause();
113 mapView.onPause();
114 }
115
116 @Override
117 protected void onDestroy() {
118 // TODO Auto-generated method stub
119 super.onDestroy();
120 mapView.onDestroy();
121 }
122
123 private class MyLocationListener implements BDLocationListener {
124
125 @Override
126 public void onReceiveLocation(BDLocation arg0) {
127 // 精度、纬度
128 MyLocationData data = http://www.mamicode.com/new MyLocationData.Builder()
129 .direction(mCurrentX).accuracy(arg0.getRadius())
130 .latitude(arg0.getLatitude())
131 .longitude(arg0.getLongitude()).build();
132 mBaiduMap.setMyLocationData(data);
133
134 // 设置自定义图标
135 MyLocationConfiguration config = new MyLocationConfiguration(
136 com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL,
137 true, mIconLocation);
138 mBaiduMap.setMyLocationConfigeration(config);
139
140 // 更新经纬度
141 mLatitude = arg0.getLatitude();
142 mLongtitude = arg0.getLongitude();
143 // 第一次进入,设置用户当前位置
144 if (isFirst) {
145 LatLng latLng = new LatLng(arg0.getLatitude(),
146 arg0.getLongitude());
147 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
148 mBaiduMap.animateMapStatus(msu);
149 isFirst = false;
150 // 显示地址
151 Toast.makeText(MainActivity.this, arg0.getAddrStr(), 0).show();
152
153 }
154
155 }
156
157 @Override
158 public void onConnectHotSpotMessage(String arg0, int arg1) {
159 // TODO Auto-generated method stub
160
161 }
162
163 }
164
165 }
1 public class MyOrientationListener implements SensorEventListener {
2 private SensorManager mSensorManager;
3 private Context mContext;
4 private Sensor mSensor;
5
6 private float lastX;
7
8 public MyOrientationListener(Context Context) {
9 this.mContext = Context;
10 }
11 //开始监听
12 public void start() {
13 mSensorManager = (SensorManager) mContext
14 .getSystemService(Context.SENSOR_SERVICE);
15 if (mSensorManager != null) {
16 // 获得方向传感器
17 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
18
19 }
20 if (mSensor != null) {
21 mSensorManager.registerListener(this, mSensor,
22 SensorManager.SENSOR_DELAY_UI);
23 }
24 else {
25 Toast.makeText(mContext, "没有方向传感器", 1).show();
26 }
27
28 }
29
30 public void stop() {
31 mSensorManager.unregisterListener(this);
32
33 }
34
35 @Override
36 public void onSensorChanged(SensorEvent event) {
37
38 if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
39 float x = event.values[SensorManager.DATA_X];
40 if (Math.abs(x - lastX) > 1.0) {
41 if (mOnOrientationListener != null) {
42 mOnOrientationListener.onOrientationChanged(x);
43 }
44 }
45 lastX = x;
46 }
47 }
48
49 @Override
50 public void onAccuracyChanged(Sensor sensor, int accuracy) {
51
52 }
53
54 private OnOrientationListener mOnOrientationListener;
55
56 public void setOnOrientationListener(
57 OnOrientationListener mOnOrientationListener) {
58 this.mOnOrientationListener = mOnOrientationListener;
59 }
60
61 public interface OnOrientationListener {
62 void onOrientationChanged(float X);
63 }
64 }
Android_百度地图基本用法3
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。