Android GPS get current location
Asked Answered
B

3

6

I developed a simple Android application and in this simple application I want to get GPS Latitued and Longitued data, it works correctly in my emulator but when I installed on real Android device it does not work. Please help me to solve this problem and the follow is my code. Thanks in advance

public class getLocation extends AppCompatActivity {

private Button btnGetLocation;
private TextView textGPS;
private LocationManager locationManager;
private LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_location);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    btnGetLocation = (Button) findViewById(R.id.btnGetLocation);
    textGPS = (TextView) findViewById(R.id.textViewGps);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            textGPS.setText("lat: "+location.getLatitude()+" long: "+location.getLongitude());

        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

        }
    };

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.INTERNET
            },10);
            return;
        }
    }else{
        configureButton();
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    switch (requestCode){
        case 10:
            if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                configureButton();
            return;
    }
}

private void configureButton() {

    btnGetLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            locationManager.requestLocationUpdates("gps", 0, 0, locationListener);
        }
    });
}

}

Screen shot from emulator: enter image description here

Screen shot from mobile: enter image description here

Bouncing answered 9/4, 2016 at 6:32 Comment(2)
You're requesting only GPS... Your device needs to make contact with satellites! Walk outside and test it! Also, you shouldn't use "gps" hard coded, you should use locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);Cornew
Thanks I walked outside it worksBouncing
C
5

You're requesting only GPS... Your device needs to make contact with satellites! In space! Walk outside and test it! This is a common issue when testing code that uses only GPS, it doesn't work very well indoors.

Also, you shouldn't use "gps" hard coded, you should use:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

As for using the FusedLocationProviderAPI, you will lose some accuracy, but it does offer much improved battery performance, since it relies heavily on Network Location, and only uses GPS if absolutely necessary. For a complete example of how to use it, see here: https://mcmap.net/q/669106/-blue-dot-and-circle-is-not-shown-on-mylocation-using-android-fused-location-api

Cornew answered 9/4, 2016 at 7:40 Comment(2)
It works fine but when i want to save the Latitued and Longitued to database i cant get them.Bouncing
@GulMuhammadAkbari You should also request Network location, which usually gives a Location faster (when there are WiFi networks in range). Bottom line, you need to just wait until you get a Location before you save it to to the database, so save it to the database when you get Locations in the onLocationChanged() callback.Cornew
B
2

add to your code this. when this app run in indoor return last known location.

        // Get the location from the given provider
        Location location = locationManager
                .getLastKnownLocation(provider);

        locationManager.requestLocationUpdates(provider, 1000, 1, this);
Benfield answered 9/4, 2016 at 8:32 Comment(0)
S
1

The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app.

You can find the example how to use it right here.

P.S. Don't forget to check if permissions granted on your device.

Sitting answered 9/4, 2016 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.