-2

GPS/ネットワーク プロバイダーの位置情報に行き詰まっています。すべてのアンドロイドユーザーにとって基本的な質問であることは知っていますが、私は長い間この問題を抱えています。

実際には、 Option Menuをクリックして次の方法で現在の場所を取得し、データベースに挿入しています。

/****
 * GPS Process First of all call listener of Location then checking for GPS_PROVIDER if not
 * available then check for NETWORK_PROVIDER and if its also not available then pass
 * 0.00,0.00 to longitude and latitude
 */
/** PROCESS for Get Longitude and Latitude **/
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// Define a listener that responds to location updates
locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        longitude = String.valueOf(location.getLongitude());
        latitude = String.valueOf(location.getLatitude());
        Log.d(TAG, "changed Loc : " + longitude + ":" + latitude);
    }

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

    public void onProviderEnabled(String provider) {
    }

    public void onProviderDisabled(String provider) {
    }
};

// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// check if GPS enabled
if (isGPSEnabled) {
    int i;
    for (i = 0; i < 3; i++) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                locationListener);

        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {
            longitude = String.valueOf(location.getLongitude());
            latitude = String.valueOf(location.getLatitude());
            break;
        } else {
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if (location != null) {
                longitude = String.valueOf(location.getLongitude());
                latitude = String.valueOf(location.getLatitude());
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                        0, locationListener);
                break;
            } else {
                longitude = "0.00";
                latitude = "0.00";
            }
        }
    }

    /**
     * Record Inserting Process in Database...
     **/

} else {
    AlertDialogManager.showAlertforSettings(AccountMenuActivity.this, "GPS Settings",
            "GPS is not Enabled. Do you want to Enable?", false);
}

緯度と経度が何度も 0.00になり、他の国 (中国、南アフリカなど) の場所が表示されることもあります。

なぜそれが起こるのかわかりませんが、Stackoverflow Solutionを見つけた後、2日間のグーグル検索でFusedLocationProviderApiという名前のGoogleの新しいAPIが見つかりました。

いくつかのリンクをたどって実装しましたが、次のようなエラーが発生しました。

/**
 * Booleans for GPS and NETWORK is Enabled or not...
 */
boolean gpsEnabled = false;
boolean networkEnabled = false;

//exceptions will be thrown if provider is not permitted.
try {
    gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
    Log.e("TAG", "GPS Error : " + ex.getLocalizedMessage());
}

try {
    networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
    Log.e("TAG", "NetWork Error : " + ex.getLocalizedMessage());
}

Log.e("TAG", "GPS Network status : " + gpsEnabled + " : " + networkEnabled);

//don't start listeners if no provider is enabled
if (!gpsEnabled && !networkEnabled) {
    AlertDialogManager.showAlertforSettings(VisitAcitivity.this, "GPS Settings", "GPS is not Enabled. Do you want to Enable?", false);
} else {
    Log.i(TAG, "isPlayServiceAvailable" + isPlayServiceAvailable);
    if(isPlayServiceAvailable) {

        createLocationRequest();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();

    } else {

        final MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {

            @Override
            public void gotLocation(final Location location) {
                // Got the location!
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        longitude = String.valueOf(location.getLongitude());
                        latitude = String.valueOf(location.getLatitude());

                        saveVisit();

                    }

                });
            }
        };
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(VisitAcitivity.this, locationResult);                
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if(isPlayServiceAvailable && mGoogleApiClient != null) 
        stopLocationUpdates();
    else if (locationManager != null) {
        locationManager.removeUpdates(locationListener);
        Log.d("msg", "Location Listener ended...");
    }
}

@Override
public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop fired ..............");
    if(mGoogleApiClient != null)
        mGoogleApiClient.disconnect();

}

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
    startLocationUpdates();
}

protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    Log.d(TAG, "Location update started ..............: ");
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "Firing onLocationChanged..............................................");
    mCurrentLocation = location;
    Log.d(TAG, "UI update initiated .............");
    if (null != mCurrentLocation) {
        latitude = String.valueOf(mCurrentLocation.getLatitude());
        longitude = String.valueOf(mCurrentLocation.getLongitude());
    } else {
        Log.d(TAG, "location is null ...............");
    }
}

protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    Log.d(TAG, "Location update stopped .......................");
}

その中で、私は場所を取得する2つの方法を管理しています:

  1. Google Play Service が利用可能かどうかは、FusedLocation API を使用して場所を取得します。
  2. それ以外の場合は、独自の方法を使用して位置を取得します (デバイスが非常に多いため)。

My で次の権限を取得しましたAndroidManifest.xml

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

次に、 NULLの場所も取得します:(この問題の解決策はありますか?

よろしくお願いします。

4

1 に答える 1

0

代わりにこれを試してください: https://github.com/mcharmas/Android-ReactiveLocation

融合された場所を取得するための 8 行のコード:

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
locationProvider.getLastKnownLocation()
    .subscribe(new Action1<Location>() {
        @Override
        public void call(Location location) {
            doSthImportantWithObtainedLocation(location);
        }
    });
于 2015-08-10T12:07:15.090 に答える