我的位置按钮未显示在地图片段中

时间:2018-10-25 05:20:42

标签: android google-maps

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

我的最低最高sdk 分别是 16 26

这是我的 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文件

  

fragment_map.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" />

0 个答案:

没有答案