Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.aminepc.testappgooglemaps;
- import android.Manifest;
- import android.content.pm.PackageManager;
- import android.icu.text.DateFormat;
- import android.location.Location;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.app.FragmentActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- 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.LocationListener;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- import java.util.Date;
- public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- LocationListener{
- TextView latitudeText;
- TextView longitudeText;
- private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi;
- private GoogleApiClient googleApiClient;
- private LocationRequest locationRequest;
- private Double myLatitude;
- private Double myLongitude;
- GoogleMap mMap;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Retrieve the content view that renders the map.
- setContentView(R.layout.activity_maps);
- // Get the SupportMapFragment and request notification
- // when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- latitudeText = (TextView) findViewById(R.id.tvLatitude);
- longitudeText = (TextView) findViewById(R.id.tvLongitude);
- googleApiClient = new GoogleApiClient.Builder(this)
- .addApi(LocationServices.API)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .build();
- locationRequest = new LocationRequest();
- locationRequest.setInterval(10 * 1000);
- locationRequest.setFastestInterval(15 * 1000);
- locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap= googleMap;
- // Add a marker in Sydney and move the camera
- }
- @Override
- public void onConnected(Bundle bundle) {
- requestLocationUpdates();
- }
- private void requestLocationUpdates() {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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(googleApiClient, locationRequest, this);
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- }
- @Override
- public void onLocationChanged(Location location) {
- myLatitude = location.getLatitude();
- myLongitude = location.getLongitude();
- latitudeText.setText("Latitude : " + String.valueOf(myLatitude));
- longitudeText.setText("Longitude : " + String.valueOf(myLongitude));
- String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
- LatLng sydney = new LatLng(myLatitude,myLongitude);
- mMap.addMarker(new MarkerOptions().position(sydney).title("Position du :"+currentDateTimeString));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
- }
- @Override
- protected void onStart() {
- super.onStart();
- googleApiClient.connect();
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (googleApiClient.isConnected()) {
- requestLocationUpdates();
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
- }
- @Override
- protected void onStop() {
- super.onStop();
- googleApiClient.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement