Position my location on google maps

I am trying to position my location on a google maps map (GPS is already enabled). For this I have the following code, which is executed in the oncreate:

    LocationManager objLocation=null;
    LocationListener objLocListener;
    objLocation=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    objLocListener=new MiPosicion();
    if(objLocation.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    objLocation.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, objLocListener);
        if(MiPosicion.latitud>0){

            l=MiPosicion.coordenadas;
        }
    }

To be able to save get the localization I made the following Class:

public class MiPosicion implements LocationListener {
public static double latitud;
public static double longitud;
public static boolean statusGPS;
public static Location coordenadas;

@Override
public void onLocationChanged(Location location) {
    latitud=location.getLatitude();
    longitud=location.getLongitude();
    coordenadas=location;
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {
statusGPS=true;
}

@Override
public void onProviderDisabled(String provider) {
statusGPS=false;
}
}

The problem I have is that the onLocationChange(location) method is not executed when objLocation.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,objLocListener); is executed How can I fix it?

 8
Author: Jorgesys, 2016-04-26

1 answers

The way to get it to be called onLocationChange() is when we define requestLocationUpdates(),

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIEMPO_ENTRE_UPDATES, MIN_CAMBIO_DISTANCIA_PARA_UPDATES, locListener, Looper.getMainLooper());

The second parameter defines the time between each update and the third parameter, the minimum change in distance for each update.

For example if we define:

  //Minimo tiempo para updates en metros.
    private static final long MIN_CAMBIO_DISTANCIA_PARA_UPDATES = 10; // 10 metros
    //Minimo tiempo para updates en Milisegundos
    private static final long MIN_TIEMPO_ENTRE_UPDATES = 1000 * 60 * 1; // 1 minuto

This means that if we move 10 meters and 1 minute elapses , the method onLocationChanged() will be called, where we can obtain the new geolocation values provided by the provider.

If you do not change the geolocation because the device was not moved it is because it is not necessary to obtain new geolocation values and the latest acquired ones are used.

I add an example so that you yourself can test what I comment:

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LocationActivity extends AppCompatActivity {

    private static final String TAG = "LocationActivity";
    private LocationManager mLocMgr;
    private TextView textViewGPS;

    //Minimo tiempo para updates en Milisegundos
    private static final long MIN_CAMBIO_DISTANCIA_PARA_UPDATES = 10; // 10 metros
    //Minimo tiempo para updates en Milisegundos
    private static final long MIN_TIEMPO_ENTRE_UPDATES = 1000 * 60 * 1; // 1 minuto

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout rl = new FrameLayout(this.getApplicationContext());
        LinearLayout linearLayout = new LinearLayout(this.getApplicationContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        setContentView(rl);
        rl.addView(linearLayout);

        textViewGPS = new TextView(getApplicationContext());
        linearLayout.addView(textViewGPS);


        mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //Requiere permisos para Android 6.0
            Log.e(TAG, "No se tienen permisos necesarios!, se requieren.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 225);
            return;
        }else{
            Log.i(TAG, "Permisos necesarios OK!.");
            mLocMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIEMPO_ENTRE_UPDATES, MIN_CAMBIO_DISTANCIA_PARA_UPDATES, locListener, Looper.getMainLooper());
        }

    }

    public LocationListener locListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.i(TAG, "Lat " + location.getLatitude() + " Long " + location.getLongitude());
            textViewGPS.setText("Lat " +   location.getLatitude() + " Long " + location.getLongitude());
        }

        public void onProviderDisabled(String provider) {
            Log.i(TAG, "onProviderDisabled()");
        }

        public void onProviderEnabled(String provider) {
            Log.i(TAG, "onProviderEnabled()");
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.i(TAG, "onStatusChanged()");
        }
    };

}
 9
Author: Jorgesys, 2016-05-11 23:54:02