2
프로그래밍에 익숙하지 않으며 Google 위치 API 자습서를 따르고 있습니다. 나는 모든 단계를 제대로 따라 왔지만, 다음 코드는 getlatitude() 및 getlongitude()에서 오류를 발생시킵니다. 오류 : getlatitude() 메서드를 해결할 수 없습니다. 누군가가이 문제를 지적 할 수 있다면 좋을 것입니다. 한 가지 더주의해야 할 점은 코드는 android.location.Location 클래스를 사용하지 않는 것으로 보이며 getlatitude()가 거기에서 올 것이라고 기대했습니다. mLastLocation
의 종류에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());
}
}
감사 무리; 귀하의 변수는 유형
Location
의 수, 그리고String.valueOf
호출하지 않고 초기화한다 나는 이제 이것이 어떻게 작동해야 하는지를 이해한다. 이 스레드를 악용 할 위험에 처해 있습니다. 코드는 작동하지만 위도와 경도의 좌표를 표시 할 수 없습니다. 나는 mLatitude와 mLongitude가 OnConnected 부분에서 업데이트되지 않는다고 생각합니다. 실제로 연결이 설정되었는지 확인하는 방법이 있습니까? – Rehan@ 리한 : 안타깝네요, 저는 두려워요. 저는 Android 개발자가 아닙니다. 그러나 Stack Overflow에 관한 기존 질문을 검색하면 많은 관련 항목을 찾을 수 있습니다. –
기존 게시물을 살펴볼 것입니다. 위치 문제를 도와 주셔서 다시 한번 감사드립니다. :) – Rehan