Android Google Maps setMyLocationEnabled(true)

前端 未结 2 892
攒了一身酷
攒了一身酷 2021-01-24 20:24

I\'m working through a tutorial and my app is basically working except for one small detail. I have an Options menu to demonstrate things like showing the map as a Satellite vie

2条回答
  •  时光说笑
    2021-01-24 21:14

    Initially when first time your GoogleMap loads takes some time and there fore Location will be null.

    So for your both menu options check this way.

    if(map!=null){
     map.setMyLocationEnabled(true);
    }
    
    
    if(map!=null){
        Location myLocation = map.getMyLocation();
    
        if(myLocation !=null){
        LatLng myLatLng = new LatLng(myLocation.getLatitude(),
                myLocation.getLongitude());
    
    
        CameraPosition myPosition = new CameraPosition.Builder()
                .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
       }
    }
    

提交回复
热议问题