源码简介
Android三种定位方式的实现,GPS、百度定位、高德定位。
源码运行截图
代码片段:
复制
public class MainActivity extends Activity implements OnClickListener{ private TextView mTextView; private Button gpsBtn, baiduBtn, amapBtn; //gps private LocationManager gpsManager; //baidu private LocationClient baduduManager; //amap private LocationManagerProxy aMapManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.text); gpsBtn = (Button) findViewById(R.id.gps); baiduBtn = (Button) findViewById(R.id.baidu); amapBtn = (Button) findViewById(R.id.amap); gpsBtn.setOnClickListener(this); baiduBtn.setOnClickListener(this); amapBtn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.gps: if (gpsBtn.getText().toString().equals("开启GPS定位")) { startGps(); gpsBtn.setText("停止GPS定位"); } else { stopGps(); gpsBtn.setText("开启GPS定位"); } break; case R.id.baidu: if (baiduBtn.getText().toString().equals("开启百度定位")) { startBaidu(); baiduBtn.setText("停止百度定位"); } else { stopBaidu(); baiduBtn.setText("开启百度定位"); } break; case R.id.amap: if (amapBtn.getText().toString().equals("开启高德定位")) { startAmap(); amapBtn.setText("停止高德定位"); } else { stopAmap(); amapBtn.setText("开启高德定位"); } break; default: break; } } private void startAmap() { aMapManager = LocationManagerProxy.getInstance(this); /* * mAMapLocManager.setGpsEnable(false); * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location * API定位采用GPS和网络混合定位方式 * ,***个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者 */ aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000, 10, mAMapLocationListener); } private void stopAmap() { if (aMapManager != null) { aMapManager.removeUpdates(mAMapLocationListener); aMapManager.destory(); } aMapManager = null; } private void startBaidu() { if (baduduManager == null) { baduduManager = new LocationClient(this); //定位的配置 LocationClientOption option = new LocationClientOption(); //定位模式选择,高精度、省电、仅设备 option.setLocationMode(LocationMode.Hight_Accuracy); //定位坐标系类型选取, gcj02、bd09ll、bd09 option.setCoorType("gcj02"); //定位时间间隔 option.setScanSpan(1000); //选择定位到地址 option.setIsNeedAddress(true); baduduManager.setLocOption(option); //注册定位的成功的回调 baduduManager.registerLocationListener(mBdLocationListener); } baduduManager.start(); } private void stopBaidu() { baduduManager.stop(); } private void startGps() { // 获取到LocationManager对象 gpsManager = (LocationManager) getSystemService(LOCATION_SERVICE); //provider可为gps定位,也可为为基站和WIFI定位 String provider = gpsManager.getProvider(LocationManager.GPS_PROVIDER).getName(); //3000ms为定位的间隔时间,10m为距离变化阀值,gpsListener为回调接口 gpsManager.requestLocationUpdates(provider, 3000, 10, gpsListener); } private void stopGps() { gpsManager.removeUpdates(gpsListener); } // 创建位置监听器 private LocationListener gpsListener = new LocationListener() { // 位置发生改变时调用 @Override public void onLocationChanged(Location location) { Log.e("Location", "onLocationChanged"); double latitude = location.getLatitude(); double longitude = location.getLongitude(); float speed = location.getSpeed(); long time = location.getTime(); String s = "latitude--->" + latitude + " longitude--->" + longitude + " speed--->" + speed + " time--->" + new Date(time).toLocaleString(); mTextView.setText("GPS定位\n" + s); } // provider失效时调用 @Override public void onProviderDisabled(String provider) { Log.e("Location", "onProviderDisabled"); } // provider启用时调用 @Override public void onProviderEnabled(String provider) { Log.e("Location", "onProviderEnabled"); } // 状态改变时调用 @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.e("Location", "onStatusChanged"); } }; private BDLocationListener mBdLocationListener = new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { //Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); sb.append("\nlatitude : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); sb.append("\nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation){ sb.append("\nspeed : "); sb.append(location.getSpeed()); sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("\ndirection : "); sb.append("\naddr : "); sb.append(location.getAddrStr()); sb.append(location.getDirection()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append("\naddr : "); sb.append(location.getAddrStr()); sb.append("\noperationers : "); sb.append(location.getOperators()); } mTextView.setText("百度定位\n" + sb.toString()); } }; private AMapLocationListener mAMapLocationListener = new AMapLocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(Location location) { } @Override public void onLocationChanged(AMapLocation location) { if (location != null) { Double geoLat = location.getLatitude(); Double geoLng = location.getLongitude(); String cityCode = ""; String desc = ""; Bundle locBundle = location.getExtras(); if (locBundle != null) { cityCode = locBundle.getString("citycode"); desc = locBundle.getString("desc"); } String str = ("定位成功:(" + geoLng + "," + geoLat + ")" + "\n精 度 :" + location.getAccuracy() + "米" + "\n定位方式:" + location.getProvider() + "\n定位时间:" + new Date(location.getTime()).toLocaleString() + "\n城市编码:" + cityCode + "\n位置描述:" + desc + "\n省:" + location.getProvince() + "\n市:" + location.getCity() + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location .getAdCode()); mTextView.setText("高德定位\n" + str); } } }; }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
复制
源码链接:http://down.51cto.com/data/1968757
1.