2016-03-18 1 views
10

MapActivityを開いたときに、自分の現在地に地図をフォーカスしたいと考えています。 私は特定の回線に問題があります。どのようにsetMyLocationEnabled権限要件を解決するには?

mMap.setMyLocationEnabled(true); 

コールは、ユーザーによって拒否することができる権限が必要です。コードは、明示的に許可が(「のcheckPermission」)と空くかどうかを確認したり、明示的に潜在的に処理する必要があります'SecurityException'。

この問題を解決するにはどうすればよいですか? この問題を検索したときにいくつかのことを読みましたが、何か役立つものが見つかりませんでした。いくつかのコメントを読んだ後

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
private static final String FIREBASE_URL="https://haerev.firebaseio.com/"; 
private Firebase firebaseRef; 
private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps2); 
    Firebase.setAndroidContext(this); 
    firebaseRef = new Firebase(FIREBASE_URL); 


    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    setUpMapIfNeeded(); 

} 

private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     mMap.setMyLocationEnabled(true); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 


      mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

       @Override 
       public void onMyLocationChange(Location arg0) { 
        // TODO Auto-generated method stub 

        mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!")); 
       } 
      }); 

     } 
    } 
} 

、この問題を読んで:

これは、完全なコードです

private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 

     if (ContextCompat.checkSelfPermission(MapsActivity.this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 


      if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, 
        Manifest.permission.ACCESS_FINE_LOCATION)) { 

       checkSelfPermission("You have to accept to enjoy the most hings in this app"); 
      } else { 


       ActivityCompat.requestPermissions(MapsActivity.this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION); 

      } 
     } 

     mMap.setMyLocationEnabled(true); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 


      mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

       @Override 
       public void onMyLocationChange(Location arg0) { 
        // TODO Auto-generated method stub 

        mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!")); 
       } 
      }); 

     } 
    } 
} 

しかし、今、私は新しいを持っている:

http://developer.android.com/training/permissions/requesting.htmlが、私はこれらの行を追加しました問題。行を認識できません。

"MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION"

私は間違った方法で使用していると思います。 どうすれば使用できますか?

+0

その行の問題は何ですか? – tyczj

+0

@tyczj申し訳ありません、私はその行を忘れました。私は上記のエラーを追加しました。 –

+1

実行時のアクセス権を処理する方法を見てください。http://developer.android.com/training/permissions/requesting.html – tyczj

答えて

9

を役に立てば幸い:

private void setUpMapIfNeeded() { 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
     } else { 
      Toast.makeText(MapsActivity.this, "You have to accept to enjoy all app's services!", Toast.LENGTH_LONG).show(); 
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { 
       mMap.setMyLocationEnabled(true); 
      } 
     } 

     if (mMap != null) { 


      mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

       @Override 
       public void onMyLocationChange(Location arg0) { 
        // TODO Auto-generated method stub 

        CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(arg0.getLatitude(), arg0.getLongitude())); 
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(12); 

        mMap.moveCamera(center); 
        mMap.animateCamera(zoom); 
       } 
      }); 

     } 
    } 
} 
+0

コメントを追加してください –

+0

あなたは許可を求めていますユーザーからの許可または許可がユーザーによって許可または拒否されたかどうかを確認する – user2779311

0

この
どちらかが

targetSdkVersion 22 

を設定したり、実行時の許可を求めるが、ここに参照してくださいしてみてください: - http://coderzpassion.com/android-new-runtime-permissions/ が、これは私のために働いているもので、それはウェブ上の大規模な調査の後おかげ

+0

動作していません。私の手助けをしてください。それは本当に私のプロジェクトを妨害しています。 –

+0

@GiladNeiger targetdkversionを22に設定するとどうなりますか? –

+0

私はどのようになっているのか分かりませんが、今は動作しています...ありがとう! しかし、今私はマップが作成されると、それは自分の位置に焦点を当てるようにしたいと思います。どうすればそれを実現できますか? –

3

もっと明確な実装:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    MapsInitializer.initialize(getContext()); 
    mGoogleMap = googleMap; 
    if(checkPermission()) 
    mGoogleMap.setMyLocationEnabled(true); 
    else askPermission(); 
} 



// Check for permission to access Location 
    private boolean checkPermission() { 
     Log.d(TAG, "checkPermission()"); 
     // Ask for permission if it wasn't granted yet 
     return (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED); 
    } 
    // Asks for permission 
    private void askPermission() { 
     Log.d(TAG, "askPermission()"); 
     ActivityCompat.requestPermissions(
       getActivity(), 
       new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 
       REQ_PERMISSION 
     ); 
    } 

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     Log.d(TAG, "onRequestPermissionsResult()"); 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case REQ_PERMISSION: { 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
        // Permission granted 
        if(checkPermission()) 
        mGoogleMap.setMyLocationEnabled(true); 

       } else { 
        // Permission denied 

      } 
      break; 
     } 
    } 
} 
関連する問題