Карта Google повреждена из-за расширенного класса фрагментов - PullRequest
0 голосов
/ 02 июня 2018

Здравствуйте, я новичок в Android.этот код работает с расширением класса Activity, но я использую этот класс с расширением с фрагментом, он не работает.Я добавил все разрешения в манифесте.Я использую этот класс Fragment с меню навигации Bottom.Пожалуйста, помогите мне.

Это мой класс фрагментов (LoctionFragment.java)

public class LoctionFragment extends Fragment implements OnMapReadyCallback, LocationListener{

GoogleMap map;
LocationManager locationManager;
TextView viewLocation;


public LoctionFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.location_layout, container, false);

    viewLocation = v.findViewById(R.id.viewLocation);

    SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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.

    }
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, (LocationListener) this);
    return v;
}

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

@Override
public void onLocationChanged(Location location) {

    map.clear();
    LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(currentLocation);
    markerOptions.title("MY LOCATION");
    map.addMarker(markerOptions);
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 17.0f));
    if (location != null) {
        double latti = location.getLatitude();
        double longi = location.getLongitude();

        viewLocation.setText("Current Location is :" + latti + "," + longi);
    } else {
        viewLocation.setText("Unable to find current location . Try again later");
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}

}

Это представление карты в формате XML (location_layout.xml)

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="383dp"
    android:layout_x="1dp"
    android:layout_y="39dp" />

1 Ответ

0 голосов
/ 02 июня 2018

Вы ставите MapFragment как дочерний фрагмент LocationFragment, поэтому вам нужно использовать getChildFragmentManager:

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
...