【问题标题】:Android 6.0: setMyLocationEnabled not workAndroid 6.0:setMyLocationEnabled 不起作用
【发布时间】:2017-11-09 10:15:49
【问题描述】:

这里是我的fragmentsn-p:

public class MapFragmentTab extends Fragment implements OnMapReadyCallback {
    private MapView mapView;
    private View rootView;
    private GoogleMap googleMap;
    private GoogleApiClient mGoogleApiClient;
    int MY_PERMISSION_LOCATION = 10;

    private static final String TAG = MapFragmentTab.class.getName();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Debug.d(TAG, "onCreateView: savedInstanceState: " + AndroidUtil.bundle2String(savedInstanceState));
        rootView = inflater.inflate(R.layout.map_fragment_tab, container, false);
        init();
        return rootView;
    }

    private void init() {
        // Create an instance of GoogleAPIClient.
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                    .addApi(LocationServices.API)
                    .build();
        }
    }

    // Called immediately after onCreateView()
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mapView = rootView.findViewById(R.id.mapView);
        if (mapView != null) {
            mapView.onCreate(null);
            mapView.onResume();
            mapView.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap map) {
        Debug.d(TAG, "onMapReady: ");
        googleMap = map;
        setUpMap();

    }

    public void setUpMap() {
        Debug.d(TAG, "setUpMap: ");
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        if (Build.VERSION.SDK_INT >= 23) {
            marshmallowGPSPremissionCheck();
        } else {
            enableMyLocation();
        }
    }

    private void marshmallowGPSPremissionCheck() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Debug.d(TAG, "marshmallowGPSPremissionCheck: requestPermissions");
            requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_LOCATION);
        } else {
            enableMyLocation();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Debug.d(TAG, "onRequestPermissionsResult: ");
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_PERMISSION_LOCATION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            enableMyLocation();
        } else {
            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
    }

    private void enableMyLocation() {
        Debug.d(TAG, "enableMyLocation: ");
        if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Toast.makeText(getActivity(), "setMyLocationEnabled = true", Toast.LENGTH_SHORT).show();
        googleMap.setMyLocationEnabled(true);
    }
}

在 Android 4.3 上。当地图显示和我点击我的位置时 Button(在右上角)相机成功显示我的位置。 但是在 Android 6.0 上,当我点击这个 Button 时,什么都没有发生。

【问题讨论】:

    标签: android android-6.0-marshmallow


    【解决方案1】:
    public class MapActivity extends Activity implements OnMapReadyCallback {
    
    //    private static LatLng goodLatLng = new LatLng(37, -120);
    private GoogleMap googleMap;
    //    private EditText et_address;
    public static String TAG = MapActivity.class.getSimpleName();
    Context context;
    int MY_PERMISSION_LOCATION = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        context = MapActivity.this;
        // Initial Map
        try {
    
            MapFragment mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
            mapFragment.getMapAsync(this);
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    
    @Override
    public void onMapReady(GoogleMap map) {
    
        googleMap = map;
    
        setUpMap();
    
    }
    
    public void setUpMap() {
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        if (Build.VERSION.SDK_INT >= 23) {
            marshmallowGPSPremissionCheck();
    
        } else {
            enableMyLocation();
    
        }
    }
    
    
    private void marshmallowGPSPremissionCheck() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && checkSelfPermission(
                android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
            requestPermissions(
                    new String[]{
                            android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSION_LOCATION);
        } else {
            enableMyLocation();
        }
    
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_PERMISSION_LOCATION
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            enableMyLocation();
        }else {
            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
    }
    
    private void enableMyLocation() {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        googleMap.setMyLocationEnabled(true);
    }
    }
    

    这是xml文件

    <fragment
    
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment" />
    

    【讨论】:

    • 我更新了我的片段。但这对我没有帮助。 Toast 显示,但按钮不起作用。
    • 我用当前位置更新了显示地图的代码。我在 Android 6.0 上测试过,它工作正常。请检查您的代码。
    • 我认为你没有添加支持库。另外,您正在使用片段,所以用 getActivity() 替换“this”。
    • 我用你的代码更新了我的帖子。 Toast 已显示,但“我的位置”按钮不起作用
    • 我发现了问题。在设备上禁用检查我的位置。在我启用结果按钮后,我的位置是工作!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多