我也尝试了其他一些相同的问题,但是我不知道我哪里错了。在开启位置时,即使应用启动也不会调用更改。我也通过移动设备进行了尝试。这是我的代码-
public class DriverMapActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location location;
LocationRequest mLocatioRequest;
LocationCallback locationCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_map);
// 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);
this.locationCallback = (new LocationCallback() {
public void onLocationResult(@Nullable LocationResult locationResult) {
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this).
addOnConnectionFailedListener(this).
addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
Toast.makeText(this, "" + location.getLongitude(), Toast.LENGTH_SHORT).show();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Log.d("checkingLocId",userid+"Funtion called");
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("driversAvailable");
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userid, new GeoLocation(location.getLatitude(), location.getLongitude()));
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocatioRequest = new LocationRequest();
mLocatioRequest.setInterval(1000);
mLocatioRequest.setFastestInterval(1000);
mLocatioRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocatioRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onStop() {
super.onStop();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("driverAvailable");
GeoFire geoFire = new GeoFire(ref);
geoFire.removeLocation(userid);
}
}
答案 0 :(得分:0)
更改您的requestLocationUpdates方法调用。
替换
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocatioRequest, this);
使用
LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(locationRequest,
new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
// your code
}
},
null);