2012-01-31 55 views
1

我需要監視用戶的位置獲得經緯度,當我使用網絡提供商或GPS提供商,其工作fine.But當我使用這兩個提供商我沒有得到價值。任何人友好給解。正在尋找和嘗試很多例子最簡單的方式使用網絡和GPS衛星Android

// Get the location manager 
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
// Define the criteria how to select the locatioin provider -> use 
// default 
Criteria criteria = new Criteria(); 
provider = locationManager.getBestProvider(criteria, true); 
networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    if (networkLoc != null) { 
    System.out.println("Provider " + provider + " has been selected."); 
    double l = (double) (networkLoc.getLatitude()); 
    double lng11 = (double) (networkLoc.getLongitude()); 
    latituteField1.setText(Double.toString(l)); 
    longitudeField1.setText(Double.toString(lng11)); 
    } 
    if (gpsLoc != null) { 
    System.out.println("Provider " + provider + " has been selected."); 
    double l0t = (double) (gpsLoc.getLatitude()); 
double l0g = (double) (gpsLoc.getLongitude()); 
    latituteField.setText(Double.toString(l0t)); 
longitudeField.setText(Double.toString(l0g)); 

} 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

    } 
@Override 
protected void onResume() { 
super.onResume(); 

locationManager.requestLocationUpdates(
     LocationManager.NETWORK_PROVIDER, 
     MINIMUM_TIME_BETWEEN_UPDATES, 
     MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
     this 
); } 
    /* Remove the locationlistener updates when Activity is paused */ 
    @Override 
protected void onPause() { 
super.onPause(); 
locationManager.requestLocationUpdates(
     LocationManager.NETWORK_PROVIDER, 
     MINIMUM_TIME_BETWEEN_UPDATES, 
     MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
     this 
); 
} 

     @Override 
    public void onLocationChanged(Location location) { 
lat1 = (double) (networkLoc.getLatitude()); 
lng1 = (double) (networkLoc.getLongitude()); 
latituteField1.setText(Double.toString(lat1)); 
longitudeField1.setText(Double.toString(lng1));  
lat = (double) (gpsLoc.getLatitude()); 
lng = (double) (gpsLoc.getLongitude()); 
latituteField.setText(Double.toString(lat)); 
longitudeField.setText(Double.toString(lng)); 
    } 
@Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
// TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
Toast.makeText(this, "Enabled new provider " + provider, 
     Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
Toast.makeText(this, "Disenabled provider " + provider, 
     Toast.LENGTH_SHORT).show(); 
} 

    protected void Display(Cursor c) { 
Toast.makeText(this, "rowid: " + c.getString(0) + "\n" + 
"Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" + 
Toast.LENGTH_LONG, 0).show(); 
    } 
+0

你可以發佈你正在使用和未使用的代碼? – SERPRO 2012-01-31 10:20:32

+0

你能指導我什麼問題在我的代碼 – Mercy 2012-01-31 10:35:41

回答

1

不從多個供應商請求位置更新..相反,選擇一個Criteria一個Android會選擇最適合你的。

例如,而不是:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

用途:

Criteria c = new Criteria(); 
    c.setAccuracy(Criteria.ACCURACY_COARSE); //Or whatever criteria you want 
    final String PROVIDER = locationManager.getBestProvider(c, true); 
    locationManager.requestLocationUpdates(PROVIDER, minTime, minDistance, this); 
+0

謝謝我想從網絡供應商和GPS提供商獲得緯度價值。 – Mercy 2012-01-31 10:59:10

+0

是否可以找到哪個提供商? – Mercy 2012-01-31 11:26:48

+0

那麼你可以得到每個提供商的最後一個已知的位置..但它可能是和舊的位置.. http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String ) – SERPRO 2012-01-31 11:29:32