-1
我在做一個應用程序,就可以訪問和使用用戶的經度和緯度,顯示附近的餐館。訪問緯度和經度谷歌Android API
我試圖從onConnected(Bundle bunle)方法中檢索緯度和經度變量,如下所示,以訪問並在onCreate()方法中使用它們,但是我在頂部聲明的字符串緯度不會存儲當我將它設置爲等於onConnnected()方法的用戶緯度時的緯度值。我嘗試測試在Toast消息中顯示String時的結果,但Toast只是空白,表示該字符串未設置爲等於緯度值。但是,如果我嘗試在onConnected()方法的範圍內顯示吐司,它就可以工作。但我真的需要在onCreate()方法之外訪問onConnected()方法之外的經度和緯度。
我已經發佈下面的代碼!任何幫助將不勝感激!
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, com.google.android.gms.location.LocationListener {
protected static final String TAG = "Gokul Log Message: ";
protected GoogleApiClient mGoogleApiClient;
protected Location mLastLocation;
protected TextView textLatitude;
public String mLat = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLatitude = (TextView)this.findViewById(R.id.textLatitude);
Toast.makeText(MainActivity.this, mLat, Toast.LENGTH_LONG).show();
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient(){
Log.v("Log1", "Entering buildGoogleApiClient method");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart(){
Log.v("Log1", "Entering onStart() method");
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop(){
Log.v("Log1", "Entering onStop() method");
super.onStop();
if (mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
@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
public void onConnected(Bundle bundle) {
Log.v("Log1", "Entering onConnected() method");
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null){
textLatitude.setText(String.valueOf(mLastLocation.getLatitude()));
//textLongitude.setText(String.valueOf(mLastLocation.getLongitude()));
mLat = String.valueOf(mLastLocation.getLatitude());
Log.v("Log1onConnected", mLat + "asd");
}
Log.v("Log1onConnected2", mLat + "asd");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed: " + connectionResult.getErrorCode());
}
}
嗨加布!我認爲我們有一個誤解讓我重述這個問題。我知道,它不會讓我不斷的位置更新,但目前的代碼會給我一個靜態的,不變的緯度值,我已經設置爲等於字符串mLat。當我嘗試在烤麪包上顯示此值時,字符串不會出現。它不是位置更新的問題,它的字符串根本不會存儲緯度的值。那有意義嗎?會很好,如果我能得到這個幫助:) – gkquantum17
手機並不總是知道它的位置。如果沒有人主動請求更新,它不會在後臺保持跟蹤。當你問它getLastLocation時,如果某個應用程序最近正在更新,那麼該函數將只返回一個很好的值。如果不是,它將不會有一個並返回null。這就是你在這裏看到的。 –
但是,當我將textveiw的文本設置爲等於onConnected()方法內的緯度時,它將完美顯示緯度。但是它不會讓我訪問我設置爲等於onConnected()方法之外的緯度的字符串。林詢問是否有任何方式訪問緯度作爲該函數外的字符串,就像onCreate()方法,我有一個敬酒等於緯度? – gkquantum17