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