-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