사용자의 위치를 사용하여지도 활동에 마커를 배치하거나이 경우 위도와 경도를 인쇄하는 Android 앱을 명확하게 작성하려고합니다. 그러나, 나는 나를 위해 일하는 튜토리얼 또는 이전 질문을 찾을 수 없습니다. GPS를 사용하여 위치 객체 또는 위도와 경도의 형태로 사용자의 현재 위치를 가져 오는 가장 간단한 방법을 찾고 있습니다.현재 GPS 위치 가져 오기 android studio
public class MainActivity extends AppCompatActivity
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private TextView lat, lng;
private GoogleApiClient mGoogleApiClient;
private Location mCurrentLocation;
private boolean mRequestingLocationUpdates;
private LocationRequest mLocationRequest;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat = (TextView) findViewById(R.id.lat);
lng = (TextView) findViewById(R.id.lng);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) {
startLocationUpdates();
}
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
@Override
public void onConnected(Bundle bundle) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mCurrentLocation != null) {
lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude()));
lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude()));
}
else
Log.i("Problem", "mCurrentLocation is null");
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
updateUI();
}
private void updateUI() {
lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude()));
lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude()));
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
내 AndroidManifest.xml에는 다음과 같습니다 : 다음과 같이 내 현재 코드는 this tutorial에서 직접입니다
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.locationtest">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="LocationTest"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
난 항상 로그 메시지 "mCurrentLocation가 null"을 얻을 것 같다, 그래서 아무것도 없습니다 앱이 변경됩니다. mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
행에 "사용자가 거부 할 수있는 권한이 필요합니다."라는 경고가 표시됩니다. 어떤 도움을 주셔서 감사합니다, 미리 감사드립니다.
는 편집 :이 경고를 해결
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
: 나는 모든 라인을 둘러싼 if 문에 "전화 사용자에 의해 거부 될 수 있습니다 권한이 필요합니다." 그러나 onConnect() 메서드에서 사용 권한이 거부되고 mCurrentLocation은 여전히 null입니다.
AndroidManifest.xml 파일 – user2025187
을 추가하십시오.이 답변에서 자습서를 따르십시오. 이렇게하면 현재 위치, 위도, 경도를 확인할 수 있으며 현재 위도와 경도를 실시간으로 업데이트 할 수 있습니다. http://stackoverflow.com/a/42131361/4201474 – TranHieu