2010-12-17 71 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); 

反映你的位置監聽器的實現