2015-06-22 71 views
3

我是新來的android。我正在嘗試一個代碼,以獲得每100米更新,即每100米的位置需要在我的應用程序TextView上更新。位置更新每100米使用LocationListener

這段代碼是否夠用,是因爲當我坐車或騎車時,TetView沒有正確更新(正確地說,我得到相同的位置,我需要關閉應用程序並重新打開它才能獲得新位置)

還有一個疑問就是,是否記下文件中最後一個知道的位置並稍後顯示它?如果我沒有網絡連接,我可以獲取最後一個已知位置。

我的代碼:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { 


    public static final String TAG=MainActivity.class.getSimpleName(); 
    UserLocationDetails locationDetails=new UserLocationDetails(); 
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
    GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 
    TextView userLocationView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     userLocationView=(TextView) findViewById(R.id.idLocationOfUser); 

     mGoogleApiClient=new GoogleApiClient.Builder(this). 
       addConnectionCallbacks(this). 
       addOnConnectionFailedListener(this). 
       addApi(LocationServices.API).build(); 
     mLocationRequest = LocationRequest.create().setSmallestDisplacement(100) 
       .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

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

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if(mGoogleApiClient.isConnected()){ 
      mGoogleApiClient.disconnect(); 
     } 
    } 

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

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

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.i(TAG,"Location Service Connected"); 
     Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
      Log.e(TAG,"Location is NULL"); 
     } 
     else { 
      handleNewLocation(location); 
     } 


    } 

    private void handleNewLocation(Location location) { 

     double latitude=location.getLatitude(); 
     double longitude=location.getLongitude(); 
     Log.i(TAG,"Latitude : "+location.getLatitude()); 
     Log.i(TAG,"Longitde : "+location.getLongitude()); 

     Log.i(TAG, "Location at : " + location.toString()); 

       locationDetails.setLocationDetailsOfUser(location.toString()); 

     userLocationView.setText(locationDetails.getLocationDetailsOfUser()); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i(TAG,"Location Service Suspended"); 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      } catch (IntentSender.SendIntentException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 

    } 


    @Override 
    public void onLocationChanged(Location location) { 
     handleNewLocation(location); 
    } 
} 
+1

請定義「沒有正確更新」。什麼是「正確的」,反而會發生什麼? – cygery

+1

我現在編輯它。感謝您讓我知道。在某種意義上,我得到相同的位置,我需要關閉應用程序,並重新打開它以獲得新的位置 –

+1

請發佈您的完整代碼。 – cygery

回答

0
@Override 
public void onLocationChanged(Location location) { 
    if (location.distanceTo(mCurrentLocation) > 100) 
    { 
     // do update stuff 
     mCurrentLocation = location; 
    } 
} 

哪裏mCurrentLocation是最後一個已知位置。

您不能根據距離請求位置更新。

+0

'你不能請求位置更新基於距離'哦?那麼請解釋一下'setSmallestDisplacement'的 – tyczj

+0

我應該這樣說。我假設他期望「setInterval」每次用戶移動100米時更新位置。 –

相關問題