【问题标题】:Android Maps: My Location Button doesn't work on activity first launchAndroid 地图:我的位置按钮在活动首次启动时不起作用
【发布时间】:2019-03-16 09:54:45
【问题描述】:

当我第一次运行我的地图活动并接受位置权限时,如果我按下“我的位置”按钮,没有任何反应。

但是,如果我关闭活动并返回它,那么按钮之后就可以正常工作了!所以,只有在我第一次接受活动启动的权限后才会出现问题。

如果我在接受权限后重新创建活动,问题就解决了,但我不喜欢那个解决方案,而且我想了解问题的原因。

是什么导致了这个错误?

 public class MapsActivity extends FragmentActivity implements` OnMapReadyCallback {

        private GoogleMap mMap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);

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

        GoogleMapOptions options = new GoogleMapOptions();
        options.mapType(GoogleMap.MAP_TYPE_NORMAL);

        mapFragment.newInstance(options);
        mapFragment.getMapAsync(this);
    }

#

public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            requestLocationPermissions();
            mMap.getUiSettings().setMapToolbarEnabled(true);
    }

#

 public void requestLocationPermissions() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                    15);
        }else{

            mMap.setMyLocationEnabled(true);//needs location permission
            mMap.getUiSettings().setMyLocationButtonEnabled(true);

        }
    }

    @SuppressLint("MissingPermission")
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 15) {
            if (grantResults.length == 0) {
                requestLocationPermissions();
            } else {
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED  ) {
                    Toast.makeText(this, "You have to accept all Permissions!", Toast.LENGTH_SHORT).show();
                    Log.e("Permission refused","Permission refused");
                    requestLocationPermissions();
                } else {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED ) {
                    mMap.setMyLocationEnabled(true);
                    mMap.getUiSettings().setMyLocationButtonEnabled(true);
                   }
                }
            }
        }

    }

【问题讨论】:

  • 接受定位权限后,不带动画刷新当前活动,希望对您有所帮助
  • @Sniffer 好的,如果我 reCreate() 活动,问题就解决了。但是,我不喜欢这种解决方案,而且我仍然不知道问题的原因。
  • 请先打开您的谷歌地图安卓应用程序,然后再打开您的应用程序。如果它可以正常工作,请告诉我。(确保您的 GPS 已打开)
  • @MohitGaur 1) 删除了我的应用。 2) GPS 开启,我打开谷歌地图应用程序,看到我当前的位置处于活动状态。 3)安装我的应用程序,接受权限。同样的问题...
  • 不,不要删除它,在打开你的应用之前打开谷歌地图

标签: android google-maps google-maps-api-3 google-maps-android-api-2 android-maps-v2


【解决方案1】:

调试您的代码以检查您在首次授予权限时收到的纬度和经度值。那个时候可能没有设置位置。如果 GPS 尚未打开,获取精确位置有时需要更多时间。我在一些用于测试的设备上遇到了同样的问题。

【讨论】:

  • GPS 已开启。当我授予许可时,位置是正确的。即使我等待一段时间,位置按钮也不会做任何事情,除非我重新创建活动
【解决方案2】:

我一直在努力解决这个问题,目前我发现的唯一解决方法是使用 fuse location client 检索设备的位置并为“我的位置”按钮添加自定义行为。

以这种方式,它可以一直工作而无需重新加载活动。

private void getDeviceLocation() {
        /*
         * Get the best and most recent location of the device, which may be null in rare
         * cases when a location is not available.
         */
        try {
            if (hasLocationPermission()) {
                fusedLocationClient
                        .getLastLocation()
                        .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                            @Override
                            public void onSuccess(Location location) {
                                // Got last known location. In some rare situations this can be null.
                                if (location != null) {
                                    lastLocation = location;
                                    // Logic to handle location object
                                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                                            new LatLng(location.getLatitude(),
                                                    location.getLongitude()), DEFAULT_ZOOM));
                                    mMap.setOnMyLocationButtonClickListener(MapsActivity.this);
                                    mMap.setMyLocationEnabled(true);
                                    mMap.getUiSettings().setMyLocationButtonEnabled(true);
                                } else {
                                    mMap.setMyLocationEnabled(false);
                                    mMap.getUiSettings().setMyLocationButtonEnabled(false);
                                }
                            }
                        });
            }
        } catch (SecurityException e) {
            Log.e("Exception: %s", e.getMessage());
        }
    }

MapsActivity 是 GoogleMap.OnMyLocationButtonClickListener 的监听器然后有这段代码

@Override
public boolean onMyLocationButtonClick() {
    // I've tried just setting the default behavior of the button, but just works after reloading
    // the activity, then I've managed the click event to latest location; now it works for
    // all cases.
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(lastLocation.getLatitude(),
                    lastLocation.getLongitude()), DEFAULT_ZOOM));
    return false;
}

这是onMapReady的代码

@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    mMap.getUiSettings().setZoomControlsEnabled(true);

    getMuseums();

    if (!hasLocationPermission()) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_PERMISSION_LOCATION);
        return;
    }
    getDeviceLocation();
}

而我的实际 DEFAULT_ZOOM 值为 10。

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 2022-01-08
    • 2016-09-03
    • 1970-01-01
    • 2021-09-04
    • 2020-07-30
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多