gpt4 book ai didi

java - 无法解析方法 getlatitude()

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:27 26 4
gpt4 key购买 nike

我是编程新手,正在学习 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());
}


}

最佳答案

查看mLastLocation的类型:

protected String mLastLocation;

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

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

关于java - 无法解析方法 getlatitude(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com