【问题标题】:Geofencing : GoogleApiClient is not connected yet地理围栏:GoogleApiClient 尚未连接
【发布时间】:2016-11-09 04:54:33
【问题描述】:

我正在尝试在我的应用程序中实现地理围栏,因此每当用户输入其中一个地理围栏对象时,就会发生一些事情。当我添加这个 sn-p 时,遵循 android 开发人员指南(让您的应用程序位置感知):

LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            getGeofencePendingIntent()
    ).setResultCallback(this);

我在加载地理围栏后将其放入我的 onMapReady() 中,以便它们立即添加到我的应用程序中。但是,我收到此错误:

java.lang.IllegalStateException: GoogleApiClient is not connected yet.

我确定我已经在 onCreate() 中构建了 google api 客户端,但我仍然收到此错误。如果我删除上面的 sn-p(第一个代码 sn-p),则错误不再存在。我做错了什么?

代码:

公共类 MapsActivity 扩展 AppCompatActivity 实现 OnMapReadyCallback、ConnectionCallbacks、OnConnectionFailedListener、LocationListener、ResultCallback {

//Google Maps
public GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest = new LocationRequest();
public Location mCurrentLocation;
private static final int FINE_LOCATION_PERMISSION_REQUEST = 1;
private static final int CONNECTION_RESOLUTION_REQUEST = 2;
List<Marker> markerList = new ArrayList<Marker>();

//Temporary Vars
double lat = 0;
double lon = 0;
Marker current_marker;

//Request Info vars
static final int GET_DETAILS = 1;
static final int EDIT_DETAILS = 2;


//Debug
private static final String TAG = "GeekysMessage";

SharedPreferences sharedPreferences;
int markerCount;

private Toolbar toolbar;


//Geofence
private boolean mGeofencesAdded;
ArrayList mGeofenceList = new ArrayList<Geofence>();
private PendingIntent mGeofencePendingIntent;
public int geofenceCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);


    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    buildGoogleAPIClient();
    //Geofence
    // Empty list for storing geofences.
    mGeofenceList = new ArrayList<Geofence>();

    // Initially set the PendingIntent used in addGeofences() and removeGeofences() to null.
    mGeofencePendingIntent = null;
}

@Override
protected void onResume() {
    super.onResume();

    buildGoogleAPIClient();

    if (mGoogleApiClient.isConnected()) {
        startLocationUpdates();
    }
}

//LOCATION
private void buildGoogleAPIClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        createLocationRequest();
    }
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}



protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        if (mCurrentLocation == null) {
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            updateUI();
        }
        startLocationUpdates();

    }

}

@TargetApi(Build.VERSION_CODES.N)
@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    updateUI();
    Toast.makeText(this, "Updated",
            Toast.LENGTH_SHORT).show();
}

private void updateUI() {

}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(this, "Connection suspended", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(this, CONNECTION_RESOLUTION_REQUEST);
        } catch (IntentSender.SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            mGoogleApiClient.connect();
        }
    } else {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 1);
        dialog.show();
    }
}

protected void stopLocationUpdates() {
    // It is a good practice to remove location requests when the activity is in a paused or
    // stopped state. Doing so helps battery performance and is especially
    // recommended in applications that request frequent location updates.

    // The final argument to {@code requestLocationUpdates()} is a LocationListener
    // (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

@Override
protected void onPause() {
    super.onPause();
    // Stop location updates to save battery, but don't disconnect the GoogleApiClient object.
    if (mGoogleApiClient.isConnected()) {
        stopLocationUpdates();
    }
}


int requestCode = 0;

//MAP
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //load all saved markers
    loadMarkers();

    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            getGeofencePendingIntent()
    ).setResultCallback(this);

    //Permissions
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                FINE_LOCATION_PERMISSION_REQUEST);
    } else {
        mMap.setMyLocationEnabled(true);

    }


}
private void loadMarkers() {

    // Opening the sharedPreferences object
    sharedPreferences = getSharedPreferences("location", 0);

    // Getting number of locations already stored
    markerCount = sharedPreferences.getInt("markerCount", 0);

    //if marker are already saved
    if (markerCount != 0) {

        String lat = "";
        String lng = "";
        String marker_title = null;
        String marker_snippet = null;
        int marker_radius = 0;

        for (int i=0; i < markerCount; i++) {
            lat = sharedPreferences.getString("lat"+i, "0");
            lng = sharedPreferences.getString("lng"+i, "0");
            marker_title = sharedPreferences.getString("title"+i, "");
            marker_snippet = sharedPreferences.getString("snippet"+i, "");
            marker_radius = sharedPreferences.getInt("radius"+i, 20);

            double lati = Double.valueOf(lat);
            double lngi = Double.valueOf(lng);

            addMarker(lati, lngi, marker_title, marker_snippet);
            addGeofence(marker_title, lati, lngi, marker_radius);

        }

    }
}
private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
    // calling addGeofences() and removeGeofences().
    return PendingIntent.getService(this, 0, intent, PendingIntent.
            FLAG_UPDATE_CURRENT);
}

