+ -
当前位置:首页 → 问答吧 → GPS无法自动启动

GPS无法自动启动

时间:2010-07-19

来源:互联网

网路上找到一个范例

是可以在mapview上划路线的程式

但是放在手机时

却无法自动开启GPS搜寻

导致找不到所在位置

是少写什么了吗??

(手机的GPS已设定启动)

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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.android.dara;
 
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;
import android.widget.Toast;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
 
public class mymap extends MapActivity
{ private TextView mTextView; private Button mButton01; private Button mButton02; private Button mButton03; private Button mButton04; private Button mButton05; private MapView mMapView; private MapController mMapController; private LocationManager mLocationManager; private Location mLocation; private String mLocationPrivider=""; private int zoomLevel=0; private GeoPoint gp1; private GeoPoint gp2; private boolean _run=false; private double distance=0;
  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); /* 建立MapView物件 */ mMapView = (MapView)findViewById(R.id.myMapView1); mMapController = mMapView.getController(); /* 物件初始化 */ mTextView = (TextView)findViewById(R.id.myText1); mButton01 = (Button)findViewById(R.id.myButton1); mButton02 = (Button)findViewById(R.id.myButton2); mButton03 = (Button)findViewById(R.id.myButton3); mButton04 = (Button)findViewById(R.id.myButton4); mButton05 = (Button)findViewById(R.id.myButtonreturn); /* 设定预设的放大层级 */ zoomLevel = 17; mMapController.setZoom(zoomLevel);   /* Provider初始化 */ mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); /* 取得Provider与Location */ getLocationPrivider(); if(mLocation!=null) { /* 取得目前的经纬度 */ gp1=getGeoByLocation(mLocation); gp2=gp1; /* 将MapView的中点移至目前位置 */ refreshMapView(); /* 设定事件的Listener */ mLocationManager.requestLocationUpdates(mLocationPrivider, 2000, 10, mLocationListener); } else { new AlertDialog.Builder(mymap.this).setTitle("系统讯息") .setMessage(getResources().getString(R.string.str_message)) .setNegativeButton("确定",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mymap.this.finish(); } }) .show(); } /* 开始记录的Button */ mButton01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { gp1=gp2; /* 清除Overlay */ resetOverlay(); /* 画起点 */ setStartPoint(); /* 更新MapView */ refreshMapView(); /* 重设移动距离为0,并更新TextView */ distance=0; mTextView.setText("移动距离:0M"); /* 启动画路线的机制 */ _run=true; } }); /* 结束记录的Button */ mButton02.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { /* 画终点 */ setEndPoint(); /* 更新MapView */ refreshMapView(); /* 终止画路线的机制 */ _run=false; } }); /* 缩小地图的Button */ mButton04.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { zoomLevel--; if(zoomLevel<1) { zoomLevel = 1; } mMapController.setZoom(zoomLevel); } }); /* 放大地图的Button */ mButton03.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { zoomLevel++; if(zoomLevel>mMapView.getMaxZoomLevel()) { zoomLevel = mMapView.getMaxZoomLevel(); } mMapController.setZoom(zoomLevel); } }); mButton05.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) {     Intent i = new Intent(mymap.this, sample1.class); startActivity(i); } }); } /* MapView的Listener */ public final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { /* 如果记录进行中,就画路线并更新移动距离 */ if(_run) { /* 记下移动后的位置 */ gp2=getGeoByLocation(location); /* 画路线 */ setRoute(); /* 更新MapView */ refreshMapView(); /* 取得移动距离 */ distance+=GetDistance(gp1,gp2); mTextView.setText("移动距离:"+format(distance)+"M");   gp1=gp2; } } @Override public void onProviderDisabled(String provider) {   Toast.makeText(
        getBaseContext(),
        "ProviderDisabled.",
        Toast.LENGTH_SHORT).show();     } @Override public void onProviderEnabled(String provider) {   Toast.makeText(
        getBaseContext(),
        "ProviderEnabled,provider:"+provider,
        Toast.LENGTH_SHORT).show();     } @Override public void onStatusChanged(String provider,int status, Bundle extras) { } };   /* 取得GeoPoint的method */ private GeoPoint getGeoByLocation(Location location) { GeoPoint gp = null; try { if (location != null) { double geoLatitude = location.getLatitude()*1E6; double geoLongitude = location.getLongitude()*1E6; gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); } } catch(Exception e) { e.printStackTrace(); } return gp; } /* 取得LocationProvider */ public void getLocationPrivider() {    Criteria mCriteria01 = new Criteria(); mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); mCriteria01.setAltitudeRequired(false); mCriteria01.setBearingRequired(false); mCriteria01.setCostAllowed(true); mCriteria01.setPowerRequirement(Criteria.POWER_LOW); mLocationPrivider = mLocationManager .getBestProvider(mCriteria01, true); mLocation = mLocationManager .getLastKnownLocation(mLocationPrivider);   } /* 设定起点的method */ private void setStartPoint() { int mode=1; drawmap mOverlay = new drawmap(gp1,gp2,mode); List<Overlay> overlays = mMapView.getOverlays(); overlays.add(mOverlay); } /* 设定路线的method */ private void setRoute() { int mode=2; drawmap mOverlay = new drawmap(gp1,gp2,mode); List<Overlay> overlays = mMapView.getOverlays(); overlays.add(mOverlay); } /* 设定终点的method */ private void setEndPoint() { int mode=3; drawmap mOverlay = new drawmap(gp1,gp2,mode); List<Overlay> overlays = mMapView.getOverlays(); overlays.add(mOverlay); } /* 重设Overlay的method */ private void resetOverlay() { List<Overlay> overlays = mMapView.getOverlays(); overlays.clear(); } /* 更新MapView的method */ public void refreshMapView() { mMapView.displayZoomControls(true); MapController myMC = mMapView.getController(); myMC.animateTo(gp2); myMC.setZoom(zoomLevel); mMapView.setSatellite(true); } /* 取得两点间的距离的method */ public double GetDistance(GeoPoint gp1,GeoPoint gp2) { double Lat1r = ConvertDegreeToRadians(gp1.getLatitudeE6()/1E6); double Lat2r = ConvertDegreeToRadians(gp2.getLatitudeE6()/1E6); double Long1r= ConvertDegreeToRadians(gp1.getLongitudeE6()/1E6); double Long2r= ConvertDegreeToRadians(gp2.getLongitudeE6()/1E6); /* 地球半径(KM) */ double R = 6371; double d = Math.acos(Math.sin(Lat1r)*Math.sin(Lat2r)+ Math.cos(Lat1r)*Math.cos(Lat2r)* Math.cos(Long2r-Long1r))*R; return d*1000; }
  private double ConvertDegreeToRadians(double degrees) { return (Math.PI/180)*degrees; } /* format移动距离的method */ public String format(double num) { NumberFormat formatter = new DecimalFormat("###"); String s=formatter.format(num); return s; } @Override protected boolean isRouteDisplayed() { return false; }
} 

