Android requestLocationUpdates没有minDistance

时间:2011-04-06 15:47:20

标签: java android gps location locationmanager

我需要每隔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位置控制器发送信息,我们将不胜感激

此致 弥兰陀

2 个答案:

答案 0 :(得分:1)

requestLocationUpdates需要毫秒,而不是秒。 你应该改变:

private final static Long INTERVAL_TIME = new Long(20);
private final static Float METERS_MOVED_VALUE = 0f;

为:

private final static int INTERVAL_TIME_SECONDS = 20 * 1000; // 20 seconds
private final static float INTERVAL_DISTANCE_METERS = 0f; // 0 meters

答案 1 :(得分:0)

就模拟器上的测试而言。我认为您仅限于手动推送GPS位置或创建一些自动模拟器: Testing GPS in Android