Android requestLocationUpdates without minDistance

发布于 2024-10-30 22:41:58 字数 2662 浏览 1 评论 0原文

我需要每 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 位置发送信息的任何想法控制将不胜感激

问候, 米琳达

I have a need to get the gps location every 20 seconds or so, but ignoring the need to have the user move a certain number of meters, I tried setting the minDistance to zero but still no luck, its only when I manually send the location using the location control from DDMS that this gets activated and the location gets updated.

Note - This is run and tested from the emulator and not a real device, for now at least

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);
    }
}

Here is the related code fragment, any ideas on how to trigger the listener without having the user move or me having to send information from the DDMS Location Controls woild be greatly appreciated

Regards,
Milinda

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-11-06 22:41:58

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

requestLocationUpdates takes milliseconds, not seconds.
You should change:

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

to:

private final static int INTERVAL_TIME_SECONDS = 20 * 1000; // 20 seconds
private final static float INTERVAL_DISTANCE_METERS = 0f; // 0 meters
極樂鬼 2024-11-06 22:41:58

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

As far as testing on the emulator goes. I think your limited to manually pushing GPS locations or creating a few automated simulators:
Testing GPS in Android

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文