Unable to get current GPS Location co-ordinates

痴心易碎 提交于 2019-12-24 15:52:14

问题


I have been trying hard to get my current location's GPS co-ordinates but my app never locks on to a GPS satellite.

The GPS icon in the notification area just keeps on blinking.

Whereas I tried using Google Maps on Android (the pre-installed app) and that thing is able to lockon in approx 60 secs! In both of the cases my 3G data connection was switched on and working.

Update: I am using Android 2.3.3 (HTC Desire S (Factory installed OS; no updates applied)) Logcat output is here. Now this is without setting LocationUpdates()'s min time and min-distance between update to 0, 0.

Update #2: My earlier code is here(PasteBin Link).

Update #3: Now, I am getting a force close after displaying a Toast .."Available".. in on onStatusChanged().

Update #4: Finally..I got it to work.

--
So, is it like that the Google map's app uses some proprietary code for locking on to GPS signals? I have tried using various version of my code. Tried using criteria(s) but never got them to work with GPS. For me getting precise (~50ft accuracy) location co-ordinates through GPS is a must.

My Code:

public class LocationDemoActivity extends Activity implements LocationListener {
    LocationManager locationManager;
    StringBuilder builder;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000l, 50.0f, this);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        locationManager.removeUpdates(this);
        locationManager = null;
        Intent i = new Intent(
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(i);

    }

    @Override
    public void onLocationChanged(Location location) {
//      builder = new StringBuilder();
        double lati=location.getLatitude();
        double longi=location.getLongitude();
        double alti=location.getAltitude();
        float acc=location.getAccuracy();
        float speed=location.getSpeed();
        long time=location.getTime();

        System.out.println(lati);
        System.out.println(longi);
        System.out.println(alti);
        System.out.println(acc);
        System.out.println(speed);
        System.out.println(time);

        /*Toast.makeText(this, "Lati: " + lati,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Long: " + longi,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Alti: " + alti,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Acc.: " + acc,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Speed: " + speed,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Time: " + time,Toast.LENGTH_SHORT).show();*/


        builder.append("Longitide: " + location.getLongitude());
        builder.append('\n');
        builder.append("Latitude: " + location.getLatitude());
        builder.append('\n');
        builder.append("Altitude: " + location.getAltitude());
        builder.append('\n');
        builder.append("Accuracy: " + location.getAccuracy());
        builder.append('\n');
        builder.append("TimeStamp:" + location.getTime());
        builder.append('\n');
        System.out.println(builder.toString());

        Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        System.out.println("Provider Disabled:");
        Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    @Override
    public void onProviderEnabled(String provider) {
        System.out.println("Provider Enabled:");
        Toast.makeText(this, "GPS is now enabled...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
        case LocationProvider.OUT_OF_SERVICE:
            System.out.println("Status Changed: Out of Service");
            Toast.makeText(this, "Status Changed: Out of Service",
                    Toast.LENGTH_SHORT).show();
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            System.out.println("Status Changed: Temporarily Unavailable");
            Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                    Toast.LENGTH_SHORT).show();
            break;
        case LocationProvider.AVAILABLE:
            System.out.println("Status Changed: Available");
            Toast.makeText(this, "Status Changed: Available",
                    Toast.LENGTH_SHORT).show();
            break;
        }
    }

}

Please do answer as it's quite urgent on me and any help is greatly appreciable :)

Thanks..


回答1:


It seams pretty obvious that you won't get any Location updates, because you have set the minDistance to 50 meters or 164.042 ft. It appears you have confused this with accuracy.

The minDistance parameter is the minimum distance between location updates. So you would have to move at least 50 meters to get a location update.

Also make sure you have a clear view of the sky in order to have GPS signal.

Read more in the documentation

http://developer.android.com/reference/android/location/LocationManager.html




回答2:


I asked a related question here.

To me looks like your GPS on the phone is not able to see the sat's. You need to get our of your office/home onto open air. In my case, I moved to NetworkProvider since GPS was just too clumsy.

Also note, that the parameters you give for distance and time are not literal, its the best guess that the api makes. So dont count on response times/distances from the API callback.




回答3:


Do you have all these manifest permissions?

ACCESS_COARSE_LOCATION

ACCESS_FINE_LOCATION

ACCESS_LOCATION_EXTRA_COMMANDS

ACCESS_MOCK_LOCATION

CONTROL_LOCATION_UPDATES

INTERNET


来源:https://stackoverflow.com/questions/14692202/unable-to-get-current-gps-location-co-ordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!