-1
這是我的代碼。它不僅帶來了一個地圖,而更新後的位置大部分的時間,但令人驚訝的一些罕見的時候,它確實帶來了更新的位置地圖谷歌地圖更新地址
public class CarteClient extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private GoogleMap mMap;
GoogleApiClient Gac;
Location Local;
LocationRequest demLocal;
private Button déco, demT;
private LatLng positionClient;
//Gac=Google Api Client
//demLocal=demande de localisation
//déco=déconnexion
//demT=demande Toleka
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carte_client);
// 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);
déco=(Button)findViewById(R.id.déconnexion);
demT=(Button)findViewById(R.id.demtoleka);
déco.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Intent i= new Intent(CarteClient.this,SecondActivity.class);
startActivity(i);
finish();
return;
}
});
demT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String IdUtil= FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference réfbd= FirebaseDatabase.getInstance().getReference("DemandeClient");
GeoFire geoFire= new GeoFire(réfbd);
geoFire.setLocation(IdUtil, new GeoLocation(Local.getLatitude(), Local.getLongitude()));
positionClient=new LatLng(Local.getLatitude(), Local.getLongitude());
mMap.addMarker(new MarkerOptions().position(positionClient).title("Client par Ici"));
demT.setText("Contact avec le chauffeur en cours...");
//réfbd=référence base de données
//IdUtil= Identité Utilisateur
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
buildGoogleApiClient();
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) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
}
protected synchronized void buildGoogleApiClient() {
Gac=new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
Gac.connect();
}
@Override
public void onLocationChanged(Location location) {
Local=location;
LatLng latlon= new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latlon));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
}
@Override
public void onConnected(@Nullable Bundle bundle) {
demLocal = new LocationRequest();
demLocal.setInterval(1000);
demLocal.setFastestInterval(1000);
demLocal.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_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) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(Gac, demLocal, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onStop(){
super.onStop();
}
}