首页 > 解决方案 > 我的位置按钮未显示在地图片段中

问题描述

我想在我的应用程序中显示mylocationbutton 。为此,我在代码中添加了setMyLocationEnabled(true)。但它不起作用。请为此建议我一个解决方案。这是我的代码。

我的最小最大sdk分别是1626

这是我的mapFragment,我在其中编写代码来设置 myLocation 按钮

MapFragment.java

public class MapFragment extends Fragment implements OnMapReadyCallback {

private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
private GoogleMap mMap;
private boolean isUp;
private View addressView,mapView;
private View searchLayout;
private TextView addressLine1, addressLine2;
private Button cancel, select;
private Geocoder geocoder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    //ToDo : Replace with orginal view
    View view = inflater.inflate(R.layout.fragment_map, null, false);
    init(view);
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    mapView = mapFragment.getView();

    setListeners();
    return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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;
    }
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.setMyLocationEnabled(true);
    isUp = false;
    addressView.setVisibility(View.INVISIBLE);
    // Add a marker in Sydney and move the camera
    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {

            setUpMarker(latLng);
            geocoder = new Geocoder(getContext());
            try {
                List<Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
                search(addresses);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    if (mapView != null &&
            mapView.findViewById(Integer.parseInt("1")) != null) {
        // Get the button view
        View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
        // and next place it, on bottom right (as Google Maps app)
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                locationButton.getLayoutParams();
        // position on right bottom
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        layoutParams.setMargins(0, 0, 30, 30);
    }
}
}

我的xml文件

片段映射.xml

 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.HomeActivity" />

清单文件中的权限

清单文件

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

标签: androidgoogle-maps

解决方案


推荐阅读