3

我想从同一个监听器和实现中同时收听 GPS 和 NETWORK 位置提供程序

这样做可以吗:

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,metersToUpdate,this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,metersToUpdate,this);

它会为两个提供者使用相同的方法吗?

4

3 回答 3

1

谷歌在这里说:

您还可以通过调用 requestLocationUpdates() 两次从 GPS 和网络位置提供程序请求位置更新——一次用于 NETWORK_PROVIDER,一次用于 GPS_PROVIDER。

于 2012-08-31T15:20:40.260 回答
1

就像 1.2.3 一样简单,看看我的例子......

try {
            Criteria criteria = new Criteria();
            mLocationManagerHelper.SetLocationManager((LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE));
            mLocationManagerHelper.GetLocationManager().requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, mLocationManagerHelper.GetLocationListener());

            String provider = mLocationManagerHelper.GetLocationManager().getBestProvider(criteria, false);
            Location location = mLocationManagerHelper.GetLocationManager().getLastKnownLocation(provider);

            if (location != null) {
                mLongitude = location.getLongitude();
                mLatitude = location.getLatitude();
            }
        } catch (Exception ex) {
            Log.e(TAG, "GPS", ex);
        }

定位助手

public class LocationManagerHelper {
private static final String TAG = LocationManagerHelper.class.getSimpleName();
private Context mContext;

private LocationManager mLocationManager;
private GeoUpdateHandler mLocationListener = new GeoUpdateHandler();

public LocationManagerHelper(Context context) {
    this.mContext = context;
}


public GeoUpdateHandler GetLocationListener() {
    return mLocationListener;
}

public void SetLocationManager(LocationManager locationManager) {
    mLocationManager = locationManager;
}

public LocationManager GetLocationManager() {
    return mLocationManager;
}


public void Stop() {
    if (mLocationManager != null) {
        mLocationManager.removeUpdates(mLocationListener);
    }
}

private class GeoUpdateHandler implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        String longitude = "Longitude: " + loc.getLongitude();
        Log.v(TAG, longitude);
        String latitude = "Latitude: " + loc.getLatitude();
        Log.v(TAG, latitude);
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {
    }

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

}

于 2012-08-31T15:23:48.703 回答