【发布时间】:2011-07-30 23:11:46
【问题描述】:
我需要每 20 秒左右获取一次 gps 位置,但忽略了让用户移动一定数量的米的需要,我尝试将 minDistance 设置为零但仍然没有运气,只有当我手动时使用来自 DDMS 的位置控制发送位置,这会被激活并更新位置。
注意 - 这是从模拟器而不是真实设备上运行和测试的,至少目前是
private final static Long INTERVAL_TIME = new Long(20);
private final static Float METERS_MOVED_VALUE = 0f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.journey);
Bundle dataBundle = this.getIntent().getExtras();
try {
driverData = new JSONObject(dataBundle.getString("driverData"));
} catch (JSONException e) {
e.printStackTrace();
}
//Set the UI elements into variables
journeyDescription = (TextView)findViewById(R.id.journeyDescription);
journeyText = (TextView)findViewById(R.id.journeyText);
confirmButton = (Button)findViewById(R.id.btnCheck);
//Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
try{
//Until requests are found keep broadcasting the drivers location and checking for requests
if(!foundRequest){
updateDriverLocationAndCheckForNewDriverRequests(location);
}
//Always keep updating the drivers location
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
}catch(JSONException e){
Toast.makeText(JourneyActivity.this, "An unexpected error occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//Register the listener with the Location Manager to receive location updates can be either gps provider
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, INTERVAL_TIME, METERS_MOVED_VALUE, locationListener);
}
else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, INTERVAL_TIME, METERS_MOVED_VALUE, locationListener);
}
}
这是相关的代码片段,任何关于如何在不让用户移动或我不得不从 DDMS 位置控制发送信息的情况下触发侦听器的想法将不胜感激
问候, 米琳达
【问题讨论】:
-
我建议创建一个自己的线程或服务,每 20 秒请求一次最后一次修复
-
我是android新手,如果有的话请原谅我的无知,但是即使使用线程,LocationListener是否仍然需要满足一定的最小时间间隔和一定的最小距离?
-
你在外面吗?全球定位系统仅适用于天空晴朗的设备。网络提供的坐标仅我认为到 100 m。
-
这实际上是在模拟器上运行的,我还没有拿到一个设备来测试。如果用户的手机不支持 GPS,网络坐标只是一个备份,但是原型将具有 GPS,因此也不会成为问题
标签: java android gps location locationmanager