Get Coordinate Latitude Longitude from The Grey Point Google Map

120 views Asked by At

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();
            }

        }
    }
});

}

0

There are 0 answers