作者: orzreynold   发布时间: 2010-07-19

1. 先确定 location provider is GPS
2. 将底下这行

mLocationManager.requestLocationUpdates(mLocationPrivider,
2000, 10, mLocationListener);

改成如下再报告你的实验结果

作者: samlu_128   发布时间: 2010-07-19

刚刚拿了手机出去测试

还是不会自动开启耶...

必须得靠别的软体定位完才可使用

还有请问刚刚改的那段数字的意思是??

以及什么意思是将provider为GPS?

如果有用别的软体定位才开

的确是会显是provider:GPS enable

但是还是得靠别的软体就是了

无法自己设定

作者: orzreynold   发布时间: 2010-07-19

requestLocationUpdates查一下方法就会有解释了,两个数字应该是多久更新一次和定位范围(这个有点忘了QQ")
靠别的软体才开的意思是? 怎靠? 啥软体?

作者: a312126   发布时间: 2010-07-22

开MyTracks就可以开启GPS定位

定位完后

在开此程式

就会自动开启GPS,并正常运作了

作者: orzreynold   发布时间: 2010-07-24

意思是没利用my track跑此程式
一开始的画面只是单纯的显示google map,但中心点不是在使用者的位置
只是一开始抓不到点吗?

作者: a312126   发布时间: 2010-07-28

那要加甚么程式码
在开启程式才可以正常使用
总不是每次想要用这支程式的时候
都要先开其他程式定位吧0.0

作者: a3230230   发布时间: 2011-08-31

热门下载

更多