0
화면에 들어가면 GPS가 켜져 있는지 확인하고 그렇지 않으면 GPS를 활성화하는 대화 상자가 표시됩니다. 사용자가 예를 클릭하면 onActivityResult -> GPS가 켜지고 위치를 얻으려고 시도하지만 null이 항상 반환됩니다.현재 위치는 안드로이드에서 null이됩니다
GPS가있는 화면으로 들어가면 위치가 올바르게 검색됩니다. 나는 며칠 동안이 일에 어려움을 겪었으므로 자원을 찾을 수없는 것 같습니다.
제가 뭘 잘못했는지 도와주세요. u는 LOG를 받고 있지 않은 경우
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recyler_list, container, false);
initializeView(view);
registerGoogleClient()
return view;
}
private void registerGoogleClient() {
try {
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
LocationRequest locationRequest = LocationRequest.create();
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
locationSettingsRequest = builder.build();
connectGoogleApiClient();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onStart() {
super.onStart();
try {
connectGoogleApiClient();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
private void connectGoogleApiClient() {
try {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
checkDeviceGps();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onStop() {
super.onStop();
disConnectGoogleApiClient();
}
private void disConnectGoogleApiClient() {
try {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onConnected(Bundle connectionHint) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
System.out.println("if block");
if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
} else {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
System.out.println("mLastLocation is====>" + mLastLocation);
if (mLastLocation != null) {
latitude = mLastLocation.getLatitude();
longtude = mLastLocation.getLongitude();
System.out.println("Latitude====>" + mLastLocation.getLatitude());
System.out.println("Longitude====>" + mLastLocation.getLongitude());
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
private void checkDeviceGps() {
try {
PendingResult<LocationSettingsResult> pendingResult =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, locationSettingsRequest);
pendingResult.setResultCallback(this);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(getActivity(), 100);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100:
getLatLong();
break;
}
}
private void getLatLong() {
try {
if (mLastLocation != null) {
latitude = mLastLocation.getLatitude();
longtude = mLastLocation.getLongitude();
System.out.println("Latitude====>" + mLastLocation.getLatitude());
System.out.println("Longitude====>" + mLastLocation.getLongitude());
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
u는 GPS를 허용 한 후에 데이터를 찾기 위해 몇 초 동안 20-30 초를 기다리려고 했습니까? – Ak9637
어떻게 할 수 있습니까? 코드로 설명해주십시오. – AbhiRam
위치를 얻으려면 몇 초 기다려야합니다. 이것을 위해 스레드를 사용할 수 있습니다. –