Tôi muốn đặt bản đồ của mình tập trung vào vị trí hiện tại của tôi khi mở MapActivity
. Tôi có một vấn đề với một dòng cụ thể:Cách giải quyết yêu cầu cấp quyền setMyLocationEnabled?
mMap.setMyLocationEnabled(true);
Gọi đòi hỏi sự cho phép có thể bị từ chối bởi người sử dụng: Mã một cách rõ ràng nên kiểm tra xem nếu phép là availabe với ('checkPermission') hoặc xử lý một cách rõ ràng với tiềm năng 'Ngoại lệ an ninh'.
Làm cách nào để giải quyết vấn đề này? Tôi đã đọc một số điều khi tôi tìm kiếm sự cố này và tôi không thể tìm thấy điều gì hữu ích.
Đây là mã đầy đủ:
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!"));
}
});
}
}
}
Sau khi đọc một số ý kiến, và đọc vấn đề này: http://developer.android.com/training/permissions/requesting.html
tôi thêm những dòng này:
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!"));
}
});
}
}
}
Nhưng bây giờ tôi có một mới vấn đề. Nó không nhận ra dòng
"MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION".
Tôi đoán tôi đang sử dụng nó theo cách sai. Tôi nên sử dụng nó như thế nào?
vấn đề với dòng đó là gì? – tyczj
@tyczj xin lỗi tôi quên dòng đó. Tôi đã thêm lỗi ở trên. –
bạn nên xem xét cách xử lý các quyền thời gian chạy http://developer.android.com/training/permissions/requesting.html – tyczj