我正在创建一个应用程序,可以访问并使用用户的经度和经度来显示附近的餐馆。
我试图从onConnected(Bundle bunle)方法中检索纬度和经度变量,如下所示,以便在onCreate()方法中访问和使用它们,但我在顶部声明的String纬度不会存储当我将其设置为等于onConnnected()方法上的用户纬度时的纬度值。我尝试测试当我在Toast消息上显示String时结果是什么,但是toast只是空白,表明该字符串未设置为等于纬度值。但是,如果我尝试在onConnected()方法的范围内显示toast,它就可以工作。但我真的需要在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());
}
}
答案 0 :(得分:2)
它没有改变,因为它还没有位置。仅仅因为你连接谷歌播放并不意味着一个位置准备好了。您需要侦听位置更新并在onLocationChanged中设置值。