【问题标题】:Accessing Latitude and Longitude Android Google API访问经纬度 Android Google API
【发布时间】:2015-07-20 20:31:31
【问题描述】:

我正在制作一个能够访问并使用用户的纬度和经度来显示附近餐馆的应用。

我试图从 onConnected(Bundle bunle) 方法中检索纬度和经度变量,如下所示,以便在 onCreate() 方法中访问和使用它们,但是我在顶部声明的字符串纬度不会存储当我将其设置为等于 onConnnected() 方法上的用户纬度时的纬度值。当我在 toast 消息上显示字符串时,我尝试测试结果是什么,但 toast 只是空白,表明字符串尚未设置为等于纬度值。但是,如果我尝试在 onConnected() 方法的范围内显示 toast,它会起作用。但我确实需要在 onConnected() 方法之外的 onCreate() 方法中访问这个纬度和经度。

我已经发布了下面的代码!任何帮助将不胜感激!

    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());

}

}

【问题讨论】:

    标签: android google-maps google-play-services latitude-longitude google-geocoding-api


    【解决方案1】:

    它没有改变,因为它还没有位置。仅仅因为您连接到 google play 并不意味着位置已准备好。您需要监听位置更新并在 onLocationChanged 中设置值。

    【讨论】:

    • 嗨,加布!我认为我们有一个误解,让我重新表述这个问题。我知道它不会让我不断更新位置,但当前代码会给我一个静态的、不变的纬度值,我已经设置为等于字符串 mLat。当我尝试在吐司上显示这个值时,字符串根本不会出现。它不是位置更新的问题,它的字符串根本不会存储纬度的值。那有意义吗?如果我能在这方面得到帮助会很棒:)
    • 手机并不总是知道它的位置。如果没有人主动请求更新,它不会在后台跟踪。当您向它询问 getLastLocation 时,该函数只会在某个应用最近获得更新时返回一个好的值。如果没有,它将没有一个并返回 null。这就是你在这里看到的。
    • 但是当我在 onConnected() 方法中将 textveiw 的文本设置为等于纬度时,它会完美地显示纬度。但它不会让我访问我设置为等于 onConnected() 方法之外的纬度的字符串。我问是否有任何方法可以在该函数之外以字符串的形式访问纬度,例如在 onCreate() 方法中,我有一个等于纬度的吐司?
    • 当然,您可以将值保存到类级别变量中。但是 onConnected 是异步的——直到 onCreate 完成很久之后才会调用它。这就是为什么它是一个回调函数,否则连接调用只会返回 true 或 false 的连接状态。
    • 谢谢!您能否更详细地说明我如何做到这一点?现在我试图将纬度值从 onConnected 保存到一个字符串,然后我可以从 onCreate() 方法显示为 toast。有没有办法做到这一点?我是 android 新手,所以请多多包涵 :) 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多