2

現在の場所を取得するために融合された場所を実装しようとしています。私はこのリンクの助けを借りてそれをやっています

googleAPICLient が接続されました。ただし、場所は常に null

私のマニフェストファイルにこれを入れました

   <meta-data android:name="com.google.android.gms.version"
        android:value="5089000" />

別の Fusionclass を作成しました。

私のフュージョンクラス--

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.XXX.constants.LogConstants;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class FusedLocation implements 
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {



private static final long INTERVAL = 1000 * 30;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    private static final long ONE_MIN = 1000 * 60;
    private static final long REFRESH_TIME = ONE_MIN * 5;
//  private static final float MINIMUM_ACCURACY = 50.0f;
    Activity LauncherActivity; 
    private LocationRequest locationRequest;
    private GoogleApiClient googleApiClient;
    private Location location;
    private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
public FusedLocation(Activity LauncherActivity) {
    locationRequest = LocationRequest.create();
//  locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(INTERVAL);
    locationRequest.setFastestInterval(FASTEST_INTERVAL);
    this.LauncherActivity = LauncherActivity;

    googleApiClient = new GoogleApiClient.Builder(LauncherActivity)
            .addApi(LocationServices.API).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();

    if (googleApiClient != null) {
         Log.i(LogConstants.INFO_LOG, "googleApiClient is not null");
  //the console prints this
        googleApiClient.connect();

    }
}

@Override
public void onConnected(Bundle connectionHint) {
     Log.i(LogConstants.INFO_LOG, "GoogleApiClient connected");
    Location currentLocation = fusedLocationProviderApi.getLastLocation(googleApiClient);
    if (currentLocation != null && currentLocation.getTime() > REFRESH_TIME) {
        location = currentLocation;
    } else {
          fusedLocationProviderApi.requestLocationUpdates(googleApiClient,locationRequest, locListen);
        // Schedule a Thread to unregister location listeners
        Executors.newScheduledThreadPool(1).schedule(new Runnable() {
            @Override
            public void run() {
                fusedLocationProviderApi.removeLocationUpdates(googleApiClient, locListen);
            }
        }, ONE_MIN, TimeUnit.MILLISECONDS);
    }
}




 com.google.android.gms.location.LocationListener locListen=new           com.google.android.gms.location.LocationListener() {

    @Override
    public void onLocationChanged(Location location) {

         Log.i(LogConstants.INFO_LOG, "75location::"+location);



    }
};

public Location getLocation() {
    return this.location;
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

私のアクティビティ クラスから FusedLOcation クラスを呼び出しています。oncreate

 //show error dialog if GoolglePlayServices not available
    if (!isGooglePlayServicesAvailable()) {
        finish();
    }
    fusedLocation = new FusedLocation(this);
    Location location = fusedLocation.getLocation();
    String locationResult = "";
    if (null != location) {
        Log.i(LogConstants.INFO_LOG, location.toString());
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Log.i(LogConstants.INFO_LOG, "longitude::!"+longitude);
        float accuracy = location.getAccuracy();
        /*double elapsedTimeSecs = (double) location.getElapsedRealtimeNanos()
                / 1000000000.0;*/
        String provider = location.getProvider();
        double altitude = location.getAltitude();
        locationResult = "Latitude: " + latitude + "\n" +
                "Longitude: " + longitude + "\n" +
                "Altitude: " + altitude + "\n" +
                "Accuracy: " + accuracy + "\n" +
                "Elapsed Time: "  + " secs" + "\n" +
                "Provider: " + provider + "\n";
    } else {
        locationResult = "Location Not Available!";
        Log.i(LogConstants.INFO_LOG, "Location Not Available!");
    }

 private boolean isGooglePlayServicesAvailable() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        Log.i(LogConstants.INFO_LOG, "status:"+status);
        if (ConnectionResult.SUCCESS == status) {
            return true;
        } else {
            GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
            return false;
        }
    }

場所は常に null です。リスナーが機能していないようです。

4

2 に答える 2

2

位置情報サービスを確認してください。

オフになっている可能性があるので、オンにしてからテストしてください。

GPS が有効かどうかの条件を設定することもできます。

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

ありがとう。

于 2015-07-29T12:00:05.433 に答える