2010-12-17 70 views
2

我想知道如何从最好的供应商处获得位置
我必须为GPS和网络1制定两个单独的标准吗?还是有办法将它们全部一起 ?
这是我的代码,当我添加COARSE标准的GPS不会继续(屏幕上方没有GPS闪烁的标志),当我使用FINE标准,我没有从网络上得到任何东西.....我是否必须为两者编写标准并在它们之间切换以获得所有可用的标准,或者它们是否都符合相同的标准?
因为我有“getBestProvider(criteria,true);”在我的代码,所以它应该得到最好的提供商的位置....对.. ??!GPS和网络的标准

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
//// criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setCostAllowed(true); 
criteria.setPowerRequirement(Criteria.POWER_HIGH); 
String provider = locationManager.getBestProvider(criteria, true); 
+0

为什么你甚至调用'setAccuracy()',因为显然你愿意接受任何准确性? – CommonsWare 2010-12-17 10:43:17

+0

我想要的准确性,但在一些地方,如隧道或高层建筑物,你可以从网络获得的唯一位置... – moe 2010-12-17 15:11:25

回答

1

使用getBestProviderCriteria.ACCURACY_FINE永远不会返回NETWORK_PROVIDER,即使无线网络位置通常是相当准确的。

在我的应用程序中,我使用getProviders得到所有提供商匹配我的标准,然后添加NETWORK_PROVIDER,如果它被激活,并且尚未在列表中。

然后,我为这些供应商中的每家供应商推出requestLocationUpdates,确保在获得足够准确的位置时致电removeUpdates

这样,如果由网络提供的定位是不够准确的GPS供应商将被关闭

0

@nicopico getBestProvider与Criteria.ACCURACY_FINE可以返回NETWORK_PROVIDER如果没有启用GPS 我用下面的在我的应用程序代码

public void getLocation() { 
     // Getting Google Play availability status 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); 

//  boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     // Showing status 
     if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available 
      Log.e(TAG, "getLocation fired Google Play Services are not available"); 

      mHandler.post(new UiToastCommunicaton(context.getApplicationContext(), 
        context.getResources().getString(R.string.gpserv_notfound))); 
     } 

     // Getting LocationManager object from System Service LOCATION_SERVICE 
     locationManager = (LocationManager) context.getSystemService(IntentService.LOCATION_SERVICE); 
     if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//check if network location provider context on 
      Log.e(TAG, "network location provider not enabled"); 
     } else { 
      criteria = new Criteria(); 
      criteria.setAccuracy(Criteria.ACCURACY_FINE); 
      criteria.setAltitudeRequired(false); 
      criteria.setBearingRequired(false); 
      criteria.setCostAllowed(true); 
      //criteria.setPowerRequirement(Criteria.POWER_LOW); 
      // Getting the name of the best provider 
      provider = locationManager.getBestProvider(criteria, true); 

      // Check provider exists then request for location 
      if (provider != null) { 
       requestLocation(locationManager, provider); 
      } else { 
       //start wifi, gps, or tell user to do so 
      } 

     } 
    } 
public void requestLocation(LocationManager locationManager, String provider) { 
Log.e(TAG, "requestLocation fired getting location now"); 

if (provider != null) { 

    locationManager.requestLocationUpdates(provider, 0, 0, this); 
    //locationManager.requestLocationUpdates(provider, 0, 0, this, Looper.getMainLooper()); 
} else { 
    //tell user what has happened 
} 
} 

这个代码是实现LocationListener的 您可以编辑这行

locationManager.requestLocationUpdates(provider, 0, 0, this); 

反映你的位置监听器的实现