Yo dejo el código más apto que eh podido formar para casos en los que tengo que manejar mi ubicación, esto logro con la GoogleApiClient
, para y el GPS para manejar la posición más exacta del usuario, si bien pusiera mejorarse, es funcional para mi y me ah ayudado mucho.
Bien primero que nada en tu build.gradle(Module:app):
dependencies {
...
//MAPS
implementation 'com.google.android.gms:play-services-maps:16.0.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
}
En tu build.gradle(Project:proyect):
buildscript {
repositories {
...
google()
}
dependencies {
...
classpath 'com.google.gms:google-services:4.1.0'
}
}
allprojects {
repositories {
google()
...
}
}
en tu activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
en tu Archivo MapsActivity.java
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
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.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;
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.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.Objects;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final int REQUEST_CHECK_SETTINGS = 0x1;
private static GoogleApiClient mGoogleApiClient;
private static final int ACCESS_FINE_LOCATION_INTENT_ID = 3;
private static final String BROADCAST_ACTION = "android.location.PROVIDERS_CHANGED";
private SupportMapFragment mapFragment;
private GoogleMap mGoogleMap;
private LocationRequest mLocationRequest;
private Location location;
private Marker mCurrLocationMarker, markerClient;
private BitmapDescriptor iconUser, iconClient;
private LatLng latLng, latLngOtro;
private boolean stateMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
initGoogleAPIClient(); //Init Google API Client
checkPermissions(); //Check Permission
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//ESTOS SON LOS ICONOS PERSONALIZABLES (SUSTITUYELOS)
iconUser = BitmapDescriptorFactory.fromResource(R.drawable.locationpointer);
iconClient = BitmapDescriptorFactory.fromResource(R.drawable.locationpin);
stateMap = false;
//Este handler será ejecutado 5 segundos después, lo uso ya que me ha pasado que aún dandole los permisos de gps por primera vez no los reconoce y no detecta nuestra ubicación, pero los puedes omitir .
new Handler().postDelayed(new Runnable() {
public void run() {
if (!stateMap) {
initGoogleAPIClient(); //Init Google API Client
checkPermissions(); //Check Permission
}
}
;
}, 10000); //timer
}
//////////////////////////////////////////////////////////////GPS/////////////////////////////////////////////////////////////////
private void initGoogleAPIClient() {
mGoogleApiClient = new GoogleApiClient.Builder(MapsActivity.this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(MapsActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
requestLocationPermission();
else
showSettingDialog();
} else
showSettingDialog();
}
private void requestLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LOCATION_INTENT_ID);
} else {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LOCATION_INTENT_ID);
}
}
private void showSettingDialog() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.e("TAG", "SUCCESS");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.e("TAG", "RESOLUTION_REQUIRED");
try {
status.startResolutionForResult(MapsActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.e("TAG", "GPS NO DISPONIBLE");
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case RESULT_OK:
Log.e("Settings", "Result OK");
break;
case RESULT_CANCELED:
Log.e("Settings", "Result Cancel, La aplicación se cerrará");
finish();
break;
}
break;
}
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(gpsLocationReceiver, new IntentFilter(BROADCAST_ACTION));//Registrar el receptor de difusión para comprobar el estado del GPS.
}
@Override
protected void onDestroy() {
super.onDestroy();
if (gpsLocationReceiver != null)
unregisterReceiver(gpsLocationReceiver);
}
//Ejecutar en la interfaz de usuario
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
showSettingDialog();
}
};
/* Receptor de difusión para comprobar el estado del GPS */
private BroadcastReceiver gpsLocationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Si la acción es la ubicación
if (intent.getAction().matches(BROADCAST_ACTION)) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//Compruebe si el GPS está encendido o apagado
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.e("About GPS", "GPS is Enabled in your device");
} else {
//Si el GPS está apagado, muestre el diálogo de ubicación
new Handler().postDelayed(sendUpdatesToUI, 10);
Log.e("About GPS", "GPS is Disabled in your device");
finish();
}
}
}
};
/* Método de permiso On Request para verificar si el permiso se ha otorgado o no a Marshmallow Devices */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.e("TAG", "onRequestPermissionsResult");
switch (requestCode) {
case ACCESS_FINE_LOCATION_INTENT_ID: {
// Si se cancela la solicitud, las matrices de resultados están vacías.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Si el permiso otorgado muestra el cuadro de diálogo de ubicación si APIClient no es nulo
if (mGoogleApiClient == null) {
initGoogleAPIClient();
showSettingDialog();
} else
showSettingDialog();
} else {
Toast.makeText(MapsActivity.this, "Location Permission denied.", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.e("TAG", "onConnected");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(9000);
mLocationRequest.setFastestInterval(9000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
try {
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(this), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int i) {
Log.e("TAG", "onConnectionSuspended");
mGoogleApiClient.connect(i);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("TAG", "onConnectionSuspended");
}
@Override
public void onLocationChanged(Location location) {
Log.e("TAG", "onLocationChanged");
this.location = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
markerClient.remove();
}
stateMap=true;//en el caso de que se ejecute el Handler y entre a onLocationChanged va volver verdadero stateMap y no volvera a pedir permisos de GPS
///////////////////////////////////////ESTE ES EL MARCADOR DE TU UBICACIÓN ACTUAL///////////////////////////////////////
latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Posición Actual");
markerOptions.icon(iconUser);
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));//AQUI MODIFICA EL ZOOM AL MAPA SEGUN TUS NECESIDADES
///////////////////////////////////////ESTE ES EL MARCADOR DEL DESTINO (ES UN MARCADOR CON UNA POCISION ESTATICA) ///////////////////////////////////////
latLngOtro = new LatLng(19.432602, -99.133248);
MarkerOptions markerOptions2 = new MarkerOptions();
markerOptions2.position(latLngOtro);
markerOptions2.title("ESTE ES TU DESTINO");
markerOptions2.icon(iconClient);
markerClient = mGoogleMap.addMarker(markerOptions2);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onMapReady(GoogleMap googleMap) {
Log.e("TAG", "onMapReady");
mGoogleMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mGoogleMap.setMyLocationEnabled(true);
buildGoogleApiClient();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
} else {
Toast.makeText(this, "No cuentas con los permisos necesarios, cierra y abre de nuevo la aplicación", Toast.LENGTH_SHORT).show();
}
} else {
buildGoogleApiClient();
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
protected synchronized void buildGoogleApiClient() {
Log.e("TAG", "buildGoogleApiClient");
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.reconnect();
} else {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
}
}
Lo que intento con esto es:
- Pedir los permisos de ubicación.
- Encender el GPS desde la aplicación
(como en Google Maps).
- Actualizar mi ubicación constantemente.
En conclusión esto se verá de la siguiente manera:
Nota:Si tienes problema al visualizar los marcadores la primera vez que ejecutas la app directamente de Android Studio, cierra y abre de nuevo la aplicación, me paso un par de ocasiones y desconozco el motivo, solo tomalo en cuenta y espero mi ejemplo pueda ayudar a alguien que necesite algo como lo que yo en su momento.
iconos de marcadores from Icons made by Google from www.flaticon.com is licensed by CC 3.0 BY