0

我正在编写代码以便能够使用本地化(如果可用,则基于 GPS,否则基于网络)。如果我在手机上打开 GPS 进入我的活动,一切正常。但是,如果我在关闭 GPS 的情况下进入活动,然后在活动中启用 GPS,则没有任何反应,并且基于网络的位置仍然处于活动状态,而不是切换到基于 GPS。

我错过了什么 ?谢谢

public class MapActivity extends Activity implements LocationListener {

    //Used CameraPosition attributes
    private final int MAP_BEARING = 69;
    private final int SETUP_ZOOM = 16;
    private final int AT_POSITION_ZOOM = 19;
    private final int UPDATE_FREQUENCY = 20000; //Update frequency (ms)
    private final LatLng MAP_CENTER = new LatLng(44.80736, -0.596572);

    private CheckBox mServices, mRestauration, mBuildings;
    private Resources mResources;
    private GoogleMap mMap;
    private LocationManager mLocationManager;
    private Marker mCurrentLocation;
    private ArrayList<Marker> mServicesMarkers, mRestaurationMarkers, mBuildingsMarkers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mResources = getResources();
        mServices = (CheckBox) findViewById(R.id.services_check);
        mRestauration = (CheckBox) findViewById(R.id.restauration_check);
        mBuildings = (CheckBox) findViewById(R.id.buildings_check); 
        setUpLocationServices();
    }

    public void setUpLocationServices() {
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = mLocationManager.getBestProvider(criteria, true);
        Location location = mLocationManager.getLastKnownLocation(provider);
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng currentPosition = new LatLng(latitude, longitude);

        mCurrentLocation = mMap.addMarker(new MarkerOptions()
        .position(currentPosition)
        .title("Votre position")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.user_location_marker)));   

        if (location != null)
            onLocationChanged(location);
        if (isGpsEnabled())
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        else
            mLocationManager.requestLocationUpdates(provider, UPDATE_FREQUENCY, 0, this);

    }

    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation.remove();
        LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude());
        mCurrentLocation = mMap.addMarker(new MarkerOptions()
        .position(currentPosition)
        .title("Votre position")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.user_location_marker)));      
    }

    public boolean isGpsEnabled() {
        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        return service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    @Override
    public void onProviderDisabled(String provider) {
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (isGpsEnabled()) 
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,  this);
        else 
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_FREQUENCY, 0, this);               
    }

    @Override
    public void onProviderEnabled(String provider) {
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (isGpsEnabled())
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        else 
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_FREQUENCY, 0, this);           
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub
    }
}
4

1 回答 1

0

问题似乎在这里:

  if (isGpsEnabled())
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
  else
        mLocationManager.requestLocationUpdates(provider, UPDATE_FREQUENCY, 0, this);

您只运行一次此代码,在onCreate(). 如果您希望在启用 GPS 后更新位置,则需要提出要求。在现代设备上,我更喜欢将Google 的 Play Service Location API与 Fused Location 提供程序一起使用。这就是我在上一个商业应用程序中实现的方式,只有在未安装 Google Play 时才使用 GPS。

另一种选择,如果您查看此链接,在该isBetterLocation方法中,您可以将当前值与新值进行比较,然后选择两者中较好的值。这样,如果您有一个并得到另一个,您可以决定它是否比以前更好。

于 2013-12-13T18:54:43.223 回答