谷歌地图视图损坏扩展片段类

时间:2018-06-02 03:24:53

标签: android google-maps fragment

您好我是android新手。这段代码使用扩展Activity类,但是我使用带有Fragment扩展的这个类,它不起作用。我添加了清单中的所有权限。我将这个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 :(得分:0)

您将MapFragment作为LocationFragment的子片段,因此您需要使用getChildFragmentManager

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