Finding current location works when using Wifi, but fails when using GPS

血红的双手。 提交于 2019-12-01 20:10:28

问题


I have developed an application to find the user's location using GPS.

In this there is no error. It finds the location using Wifi correctly, but in GPS mode it doesn't return any values. I added the needed permission in the manifest and GPS is on.

Can someone please tell me how to get the location using Gps?

I have included my code here:

package c.g.a.loation;

public class UseGps extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                mlocListener);
    }

    /* Class My Location Listener */
    public class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            Geocoder gp = new Geocoder(getBaseContext());
            try {
                ArrayList<Address> address = (ArrayList<Address>) gp
                        .getFromLocation(loc.getLatitude(), loc.getLongitude(),
                                1);
                if (address != null && address.size() > 0) {
                    Toast.makeText(getBaseContext(), address.get(0).toString(),
                            Toast.LENGTH_LONG).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

        @Override
        public void onProviderEnabled(String arg0) {}
    }

    public void onProviderEnabled(String provider) {
        // Toast.makeText(
        // getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
    }

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

回答1:


I have two thoughts...

  1. Your code is not checking to make sure the GPS provider is enabled. I see commented out code send a Toast when ANY provider is enabled, but you might want to check that the GPS provider is enabled in onCreate().

  2. How long are you waiting to get the GPS location? The GPS_PROVIDER can sometimes take a while to get a GPS fix. I have seen TTFF (time to fist fix) values of over two minutes before. To make matters worse, I have seen drastic TTFF differences in different versions of Android as well as different devices.

I would recommend that you also try getting a GPS fix from another app to see how long that takes. I generally launch either Google Maps, or GPS Status (free from the market) to get a feeling for how long they take to get a GPS fix. If you try Google Maps, be sure that it is getting the GPS fix and not only a location from the NETWORK_PROVIDER (the GPS icon at the top of your screen should stop flashing).



来源:https://stackoverflow.com/questions/5124525/finding-current-location-works-when-using-wifi-but-fails-when-using-gps

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