2016-01-23 78 views
0

我想在android上找到當前用戶位置。android:改進查找當前位置

我使用這些代碼:

mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(1000); // Update location every second 
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

      mMap.setMyLocationEnabled(true); 
      mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
        mGoogleApiClient); 
      if (mLastLocation != null) { 
       lat = String.valueOf(mLastLocation.getLatitude()); 
       lon = String.valueOf(mLastLocation.getLongitude()); 

      } 
      firstLoction(); 
     } 
     else 
     { 
      Toast.makeText(this, "Not promisseion", Toast.LENGTH_SHORT).show(); 
     } 

它與一些網絡(數據移動)。但不適用於所有位置。 (也適用於GPS)

我可以做更多的事情來處理網絡更好嗎?

+0

你不應該使用getLastLocation使用位置數據。構建LocationRequest之後,您應該根據此請求請求更新並實現LocationListener接口以接收它們。 – thetonrifles

回答

1

讓您的活動實施LocationListener的接口,然後利用FusedLocationApi,即採取一切可用的供應商(例如GPS,網絡)的照顧。

在你onConnected方法,你需要進行如下操作:

@Override 
public void onConnected(Bundle connectionHint) { 
    LocationRequest request = new LocationRequest(); 
    request.setInterval(10000); 
    request.setFastestInterval(5000); 
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    // using fused location api, taking care of all 
    // available providers (e.g. GPS, NETWORK) 
    // here do not forget to check for available LOCATION 
    // permission before asking for updates 
    LocationServices.FusedLocationApi.requestLocationUpdates(
     mGoogleApiClient, request, this); 
} 

由於您的活動實現LocationListener的界面,您將收到onLocationChanged方法的更新。

@Override 
public void onLocationChanged(Location location) { 
    // this method is defined by LocationListener interface 
    // here you will receive location updates based 
    // on fused location api   
} 

參見here關於如何提高代碼用於降低電池消耗的額外細節。

我看到您在您的活動中使用GoogleMap。也可以使用其方法setOnMyLocationChangeListener直接從GoogleMap對象獲取當前位置。不幸的是,這種方法現在已被棄用,所以建議使用FusedLocationProviderApi。

1
Context ctx = this; 
     gpsLocationListener = new OLL(ctx); 
     networkLocationListener = new OLL(ctx); 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location temploc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

if (useGPS) { 
      if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
       lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
        5000, 1, gpsLocationListener); 
       } 
     } 
     if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
      5000, 1, networkLocationListener); 
     } 

public class OLL implements LocationListener { 
Context ctx; 
private final static String TAG = "TAG"; 
    public OVoidLL(Context ctx) { 

     this.ctx = ctx; 
    } 
@Override 
    public void onLocationChanged(Location location) { 
Log.d(TAG, location.toString); 

    } 
    @Override 
    public void onProviderDisabled(String provider) { 
    } 
    @Override 
    public void onProviderEnabled(String provider) { 
    } 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
} 
1

看到這個link通過如何獲得和在Android應用