更改Mylocation按钮的位置

时间:2019-02-18 16:01:36

标签: android location-button

我尝试重新定位Googlemaps Mylocation按钮时,我的应用程序一直崩溃。 这是我的代码:

private void locationButton() {

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

    View locationButton = ((View) mapFragment.getView().findViewById( Integer.parseInt( "1" ) ).
            getParent()).findViewById( Integer.parseInt( "2" ) );
    if (locationButton != null && locationButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
        // location button is inside of RelativeLayout
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();

        // Align it to - parent BOTTOM|LEFT
        params.addRule( RelativeLayout.ALIGN_PARENT_TOP, 0 );
        params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );

        // Update margins, set to 10dp
        final int margin = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 150,
                getResources().getDisplayMetrics() );
        params.setMargins( margin, margin, margin, margin );

        locationButton.setLayoutParams( params );
    }

}

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

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){

        buildGoogleApiClient();

        mMap.setMyLocationEnabled(true);
        mMap.setOnMarkerDragListener(this);
        locationButton();


    }

}

一旦我调用了应该更改位置的函数,应用就会崩溃。这是为什么?如何解决我的问题并更改其位置?

我只需将第一行更改为:

即可解决问题
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

0 个答案:

没有答案