public void addMarker(double lati, double longi, String title, String snippet){
    if (snippet==""){snippet = null;}
     Marker m = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(lati, longi))
            .title(title)
             .snippet(snippet)
            .draggable(true));
    markerList.add(m);
}

public void saveMarkers(){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    markerCount = 0;
    for (Marker i : markerList) {
        //Save to sharedprefences
        markerCount++;

        editor.putString("lat" + Integer.toString((markerCount - 1)), String.valueOf(i.getPosition().latitude));
        editor.putString("lng" + Integer.toString((markerCount - 1)), String.valueOf(i.getPosition().longitude));
        editor.putString("title" + Integer.toString((markerCount - 1)),i.getTitle());
        editor.putString("snippet" + Integer.toString((markerCount - 1)),i.getSnippet());
        editor.putInt("markerCount", markerCount);

        editor.commit();
    }
}

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}

public void addGeofence(String key, double lat, double lng, int radius ){
    mGeofenceList.add(new Geofence.Builder()
            // Set the request ID of the geofence. This is a string to identify this
            // geofence.
            .setRequestId(key)

            .setCircularRegion(
                    lat,
                    lng,
                    radius
            )
            .setExpirationDuration(12*60*60*1000)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                    Geofence.GEOFENCE_TRANSITION_EXIT)
            .build());

    Circle circle = mMap.addCircle(new CircleOptions()
            .center(new LatLng(lat, lng))
            .radius(radius)
            .fillColor(Color.parseColor("#02bbff")));



}

@Override
public void onResult(@NonNull Status status) {
    if (status.isSuccess()) {
        // Update state and save in shared preferences.
        mGeofencesAdded = !mGeofencesAdded;
        Toast.makeText(
                this,
                getString(mGeofencesAdded ? R.string.geofences_added :
                        R.string.geofences_removed),
                Toast.LENGTH_SHORT
        ).show();
    } else {
        // Get the status code for the error and log it using a user-friendly message.
        String errorMessage = GeofenceErrorMessages.getErrorString(this,
                status.getStatusCode());
        Log.e(TAG, errorMessage);
    }
}
}

任何帮助将不胜感激。

【问题讨论】:

    标签: java android google-maps android-studio


    【解决方案1】:

    您在onStop() 中呼叫mGoogleApiClient.disconnect();LocationServices.GeofencingApi.addGeofences( 代码必须触发 onStop ,因此 mGoogleApiClient 正在断开连接。

    在创建时

    Boolean fromOnMapReady = false;
    

    在 OnMapReady 中

    @Override
    public void onMapReady(GoogleMap googleMap) {
         fromOnMapReady = true;
    

    在 OnStop 中

    protected void onStop() {
      if(!fromOnMapReady){
        mGoogleApiClient.disconnect();
      }
      super.onStop();
    }
    

    一旦任何进程完成运行 reset fromOnMapReady = false 否则 mGoogleApiClient 将不会在需要时断开连接

    【讨论】:

    • 没有用...我什至删除了那个事件,但仍然无法连接到 google api...还有其他建议吗?
    • 连接一次吗?
    • 别担心,我已经修好了,不过我还是给你功劳吧 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    相关资源
    最近更新 更多