2015-07-10 142 views
1

在我的应用程序中,我尝试使用networkProvider搜索android设备的粗略位置。我只在我的位置管理器中使用networkProvider,但如果我不打开GPS传感器,它将不起作用。Android GPS网络提供者不工作

无论GPS传感器开启还是关闭,网络提供者是否应该给出一个粗略的位置?

这是我的代码。

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 


     if(isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
      Log.e("Network","suc"); 
     else 
      Log.e("Network", "fail"); 

     if (!isNetworkEnabled) { 

     } else { 
      this.isGetLocation = true; 

      if (isNetworkEnabled) { 
       Log.e("GpsInfo", "isNetworkEnabled true"); 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

       if (locationManager != null) { 
        Log.e("GpsInfoClass", "Location manager not NULL"); 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
                lat = location.getLatitude(); 
         lon = location.getLongitude(); 
         Log.e("GpsInfoClass", lon + ", " + lat); 
        }else{ 
         Log.e("GpsInfoClass", "Location NULL"); 
        } 
       }else{ 
        Log.e("GpsInfo", "Location Manager is null"); 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return location; 
} 

在我的日志中,它打印网络(标记)失败(日志)。

这是什么意思是说,

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 

总是返回false,如果GPS传感器处于关闭状态。这是为什么发生?我错过了如何获取粗略位置?

回答

1

GPS传感器只需要准确的位置。如果您创建了Criteria并将其提供给locationManager,系统将自动选择最佳提供者并尝试获取坐标。

示例;

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
Criteria locationMode = new Criteria(); 
locationMode.setBearingRequired(false); 
locationMode.setSpeedRequired(true); 
locationMode.setAccuracy(Criteria.ACCURACY_MEDIUM); 
locationManager.requestSingleUpdate(locationMode, new LocationListener() {@Override 
    public void onLocationChanged(Location location) { 
    } 

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

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
}, null); 
} 
1

是对networkprovider应该给粗略位置,无论GPS传感器是打开还是关闭?

不,GPS无线电与获取网络位置无关。

但是,您需要将您的位置设置设置为Power SavingHigh Accuracy

使用此代码来显示当前启用的供应商举杯:

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

    Toast.makeText(context, "GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled, Toast.LENGTH_LONG).show(); 

下面是它显示了GPS onlyPower Saving

enter image description here

enter image description here

所以,你可以看到这个电话:

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

对于仅限GPS的设置返回false,因为这会禁用网络位置。 而且,它对于节电设置返回true。

它也将返回高准确度,因为这使GPS和网络位置。