Android:requestLocationUpdates

时间:2014-04-18 17:32:27

标签: android

我设法通过以下方式修复了Android设备的位置(包括网络提供商和gps提供商): locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

但我想在同一时刻使用NETWORK_PROVIDER和GPS_PROVIDER计算手机位置,以便我可以将每个准确度进行比较。

有没有人知道如何使用NETWORK_PROVIDER和GPS_PROVIDER精确定位设备?

1 个答案:

答案 0 :(得分:0)

使用2个位置监听器

public class MainActivity extends Activity {
    private Location networkLocation = null;
    private Location gpsLocation = null;

    private class NetworkLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            // if you only want one location
            // if (networkLocation == null)
            networkLocation = location;
            if (gpsLocation != null) {
                // do something
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}
    }

    private class GpsLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            // if you only want one location
            // if (gpsLocation == null)
            gpsLocation = location;
            if (networkLocation != null) {
                // do something
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}
    }

}