1 /*
2 *1.进行具体定位
3 *2.回到我的位置
4 *
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 @Override
18 protected void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 SDKInitializer.initialize(getApplicationContext());
21 requestWindowFeature(Window.FEATURE_NO_TITLE);
22 setContentView(R.layout.fragment_main);
23 mapView = (MapView) findViewById(R.id.bmapView);
24 mBaiduMap = mapView.getMap();
25 // 设置地图标尺500m
26 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
27 mBaiduMap.setMapStatus(msu);
28
29 // 初始化定位
30
31 mLocationClient = new LocationClient(this);
32 mLocationListener = new MyLocationListener();
33 mLocationClient.registerLocationListener(mLocationListener);
34 LocationClientOption option = new LocationClientOption();
35 option.setCoorType("bd09ll");// 坐标类型
36 option.setIsNeedAddress(true);
37 option.setOpenGps(true);
38 option.setScanSpan(1000);// 每隔1秒请求
39 mLocationClient.setLocOption(option);
40
41 }
42
43 public void but(View view) {
44 switch (view.getId()) {
45
46 // 定位到我的位置
47 case R.id.back:
48 LatLng latLng = new LatLng(mLatitude, mLongtitude);
49 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
50 mBaiduMap.animateMapStatus(msu);
51
52 break;
53
54 }
55
56 }
57
58 @Override
59 protected void onResume() {
60
61 super.onResume();
62 mapView.onResume();
63 }
64
65 @Override
66 protected void onStart() {
67
68 super.onStart();
69 // 开启定位
70 mBaiduMap.setMyLocationEnabled(true);
71 if (!mLocationClient.isStarted()) {
72 mLocationClient.start();
73 }
74
75 }
76
77 @Override
78 protected void onStop() {
79 // TODO Auto-generated method stub
80 super.onStop();
81 // 停止定位
82 mBaiduMap.setMyLocationEnabled(false);
83 mLocationClient.stop();
84
85 }
86
87 @Override
88 protected void onPause() {
89 // TODO Auto-generated method stub
90 super.onPause();
91 mapView.onPause();
92 }
93
94 @Override
95 protected void onDestroy() {
96 // TODO Auto-generated method stub
97 super.onDestroy();
98 mapView.onDestroy();
99 }
100
101 private class MyLocationListener implements BDLocationListener {
102
103 @Override
104 public void onReceiveLocation(BDLocation arg0) {
105 // 精度、纬度
106 MyLocationData data = new MyLocationData.Builder()
107 .accuracy(arg0.getRadius()).latitude(arg0.getLatitude())
108 .longitude(arg0.getLongitude()).build();
109
110 mBaiduMap.setMyLocationData(data);
111 // 更新经纬度
112 mLatitude = arg0.getLatitude();
113 mLongtitude = arg0.getLongitude();
114 // 第一次进入,设置用户当前位置
115 if (isFirst) {
116 LatLng latLng = new LatLng(arg0.getLatitude(),
117 arg0.getLongitude());
118 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
119 mBaiduMap.animateMapStatus(msu);
120 isFirst = false;
121 // 显示地址
122 Toast.makeText(MainActivity.this, arg0.getAddrStr(), 0).show();
123
124 }
125
126 }
127
128 @Override
129 public void onConnectHotSpotMessage(String arg0, int arg1) {
130 // TODO Auto-generated method stub
131
132 }
133
134 }
135
136 }