2016-10-09 5 views
0

Google Place APIを使用して、PlaceDetectionApiを使用してデバイスの現在地を検出しようとしています。 現在の場所を検出するために必要な設定が有効になっていない場合は、LocationSettingsオブジェクトを作成して明示的に解決のために呼び出す必要なく、ロケーションサービスを有効にする必要があります。 何か不足していますか?Googleプレイス用のLocationSettings APIを使用して現在の場所を検出する

答えて

0

場所モード無効か手動かどうかをチェックすることができますし、無効にした場合示されているようにあなたがそれを有効にする設定を直接開くことができます。

public static boolean isLocationServicesAvailable(Context context) { 
     int locationMode = 0; 
     String locationProviders; 
     boolean isAvailable = false; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
      try { 
       locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); 
      } catch (Settings.SettingNotFoundException e) { 
       e.printStackTrace(); 
      } 

      isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF); 
     } else { 
      locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
      isAvailable = !TextUtils.isEmpty(locationProviders); 
     } 

     // boolean coarsePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED); 
     // boolean finePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED); 

     return isAvailable; 
    } 

openeningの設定を無効にした場合には:

if(!isLocationServicesAvailable(this)) { 
      // notify user 
      AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
      dialog.setMessage("enabled"); 
      dialog.setPositiveButton("open GPS", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
        // TODO Auto-generated method stub 
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(myIntent); 
        //get gps 
       } 
      }); 
      dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
        // TODO Auto-generated method stub 

       } 
      }); 
      dialog.show(); 
     } 
+0

私は、インターネットとGPSを手動でチェックするのではなく、より一般的な解決策があることを期待していました。 – hsen

+0

私はあなたが手動でそれをチェックしなければならないと思います。 –

関連する問題