【问题标题】:Dont shown 'Add Permission Check' while implementing SetMyLocationEnabled实施 SetMyLocationEnabled 时不显示“添加权限检查”
【发布时间】:2017-04-06 08:02:05
【问题描述】:

我正在尝试在地图顶部获取当前位置图标。但是,当我运行该应用程序时,地图的外观;

另一种情况是我写这行的时候;

 mGoogleMap.setMyLocationEnabled(true);

我无法收到任何“添加权限检查”警告,例如;

。所以我手工写了这些权限。

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)

            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

    }

我不知道这样做与否的正确方法。这是我的完整代码;

    private Activity activity = this;
    ActionBarDrawerToggle drawerToggle;
    DrawerLayout drawerLayout;
    RecyclerView recyclerMenu;
    AdapterMenu obAdapter;
    private Tracker mTracker;
    GoogleMap mGoogleMap;
    GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_harita);

        if (googleServicesAvailable()) {

            initmap();
        } else {
            //No google maps layout
        }


    }


    public boolean googleServicesAvailable() {
        GoogleApiAvailability api = GoogleApiAvailability.getInstance();
        int isAvailable = api.isGooglePlayServicesAvailable(this);
        if (isAvailable == ConnectionResult.SUCCESS)
            return true;
        else if (api.isUserResolvableError(isAvailable)) {
            Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
            dialog.show();
        } else
            Toast.makeText(this, "Can't connect to play services", Toast.LENGTH_LONG).show();

        return false;
    }

    private void initmap() {
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)

                if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }

        }
        mGoogleMap.setMyLocationEnabled(true);


    }

    public void geoLocate(View view) throws IOException {
        EditText searchtext = (EditText) findViewById(R.id.editAra);
        String location = searchtext.getText().toString();

        Geocoder geocoder = new Geocoder(this);
        List<Address> list = geocoder.getFromLocationName(location, 1);
        Address address = list.get(0);
        String locality = address.getLocality();

        Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

        double latitude = address.getLatitude();
        double longitude = address.getLongitude();

        goToLocationZoom(latitude, longitude, 15);
    }

    private void goToLocationZoom(double latitude, double longitude, float zoom) {

        LatLng LatLang = new LatLng(latitude, longitude);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LatLang, zoom);
        mGoogleMap.moveCamera(update);
    }
}

【问题讨论】:

  • 您可以在此处附上屏幕截图。也建议以后避免断链
  • 您能否粘贴您的日志或检查是否有任何与权限相关的日志显示@DAG
  • 没有权限相关的日志显示@Poonam
  • 从设置中启用位置权限并检查它是否显示当前位置。如果有效,则表示权限存在问题。

标签: android location android-permissions


【解决方案1】:

如果用户没有授予位置权限,您应该明确请求它(仅适用于 Android M 及更高版本)。然后你应该重写 onRequestPermissionsResult 并管理结果。

这里有一个你想要的工作示例:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String[] LOCATION_PERMISSIONS = {
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION
};
private static final int LOCATION_PERMISSIONS_REQUEST_CODE = 1;

private GoogleMap mGoogleMap;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void onMapReady(final GoogleMap googleMap) {
    this.mGoogleMap = googleMap;
    if (hasGrantedLocationPermissions()) {
        showCurrentLocationMapControl();
    } else {
        requestForLocationPermissions();
    }
}

@Override
public void onRequestPermissionsResult(final int requestCode, final @NonNull String[] permissions, final @NonNull int[] grantResults) {
    if (requestCode == LOCATION_PERMISSIONS_REQUEST_CODE) {
        if (grantResults.length == LOCATION_PERMISSIONS.length) {
            for (final int grantResult : grantResults) {
                if (PackageManager.PERMISSION_GRANTED != grantResult) {
                    return;
                }
            }
            showCurrentLocationMapControl();
        }
    }
}

private boolean hasGrantedLocationPermissions() {
    return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

private void requestForLocationPermissions() {
    ActivityCompat.requestPermissions(this, LOCATION_PERMISSIONS, LOCATION_PERMISSIONS_REQUEST_CODE);
}

private void showCurrentLocationMapControl() {
    if (null != this.mGoogleMap) {
        this.mGoogleMap.setMyLocationEnabled(true);
    }
}

}

不要忘记在清单文件中定义所需的权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

【讨论】:

    【解决方案2】:
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(thisActivity,Manifest.permission. ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
    
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.READ_CONTACTS)) {
    
            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
    
        } else {
    
            // No explanation needed, we can request the permission.
    
            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.READ_CONTACTS},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
    

    那么,权限结果

    @Override
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-06
      • 2015-05-19
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多