How to get the current latitude and longitude as double in android?

748 views Asked by At

According to this demo: Code

I want to get my current latitude as double or as String. via Internet or gps

But I thing via Internet better, at the time i use gps like that :

@Override
protected void onResume() {
    super.onResume();
    MyLocation(); }

public void MyLocation() { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(provider, 200, 1, this);
}


@Override
public void onLocationChanged(Location location) {
    a_lat =String.valueOf(location.getLatitude());
    a_lon =String.valueOf(location.getLongitude()); }   
1

There are 1 answers

2
shobhit sharma On

To request, you must use Location Manager and call location listener. In the method onLocationChanged(), you can access lattitude and longitude by : loc.getlatitude() and loc.getlongitude().

You should also add permission checks as location comes under dangerous permission group according to android deveopers https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous

Further, this github repo worked fine for me:

https://github.com/Shobhit95Sharma/Gps_location/blob/master/app/src/main/java/testing/gps_location/MainActivity.java