我是编程新手,并且正在关注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());
}
}
答案 0 :(得分:3)
查看mLastLocation
的类型:
protected String mLastLocation;
String
没有getLatitude
和getLongitude
方法。您的变量应为Location
类型,并且在没有String.valueOf
调用的情况下进行初始化:
protected Location mLastLocation;
...
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);