【问题标题】:Cannot resolve method getlatitude()无法解析方法 getlatitude()
【发布时间】:2015-01-06 13:26:57
【问题描述】:

我是编程新手,正在学习 Google Location API 教程。我已正确执行所有步骤,但是,以下代码在 getlatitude() 和 getlongitude() 处出现错误。错误:无法解析方法 getlatitude()。如果有人能指出这个问题,那就太好了。还有一点需要注意,代码似乎从不使用 android.location.Location 类,我希望 getlatitude() 来自那里。

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import android.location.Location;


public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
    protected GoogleApiClient mGoogleApiClient;

    /**
     * Represents a geographical location.
     */
    protected String mLastLocation;
    protected static final String TAG = "basic-location-sample";
    protected TextView mLatitudeText;
    protected TextView mLongitudeText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
        mLatitudeText = (TextView) findViewById((R.id.latitude_text));
        mLongitudeText = (TextView) findViewById((R.id.longitude_text));
        buildGoogleApiClient();
   }

    protected synchronized void buildGoogleApiClient() {

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener((OnConnectionFailedListener) this)
                .addApi(LocationServices.API)
                .build();
    }
    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {

        mLastLocation = String.valueOf(LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient));
       if (mLastLocation!= null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
       }
 }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "Connection suspended");
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
    }


}

【问题讨论】:

    标签: java android google-play-services


    【解决方案1】:

    mLastLocation的类型:

    protected String mLastLocation;
    

    String 没有 getLatitudegetLongitude 方法。您的变量应该是Location 类型,并且在没有String.valueOf 调用的情况下进行初始化:

    protected Location mLastLocation;
    ...
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    

    【讨论】:

    • 非常感谢;我现在明白这应该如何工作了。我有滥用此线程的风险;该代码有效,但我仍然无法显示我的纬度和经度坐标。我怀疑 mLatitude 和 mLongitude 没有在 OnConnected 部分更新。有没有办法检查连接是否真的建立了?
    • @Rehan:不知道,恐怕 - 我不是 Android 开发者。但我怀疑如果您在 Stack Overflow 上搜索现有问题,您会发现很多相关问题。
    • 我会为此查看现有帖子。再次感谢您帮助解决位置问题:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2015-06-10
    • 2019-08-28
    • 2019-10-10
    • 2019-06-13
    • 2017-11-13
    相关资源
    最近更新 更多