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"
私は間違った方法で使用していると思います。 どうすれば使用できますか?
その行の問題は何ですか? – tyczj
@tyczj申し訳ありません、私はその行を忘れました。私は上記のエラーを追加しました。 –
実行時のアクセス権を処理する方法を見てください。http://developer.android.com/training/permissions/requesting.html – tyczj