2014-01-17 2 views

답변

6

나는 질문에 대답하기에는 너무 늦었다 고 생각합니다. 하지만 괜찮아. 따라서 새로운 API LocationSettingsRequest을 사용하면 클릭 한 번으로 앱 내에서 위치를 사용할 수 있습니다. 그렇게하기 위해;

 GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this).build(); 
     mGoogleApiClient.connect(); 

GoogleApiClient

를 초기화 그리고 다음과 같이 초기화 된 LocationRequest 확인 :
LocationRequest mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(Util.UPDATE_INTERVAL_IN_MILLISECONDS); 
     mLocationRequest.setFastestInterval(Util.FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); 
    } 

지금 LocationSettingsRequest를 통해 위치 설정 요청을합니다.

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
       .addLocationRequest(mLocationRequest); 

     //************************** 
     builder.setAlwaysShow(true); //this is the key ingredient 
     //************************** 

     PendingResult<LocationSettingsResult> result = 
       LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); 
     result.setResultCallback(locationSettingsResultCallback); 

그래서, 바로 당신이 LocationSettingsRequest을 같이 그것은 팝업 것 같은 대화 :

enter image description here

그래서 사용자가 선택한 어떤 옵션을 처리하기 위해, 당신은을 구현해야합니다 콜백은 각 옵션에 아래와 같이 처리합니다 :

ResultCallback<LocationSettingsResult> locationSettingsResultCallback = new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(LocationSettingsResult result) { 
      final Status status = result.getStatus(); 
      final LocationSettingsStates state = result.getLocationSettingsStates(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        // All location settings are satisfied. The client can initialize location 
        // requests here. 
        //startLocationUpdates(); 


        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        // Location settings are not satisfied. But could be fixed by showing the user 
        // a dialog. 
        try { 
         // Show the dialog by calling startResolutionForResult(), 
         // and check the result in onActivityResult(). 
         status.startResolutionForResult(
           PickupLocationActivity.this, 1000); 
        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        // Location settings are not satisfied. However, we have no way to fix the 
        // settings so we won't show the dialog. 
        break; 
      } 
     } 
    }; 

참조 :https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

관련 문제