0

I want to get my current location, but sometimes my GPS can't get a location and show the grey point in google maps. I want to get latitude and longitude from that grey point when my GPS doesn't work well. How can I get that coordinate?

1: image from grey point

I use this solution Get user's current location using GPS but I still got null for my coordinate, but works well if my GPS worked well when my google maps showing blue point

public class GpsLocationTracker extends Service implements LocationListener


/**
 * context of calling class
 */

private Context mContext;

/**
 * flag for gps status
 */
private boolean isGpsEnabled = false;

/**
 * flag for network status
 */
private boolean isNetworkEnabled = false;

/**
 * flag for gps
 */
private boolean canGetLocation = false;

/**
 * location
 */
private Location mLocation;

/**
 * latitude
 */
private double mLatitude;

/**
 * longitude
 */
private double mLongitude;

/**
 * min distance change to get location update
 */
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;

/**
 * min time for location update
 * 60000 = 1min
 */
private static final long MIN_TIME_FOR_UPDATE = 60000;

/**
 * location manager
 */
private LocationManager mLocationManager;


/**
 * @param mContext constructor of the class
 */
public GpsLocationTracker(Context mContext) {

    this.mContext = mContext;
    getLocation();
}


/**
 * @return location
 */
public Location getLocation() {

    try {

        mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        /*getting status of the gps*/
        isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        /*getting status of network provider*/
        isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGpsEnabled && !isNetworkEnabled) {

            /*no location provider enabled*/
        } else {

            this.canGetLocation = true;

            /*getting location from network provider*/
            if (isNetworkEnabled) {

                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);

                if (mLocationManager != null) {

                    mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (mLocation != null) {

                        mLatitude = mLocation.getLatitude();

                        mLongitude = mLocation.getLongitude();
                    }
                }
                /*if gps is enabled then get location using gps*/
                if (isGpsEnabled) {

                    if (mLocation == null) {

                        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);

                        if (mLocationManager != null) {

                            mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if (mLocation != null) {

                                mLatitude = mLocation.getLatitude();

                                mLongitude = mLocation.getLongitude();
                            }

                        }
                    }

                }
            }
        }

    } catch (Exception e) {

        e.printStackTrace();
    }

    return mLocation;
}

/**
 * call this function to stop using gps in your application
 */
public void stopUsingGps() {

    if (mLocationManager != null) {

        mLocationManager.removeUpdates(GpsLocationTracker.this);

    }
}

/**
 * @return latitude
 *         <p/>
 *         function to get latitude
 */
public double getLatitude() {

    if (mLocation != null) {

        mLatitude = mLocation.getLatitude();
    }
    return mLatitude;
}

/**
 * @return longitude
 *         function to get longitude
 */
public double getLongitude() {

    if (mLocation != null) {

        mLongitude = mLocation.getLongitude();

    }

    return mLongitude;
}

/**
 * @return to check gps or wifi is enabled or not
 */
public boolean canGetLocation() {

    return this.canGetLocation;
}

/**
 * function to prompt user to open
 * settings to enable gps
 */
public void showSettingsAlert() {

    AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(new ContextThemeWrapper(mContext, R.style.AppTheme));

    mAlertDialog.setTitle("Gps Disabled");

    mAlertDialog.setMessage("gps is not enabled . do you want to enable ?");

    mAlertDialog.setPositiveButton("settings", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent mIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(mIntent);
        }
    });

    mAlertDialog.setNegativeButton("cancle", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();

        }
    });

    final AlertDialog mcreateDialog = mAlertDialog.create();
    mcreateDialog.show();
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

and I use that class in MyActivity

Double sLatitude, sLongitude;
                GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(CheckInOutDetailActivity.this);

                /**
                 * Set GPS Location fetched address
                 */
                if (mGpsLocationTracker.canGetLocation())
                {
                    sLatitude = mGpsLocationTracker.getLatitude(); //0.0 if gmaps grey;
                    sLongitude = mGpsLocationTracker.getLongitude(); //0.0 if gmaps grey


                }
                else
                {
                    mGpsLocationTracker.showSettingsAlert();
                }

I also use FusedLocation but still get null if my gmaps is grey point

public void capturePhoto()\\{
FusedLocationProviderClient client;
String sLatitude, sLongitude;

client = LocationServices.getFusedLocationProviderClient(this);
client.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
    @Override
    public void onSuccess(Location location) {
        if (location != null) {
            sLatitude = String.valueOf(location.getLatitude());
            sLongitude = String.valueOf(location.getLongitude());


        }
        else
        {



            GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(CheckInOutDetailActivity.this);

            /**
             * Set GPS Location fetched address
             */
            if (mGpsLocationTracker.canGetLocation())
            {
                sLatitude = mGpsLocationTracker.getLatitude()+"";
                sLongitude = mGpsLocationTracker.getLongitude()+"";


            }
            else
            {
                mGpsLocationTracker.showSettingsAlert();
            }

        }
    }
});

}

  • Show us the code. – meditat Aug 06 '18 at 02:46
  • @meditat I just edit my post and add my code – Dhaba Widhikari Aug 06 '18 at 03:11
  • What is your real problem? You want to coordinate from grey point or you want to get current location? – RoShan Shan Aug 06 '18 at 03:49
  • @RoShanShan I want to get my current location, but sometimes my GPS doesn't work. So I think I have to get grey location to handle error get my current location. – Dhaba Widhikari Aug 06 '18 at 03:57
  • Your codes is old. You need to update new api from Google to get the latest location. You should use `FusedLocationProviderClient` from this link https://developer.android.com/training/location/retrieve-current – RoShan Shan Aug 06 '18 at 04:05
  • @RoShanShan I already use that solution, but still get null when get latitude and longitude. But works well if my google maps is blue point – Dhaba Widhikari Aug 06 '18 at 04:15
  • wait I just forgot to add permission coarse location – Dhaba Widhikari Aug 06 '18 at 04:23
  • I used `FusedLocationProviderClient` and it alway get current location if you turn on permission Location for the app. When google map has blue points, you mean you turned on Location? If google map doesn't have bule, you forgot add permission. – RoShan Shan Aug 06 '18 at 04:23
  • @RoShanShan I used FusedLocationProviderClient too but when gmaps is showing grey point it doesn't work and always return null. My gps is on btw, but sometimes it doesn't work and showing grey points, but sometimes it works and showing blue points. My device is Xiaomi Mi 6X btw – Dhaba Widhikari Aug 06 '18 at 04:44

0 Answers0