如何通过 GPS 获取用户的位置,如果网络不可用

how to get the position of user by GPS and if not availabe by Network

我是 android 开发新手,所以对获取用户位置很感兴趣!!!!

您可能知道我们可以通过两种方式访问​​用户位置:

GPS (ACCESS_FINE_LOCATON) 基于网络 (ACCESS_COARSE_LOCATION)

我的问题是:

I Want to get the location(latittude & longitude) of user by GPS. But, if the user has turned GPS Off/GPS isn't supported, then i want to get user's location by Network, or, Network-based location

.

希望你理解我的问题.... 接受任何帮助

当您使用 FusedLocationProviderApi as used in the getting the last known location training and receiving location updates training 时,如果 GPS 不可用,它会自动回退到网络位置。

希望获取 GPS 代码对您有所帮助。

    if (Common.getIsGPSEnabled()) {
        final LocationManager locationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener;
        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {  
                myLocation = locationManager.getLastKnownLocation(locationProvider);  
                Toast.makeText(c, "location changed, My location is removed to"
                + myLocation.getLatitude()+","+myLocation.getLongitude(), Toast.LENGTH_LONG).show(); 
                Latitude = myLocation.getLatitude(); 
                Longitude = myLocation.getLongitude();
            }

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

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
          };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locationManager.requestLocationUpdates(locationProvider, (long)0, (float)0, locationListener); 
        myLocation = locationManager.getLastKnownLocation(locationProvider);
        if(myLocation != null)
        { 
               ///
        }
    }

试试下面的代码:

 LocationManager mlocManager; 
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
                    mlocListener = new MyLocationListener(); 
        //if condition to check if GPS is available 
if (mlocManager                   .isProviderEnabled(LocationManager.GPS_PROVIDER)) {                         mlocManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, 
                                mlocListener, null); 
                    } 
       else if (mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
                        mlocManager.requestSingleUpdate( 
                                LocationManager.NETWORK_PROVIDER, mlocListener, null); 
                    } 


        //this is the class to get the location. 
            public class MyLocationListener implements LocationListener { 

                @Override 
                public void onLocationChanged(Location location) { 
                    // TODO Auto-generated method stub 
                    try { 
    //you may store these values where ever required.  
                        Double latitude =  location.getLatitude();      
                        Double longitude =  location.getLongitude(); 
                    } catch (Exception e) {
                        e.printStackTrace();

                    } 

                } 

                @Override 
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub 

                } 

                @Override 
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub 

                } 

                @Override 
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub 

                } 

            }