做法:
1.用的是LocationListener监听实时坐标。监听和计算等操作都在一个service里完成。
2.监听设置为实时监听。
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
3.有一个3秒间隔的的循环任务,任务就是计算3秒前后的2个实时坐标的距离,并根据距离和间隔时间计算速度。
结果:
计算出来的速度和实际速度相差太大。
设备静止时,获取到的坐标计算出来的速度也不为0。
我测试过三星手机和华为手机,都有类似的效果。
这个,,,什么原因呢,怎么破?
1.package an.android.application;
2.
3.
4.import java.util.Iterator;
5.
6.import android.app.Activity;
7.import android.content.Intent;
8.
9.import android.location.GpsSatellite;
10.import android.location.GpsStatus;
11.import android.location.Location;
12.import android.location.LocationListener;
13.import android.location.LocationManager;
14.import android.os.Bundle;
15.import android.util.Log;
16.import android.view.Menu;
17.import android.view.MenuItem;
18.import android.view.View;
19.import android.widget.Button;
20.import android.widget.Toast;
21.
22.public class SpeedToll extends Activity {
23. /** Called when the activity is first created. */
24.
25.
26. private static final int Search = Menu.FIRST;
27. private static final int Myloc = Menu.FIRST + 1;
28. private static final int Exit = Menu.FIRST + 2;
29.
30.
31. private LocationManager locationManager;
32. private GpsStatus gpsstatus;
33.
34.
35. @Override
36. public void onCreate(Bundle savedInstanceState) {
37.
38. super.onCreate(savedInstanceState);
39. /* 加载main.xml Layout */
40. setContentView(R.layout.desktop);
41. /* 以findViewById()取得Button对象,并加入onClickListener */
42. Button b1 = (Button) findViewById(R.id.button1);
43. b1.setOnClickListener(new Button.OnClickListener()
44. {
45. public void onClick(View v)
46. {
47. /* new一个Intent对象,并指定要启动的class */
48. Intent intent = new Intent();
49. intent.setClass(SpeedToll.this, Map.class);
50. /* 调用一个新的Activity */
51. startActivity(intent);
52. /* 关闭原本的Activity */
53. //SpeedToll.this.finish();
54. }
55. });
56.
57.
58. Button b2 = (Button) findViewById(R.id.button1);
59. b2.setOnClickListener(new Button.OnClickListener()
60. {
61. public void onClick(View v)
62. {
63. /* new一个Intent对象,并指定要启动的class */
64. Intent intent = new Intent();
65. intent.setClass(SpeedToll.this, Map.class);
66. /* 调用一个新的Activity */
67. startActivity(intent);
68. /* 关闭原本的Activity */
69. //SpeedToll.this.finish();
70. }
71. });
72. }
73.
74. public boolean onCreateOptionsMenu(Menu menu) {
75. super.onCreateOptionsMenu(menu);
76. menu.add(0, Search, Menu.NONE, "搜索");
77. menu.add(0, Myloc, Menu.NONE, "定位");
78. menu.add(0, Exit, Menu.NONE, "退出");
79.
80.
81. return true;
82. }//menu
83.
84. public boolean onOptionsItemSelected(MenuItem item)
85. {
86. //super.onOptionsItemSelected(item);
87. switch(item.getItemId()){
88. case Search:
89. //Search();
90. break;
91.
92. case Myloc:
93. GetMyLocation();
94. break;
95.
96. case Exit:
97. //mLocationManager.removeUpdates(this); //关闭GPS
98.
99. this.finish();
100. break;
101. }
102. return super.onOptionsItemSelected(item);
103. }
104.
105.
106. public boolean GetMyLocation(){
107. //获取到LocationManager对象
108. locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
109.
110. //根据设置的Criteria对象,获取最符合此标准的provider对象
111. String currentProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER).getName();
112.
113. //根据当前provider对象获取最后一次位置信息
114. Location currentLocation = locationManager.getLastKnownLocation(currentProvider);
115. //如果位置信息为null,则请求更新位置信息
116. if(currentLocation == null){
117. locationManager.requestLocationUpdates(currentProvider, 0, 0, locationListener);
118. }
119. //增加GPS状态监听器
120. locationManager.addGpsStatusListener(gpsListener);
121.
122. //直到获得最后一次位置信息为止,如果未获得最后一次位置信息,则显示默认经纬度
123. //每隔10秒获取一次位置信息
124. while(true){
125. currentLocation = locationManager.getLastKnownLocation(currentProvider);
126. if(currentLocation != null){
127. Log.d("Location", "Latitude: " + currentLocation.getLatitude());
128. Log.d("Location", "location: " + currentLocation.getLongitude());
129. Toast.makeText(SpeedToll.this, "Latitude: " + currentLocation.getLatitude()+ " "
130. +"location: " + currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
131. break;
132. }else{
133. Log.d("Location", "Latitude: " + 0);
134. Log.d("Location", "location: " + 0);
135. }
136. try {
137. Thread.sleep(10000);
138. } catch (InterruptedException e) {
139. Log.e("Location", e.getMessage());
140. }
141. }
142. return false;
143. }
144.
145. private GpsStatus.Listener gpsListener = new GpsStatus.Listener(){
146. //GPS状态发生变化时触发
147. @Override
148. public void onGpsStatusChanged(int event) {
149. //获取当前状态
150. gpsstatus=locationManager.getGpsStatus(null);
151. switch(event){
152. //第一次定位时的事件
153. case GpsStatus.GPS_EVENT_FIRST_FIX:
154. break;
155. //开始定位的事件
156. case GpsStatus.GPS_EVENT_STARTED:
157. break;
158. //发送GPS卫星状态事件
159. case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
160. Toast.makeText(SpeedToll.this, "GPS_EVENT_SATELLITE_STATUS", Toast.LENGTH_SHORT).show();
161. Iterable<GpsSatellite> allSatellites = gpsstatus.getSatellites();
162. Iterator<GpsSatellite> it=allSatellites.iterator();
163. int count = 0;
164. while(it.hasNext())
165. {
166. count++;
167. }
168. Toast.makeText(SpeedToll.this, "Satellite Count:" + count, Toast.LENGTH_SHORT).show();
169. break;
170. //停止定位事件
171. case GpsStatus.GPS_EVENT_STOPPED:
172. Log.d("Location", "GPS_EVENT_STOPPED");
173. break;
174. }
175. }
176. };
177.
178.
179. //创建位置监听器
180. private LocationListener locationListener = new LocationListener(){
181. //位置发生改变时调用
182. @Override
183. public void onLocationChanged(Location location) {
184. Log.d("Location", "onLocationChanged");
185. }
186.
187. //provider失效时调用
188. @Override
189. public void onProviderDisabled(String provider) {
190. Log.d("Location", "onProviderDisabled");
191. }
192.
193. //provider启用时调用
194. @Override
195. public void onProviderEnabled(String provider) {
196. Log.d("Location", "onProviderEnabled");
197. }
198.
199. //状态改变时调用
200. @Override
201. public void onStatusChanged(String provider, int status, Bundle extras) {
202. Log.d("Location", "onStatusChanged");
203. }
204.
205.
206. };
207.
208.}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。