2015-06-19 63 views
2

我跟着教程使用谷歌地圖API來獲取最後的已知位置或請求位置更新,如果沒有這樣的位置。更新後如何獲取位置?

這裏是我的onConnected方法

@Override 
public void onConnected(Bundle bundle) { 
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     Log.v(TAG, "old location" + location); 

    } 
    else { 
     handleNewLocation(location); 
     Log.v(TAG, "new location"); 
    } 
} 

所以第一個條件應該申請一個新的位置。這個位置在哪裏存儲?我想這可能執行handeNewLocation(代碼如下所示)

private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 
    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 
    loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude); 
    Log.v(TAG, loc); 
} 

我需要得到它的位置和存儲爲一個字符串,所以我的問題是我不知道哪兒

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

正在保存新的位置。

這裏是清單:

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
    </activity> 
    <activity 
     android:name=".GiveActivity" 
     android:label="@string/title_activity_give" 
     android:parentActivityName=".MainActivity" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="ashkanbakhshai.com.greenlight.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".PopActivity" 
     android:label="@string/title_activity_pop" 
     android:theme="@style/fullScreen" > 
    </activity> 
    <activity 
     android:name=".SignatureActivity" 
     android:fillViewport="true" 
     android:label="@string/title_activity_signature" 
     android:screenOrientation="landscape" > 
    </activity> 
    <activity 
     android:name=".OwnerActivity" 
     android:label="@string/title_activity_owner" 
     android:screenOrientation="landscape" 
     android:theme="@style/Theme.AppCompat.Dialog" > 
    </activity> 
    <activity 
     android:name=".TakeActivity" 
     android:label="@string/title_activity_take" 
     android:screenOrientation="portrait" > 
    </activity> 
    <activity 
     android:name=".SignatureOwnActivity" 
     android:label="@string/title_activity_signature_own" 
     android:screenOrientation="landscape" > 
    </activity> 
    <activity 
     android:name=".GreenLighted" 
     android:label="@string/title_activity_green_lighted" 
     android:screenOrientation="portrait" > 
    </activity> 
    <activity 
     android:name=".GameActivity" 
     android:label="@string/title_activity_game" > 
    </activity> 
    <activity 
     android:name=".GameOwnActivity" 
     android:label="@string/title_activity_game_own" > 
    </activity> 
    <activity 
     android:name=".RedLighted" 
     android:label="@string/title_activity_red_lighted" 
     android:theme="@style/giveFull" > 
    </activity> 
    <activity 
     android:name=".TestActivity" 
     android:label="@string/title_activity_test" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".HistoryActivity" 
     android:label="@string/title_activity_history" > 
    </activity> 
    <activity 
     android:name=".DetailHistoryActivity" 
     android:label="@string/title_activity_detail_history" > 
    </activity> 
    <activity 
     android:name=".RegisterActivity" 
     android:label="@string/title_activity_register" > 
    </activity> 
    <activity 
     android:name=".NavigationActivity" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:label="@string/title_activity_navigation" > 
    </activity> 
    <activity 
     android:name=".ExitActivity" 
     android:label="@string/title_activity_exit" 
     android:theme="@style/Theme.AppCompat.Dialog" > 
    </activity> 
    <activity 
     android:name=".ExitOwnActivity" 
     android:label="@string/title_activity_exit_own" 
     android:theme="@style/Theme.AppCompat.Dialog" > 
    </activity> 
    <activity 
     android:name=".HisListActivity" 
     android:label="@string/title_activity_his_list" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="ashkanbakhshai.com.greenlight.HistoryActivity" /> 
    </activity> 
    <activity 
     android:name=".SettingsActivity" 
     android:label="@string/title_activity_settings" > 
    </activity> 
    <activity 
     android:name=".GameAltActivity" 
     android:label="@string/title_activity_game_alt" > 
    </activity> 
    <activity 
     android:name=".GameOwnAltActivity" 
     android:label="@string/title_activity_game_own_alt" > 
    </activity> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <meta-data 
     android:name="com.google.android.geo.AIzaSyBhbrcIcN2yQV883z8bwe3Mm5-PnHUgmlI" 
     android:value="AIzaSyBhbrcIcN2yQV883z8bwe3Mm5-PnHUgmlI" /> 
    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="@string/google_maps_key" /> 

</application> 

回答

1

請求位置更新的調用就是這樣,它請求位置更新。

之後你打這通電話,新的地點將進來onLocationChanged()

如果你想永遠在當前位置進行保存,你可以用成員變量這樣做。

注意,沒有必要進行登錄,你現在正在做一個空位置,所以刪除的情況下日誌條目該位置是空:

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
    else { 
     handleNewLocation(location); 
     Log.v(TAG, "new location"); 
    } 

然後在handleNewLocation()更新成員變量:

private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 
    currentLatitude = location.getLatitude(); //update member variable 
    currentLongitude = location.getLongitude(); //update member variable 
    loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude); 
    Log.v(TAG, loc); 
} 

然後,調用handleNewLocation()onLocationChanged()回調:

@Override 
public void onLocationChanged(Location location) { 

    //location changed, update current location 
    handleNewLocation(location); 
    Log.v(TAG, "new location"); 

} 

滿級:

public class LocationActivity extends Activity implements 
     GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    double currentLatitude; //added as member variable 
    double currentLongitude; //added as member variable 
    String TAG = "myapp"; 
    String loc; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     buildGoogleApiClient(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onPause(){ 
     super.onPause(); 
     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 
    } 

    protected synchronized void buildGoogleApiClient() { 
     Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show(); 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(10); 
     mLocationRequest.setFastestInterval(10); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     //mLocationRequest.setSmallestDisplacement(0.1F); 

     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 
     else { 
      handleNewLocation(location); 
      Log.v(TAG, "new location"); 
     } 

    } 

    private void handleNewLocation(Location location) { 
     Log.d(TAG, location.toString()); 
     currentLatitude = location.getLatitude(); //update member variable 
     currentLongitude = location.getLongitude(); //update member variable 
     loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude); 
     Log.v(TAG, loc); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     //location changed, update current location 
     handleNewLocation(location); 
     Log.v(TAG, "new location"); 

    } 
} 
1

你需要調用

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    Log.v(TAG, "old location" + location); 

你叫

之前
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

那麼位置將被存儲在location

+0

然後我的條件將無法工作,因爲我測試,如果位置是空,其實我需要請求位置更新,或者如果我可以只使用位置。你認爲if語句是不必要的嗎? – Alex

+0

是的,我已經完成了這件事[之前](https://github.com/KevCron/Tracking/blob/master/app/src/main/java/com/kevincronly/trackingapp/Tracker.java)。 –

+0

好吧,我會嘗試這個並更新。 – Alex

相關問題