2016-03-18 80 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!")); 
       } 
      }); 

     } 
    } 
} 

讀了一些意見後,讀此問題:http://developer.android.com/training/permissions/requesting.html

我添加這些行:

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!")); 
       } 
      }); 

     } 
    } 
} 

但現在我有一個新的問題。它不承認該行

「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當你將targetsdkversion設置爲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; 
     } 
    } 
}