GeoFire不记录Firebase数据库Android上的位置

时间:2018-08-01 13:44:50

标签: android firebase firebase-realtime-database firebase-authentication

我正在尝试在我的应用程序上创建一个函数,该函数应表示用户登录时已识别其位置并将其上载到我创建的Firebase数据库中。这很重要,因为其他用户将需要此信息。

我一直在跟随一个教程来帮助我实现这一目标,但是我正在使用的代码示例似乎并未做到这一点。我在这里错过了一个把戏吗?

    public void onLocationChanged(Location location) {
    mLastLocation = location;

    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));


    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("respondersAvailable");

    GeoFire geoFire = new GeoFire(ref);
    geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
}


@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(ResponderMapActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
    }
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations, this can be null.
                    if (location != null) {
                        // Logic to handle location object
                    }
                }
            });
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

//onRequestPermissionResult added to get Google Maps API working
final int LOCATION_REQUEST_CODE = 1;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case LOCATION_REQUEST_CODE: {
            if (mapFragment != null) {

                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mapFragment.getMapAsync(this);
                } else {
                    Toast.makeText(getApplicationContext(), "Please provide the permission", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }
}

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

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("driversAvailable");

    GeoFire geoFire = new GeoFire(ref);
    geoFire.removeLocation(userId);

    }



    }

这也是我的成绩文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.jamesboyle.cprnow"
    minSdkVersion 18
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.2'
implementation 'com.google.android.gms:play-services-location:15.0.1'
implementation 'com.firebase:geofire-android:2.1.1'
}

apply plugin: 'com.google.gms.google-services'

0 个答案:

没有答案